Solved

halting a process for 5 seconds - given hwnd, setforgroundwindow

Posted on 2007-11-27
24
314 Views
Last Modified: 2008-02-01
I acquired hwnd before hand and now I want to pass it directly into setforgroundwindow()


#include <iostream>
#include <windows.h>

using namespace std;

int main ()
{

      HWND hwnd1 = 0x001B027E;
      HWND hwnd2 = 0x000403D6;
      
      // how do I tell it to halt for 5 seconds?
      SetForegroundWindow( hwnd1 );      

      // how do I tell it to halt for 5 seconds?
      SetForegroundWindow( hwnd2 );
      

      return 0;
}





yet it pops

focus_test.cpp:19: error: invalid conversion from `int' to `HWND__*'
focus_test.cpp:20: error: invalid conversion from `int' to `HWND__*'



is it because I can't pass it directly using numerals ?

0
Comment
Question by:Troudeloup
  • 11
  • 6
  • 4
  • +1
24 Comments
 
LVL 14

Expert Comment

by:wayside
ID: 20359323
HWND is not an int, even though it looks like one. So you must cast the int:

HWND hwnd1 = (HWND) 0x001B027E;
HWND hwnd2 = (HWND) 0x000403D6;
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20359328
this is a weird implementation, but, anyway, you need to make a casting, like this:
HWND hwnd1 = (HWND)0x001B027E;
0
 
LVL 14

Expert Comment

by:wayside
ID: 20359346
> // how do I tell it to halt for 5 seconds?

Was this part of the question? If so, just do

Sleep(5000); // Sleep takes milliseconds
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:Troudeloup
ID: 20359371
yes
0
 

Author Comment

by:Troudeloup
ID: 20359380
i got this error


focus_test.cpp:24: error: `sleep' was not declared in this scope
0
 
LVL 14

Expert Comment

by:wayside
ID: 20359397
It's not

sleep

it's

Sleep

Note the Capital S at the beginning
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20359400
it is Sleep, with upper case
0
 

Author Comment

by:Troudeloup
ID: 20359497
Sleep works now :D



A problem:


Setforeground()

makes the window blink orange yet it does not bring the window on top of others.

how do i do it?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20359517
use SetWindowPos() as explained in a previous question.
0
 

Author Comment

by:Troudeloup
ID: 20359665
yes i tried it


funny enough, when I did this


SetWindowPos(  hwnd1, HWND_TOPMOST, 250, 0, 0, 0, SWP_NOSIZE );


the window stays on top of all the other windows even though i made it lose focus.


what should I do to get it right?
0
 
LVL 14

Expert Comment

by:wayside
ID: 20359684
Try using HWND_TOP or HWND_NOTOPMOST, see if either of these does what you want.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20359694
also SWP_NOMOVE will be helpful to avoid moving the window while activating.
0
 

Author Comment

by:Troudeloup
ID: 20359763
how do I make a window invisible?
0
 

Author Comment

by:Troudeloup
ID: 20359767
and then visible?
0
 

Author Comment

by:Troudeloup
ID: 20359771
i think it's two functions, or two parameters.
0
 
LVL 14

Expert Comment

by:wayside
ID: 20359791
ShowWindow(hWnd, SW_HIDE)

and

ShowWIndow(nWnd, SW_SHOW)
0
 
LVL 14

Expert Comment

by:wayside
ID: 20359798
Oops, typos, should be

ShowWindow(hWnd, SW_SHOW)
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20359814
The problem with the HWND_TOPMOST is that it is a very unfair method cause only one window can be topmost. So, if you ever play that game you get what you deserved ;-)

If no other window was topmost, the HWND_TOP should be sufficient. Or try SetActiveWindow(hwnd) what should have the same effect for top level windows.

Regards, Alex

0
 

Author Comment

by:Troudeloup
ID: 20359834
ok i am lost in the jungle. luckily it was just a test so I am still alive.



let's use notepads as examples:


how do I make a minimized notepad gain topmost so it can be edited?

how do I make a notepad behind that first one to gain topmost so it can be edited?


SetWindowPos(  hwnd1, HWND_TOPMOST, 250, 0, 0, 0, SWP_NOSIZE );

this would make the window  editable, but then when it's supposed to go behind the other notepad, it's still on the top.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20359995
>>>> SetWindowPos(  hwnd1, HWND_TOPMOST, 250, 0, 0, 0, SWP_NOSIZE );

Did you try

      ShowWindow(hwnd1, SW_SHOWNORMAL);
      SetActiveWindow(hwnd1);

?
0
 

Author Comment

by:Troudeloup
ID: 20360037
     HWND hwnd1 = (HWND)0x001B027E;
      HWND hwnd2 = (HWND)0x000403D6;

      SetForegroundWindow( hwnd1 );
      SetWindowPos(  hwnd1, HWND_TOPMOST, 250, 0, 0, 0, SWP_NOSIZE );
        Sleep ( 5000 );
      
      SetWindowPos(  hwnd1, HWND_NOTOPMOST, 250, 0, 0, 0, SWP_NOSIZE );
      SetForegroundWindow( hwnd2 );
      SetWindowPos(  hwnd2, HWND_TOPMOST, 250, 0, 0, 0, SWP_NOSIZE );

      
      SetWindowPos(  hwnd2, HWND_NOTOPMOST, 250, 0, 0, 0, SWP_NOSIZE );







I tried doing this, but then it's strange. see for yourself by running it


I just want to automate alt-tab with multiple windows by making them become editable.
0
 

Author Comment

by:Troudeloup
ID: 20360190
     ShowWindow(hwnd1, SW_SHOWNORMAL);
      SetActiveWindow(hwnd1);



this crashed them, I don't know why
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20360604
>>>> I tried doing this, but then it's strange. see for yourself by running it

????  Do you think calling a wrong function more than once does make things better?

>>>> this crashed them, I don't know why

The hwnd1 must be a top-level window. But even if it isn't it should not crash. Are you sure the hwnd1 is a valid window at all?
0
 

Author Comment

by:Troudeloup
ID: 20360618
i am sure.

i got sleep from this thread, so I opened another

http://www.experts-exchange.com/Programming/Languages/CPP/Q_22985760.html
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question