Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

halting a process for 5 seconds - given hwnd, setforgroundwindow

Posted on 2007-11-27
24
Medium Priority
?
333 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
Independent Software Vendors: 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

650 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