Solved

halting a process for 5 seconds - given hwnd, setforgroundwindow

Posted on 2007-11-27
24
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

691 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