Nicholas Pipitone
Nicholas Pipitone

Reputation: 4192

Checking if a window is active

I have a console application that uses GetAsyncKeyState();, but if the user is on looking at another window and pressed a button, GetAsyncKeyState(); picked it up (I already knew that).

Without having to do GetActiveWindow();, how else could I check if my window is the one on top?

EDIT: GetConsoleWindow() == GetForegroundWindow() worked.

Upvotes: 1

Views: 12349

Answers (2)

Ruza
Ruza

Reputation: 41

This thing worked for me:

HWND name;
name=GetForegroundWindow();

while(!_kbhit()){
   if(name==GetForegroundWindow())
      printf("Mine window is active\n");
   else
      printf("Mine window is not active\n");
}

Upvotes: 2

Devolus
Devolus

Reputation: 22094

To get the active Window you can call GetActiveWindow. GetFocus will return the handle of the window that has the input focus. This window can be a control as well. So you can check against your window handle and see if it has the focus.

Upvotes: 1

Related Questions