Solved

halting a process for 5 seconds - given hwnd, setforgroundwindow

Posted on 2007-11-27
24
311 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
Comment Utility
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
Comment Utility
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
Comment Utility
> // 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
 

Author Comment

by:Troudeloup
Comment Utility
yes
0
 

Author Comment

by:Troudeloup
Comment Utility
i got this error


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

Expert Comment

by:wayside
Comment Utility
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
Comment Utility
it is Sleep, with upper case
0
 

Author Comment

by:Troudeloup
Comment Utility
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
Comment Utility
use SetWindowPos() as explained in a previous question.
0
 

Author Comment

by:Troudeloup
Comment Utility
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
Comment Utility
Try using HWND_TOP or HWND_NOTOPMOST, see if either of these does what you want.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
also SWP_NOMOVE will be helpful to avoid moving the window while activating.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:Troudeloup
Comment Utility
how do I make a window invisible?
0
 

Author Comment

by:Troudeloup
Comment Utility
and then visible?
0
 

Author Comment

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

Expert Comment

by:wayside
Comment Utility
ShowWindow(hWnd, SW_HIDE)

and

ShowWIndow(nWnd, SW_SHOW)
0
 
LVL 14

Expert Comment

by:wayside
Comment Utility
Oops, typos, should be

ShowWindow(hWnd, SW_SHOW)
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
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
Comment Utility
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
Comment Utility
>>>> SetWindowPos(  hwnd1, HWND_TOPMOST, 250, 0, 0, 0, SWP_NOSIZE );

Did you try

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

?
0
 

Author Comment

by:Troudeloup
Comment Utility
     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
Comment Utility
     ShowWindow(hwnd1, SW_SHOWNORMAL);
      SetActiveWindow(hwnd1);



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

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> 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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

728 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now