[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

halting a process for 5 seconds - given hwnd, setforgroundwindow

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
Troudeloup
Asked:
Troudeloup
  • 11
  • 6
  • 4
  • +1
1 Solution
 
waysideCommented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
this is a weird implementation, but, anyway, you need to make a casting, like this:
HWND hwnd1 = (HWND)0x001B027E;
0
 
waysideCommented:
> // 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
TroudeloupAuthor Commented:
yes
0
 
TroudeloupAuthor Commented:
i got this error


focus_test.cpp:24: error: `sleep' was not declared in this scope
0
 
waysideCommented:
It's not

sleep

it's

Sleep

Note the Capital S at the beginning
0
 
Jaime OlivaresSoftware ArchitectCommented:
it is Sleep, with upper case
0
 
TroudeloupAuthor Commented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
use SetWindowPos() as explained in a previous question.
0
 
TroudeloupAuthor Commented:
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
 
waysideCommented:
Try using HWND_TOP or HWND_NOTOPMOST, see if either of these does what you want.
0
 
Jaime OlivaresSoftware ArchitectCommented:
also SWP_NOMOVE will be helpful to avoid moving the window while activating.
0
 
TroudeloupAuthor Commented:
how do I make a window invisible?
0
 
TroudeloupAuthor Commented:
and then visible?
0
 
TroudeloupAuthor Commented:
i think it's two functions, or two parameters.
0
 
waysideCommented:
ShowWindow(hWnd, SW_HIDE)

and

ShowWIndow(nWnd, SW_SHOW)
0
 
waysideCommented:
Oops, typos, should be

ShowWindow(hWnd, SW_SHOW)
0
 
itsmeandnobodyelseCommented:
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
 
TroudeloupAuthor Commented:
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
 
itsmeandnobodyelseCommented:
>>>> SetWindowPos(  hwnd1, HWND_TOPMOST, 250, 0, 0, 0, SWP_NOSIZE );

Did you try

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

?
0
 
TroudeloupAuthor Commented:
     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
 
TroudeloupAuthor Commented:
     ShowWindow(hwnd1, SW_SHOWNORMAL);
      SetActiveWindow(hwnd1);



this crashed them, I don't know why
0
 
itsmeandnobodyelseCommented:
>>>> 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
 
TroudeloupAuthor Commented:
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

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 11
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now