GetForegroundWindow()
-
Hi all. Im trying to find out whether a window is active or not. This is what i have so far...
#include #include using namespace std; int main(){ Sleep(1000); if(strcmp("00315434", GetForegroundWindow()) == 0){ cout << "Notepad found! " << endl; } return 0; }
But i get an error. error C2664: 'strcmp' : cannot convert parameter 2 from 'struct HWND__ *(__stdcall *)(void)' to 'const char *' How do i compare what GetForegroundWindow returns so i can make my app look for a specific window? Thanx in advance! -
Hi all. Im trying to find out whether a window is active or not. This is what i have so far...
#include #include using namespace std; int main(){ Sleep(1000); if(strcmp("00315434", GetForegroundWindow()) == 0){ cout << "Notepad found! " << endl; } return 0; }
But i get an error. error C2664: 'strcmp' : cannot convert parameter 2 from 'struct HWND__ *(__stdcall *)(void)' to 'const char *' How do i compare what GetForegroundWindow returns so i can make my app look for a specific window? Thanx in advance!dellthinker wrote:
But i get an error. error C2664: 'strcmp' : cannot convert parameter 2 from 'struct HWND__ *(__stdcall *)(void)' to 'const char *'
The
GetForegroundWindow()
function returns a handle to the foreground window, while the second argument tostrcmp()
is a null-terminated string (to compare). Of course, you already knew all of this.dellthinker wrote:
How do i compare what GetForegroundWindow returns so i can make my app look for a specific window?
By also employing
GetWindowText()
. Otherwise, checkoutFindWindow()
."Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
dellthinker wrote:
But i get an error. error C2664: 'strcmp' : cannot convert parameter 2 from 'struct HWND__ *(__stdcall *)(void)' to 'const char *'
The
GetForegroundWindow()
function returns a handle to the foreground window, while the second argument tostrcmp()
is a null-terminated string (to compare). Of course, you already knew all of this.dellthinker wrote:
How do i compare what GetForegroundWindow returns so i can make my app look for a specific window?
By also employing
GetWindowText()
. Otherwise, checkoutFindWindow()
."Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Ok so now im using GetWindowText() and it seems to capture all window titles. But i need it to look for a specific window, Notepad in this case. Here is my code:
#include #include using namespace std; int main(void) { HWND currenthwnd,hwnd; char s[1024]; hwnd=NULL; while (1){ Sleep(1000); currenthwnd=GetForegroundWindow(); if (currenthwnd!=hwnd){ hwnd=currenthwnd; GetWindowText(hwnd,s,1024); printf("%s\n",s); } if(strstr(s, "Notepad") == 0){ cout << "Notepad is open! " << endl; } else{ cout << "Its not open " << endl; } } return 0; }
Problem is when the program is running it prints Notepad is open when it really isnt. Can someone help me fix this? Thanx in advance! -
Ok so now im using GetWindowText() and it seems to capture all window titles. But i need it to look for a specific window, Notepad in this case. Here is my code:
#include #include using namespace std; int main(void) { HWND currenthwnd,hwnd; char s[1024]; hwnd=NULL; while (1){ Sleep(1000); currenthwnd=GetForegroundWindow(); if (currenthwnd!=hwnd){ hwnd=currenthwnd; GetWindowText(hwnd,s,1024); printf("%s\n",s); } if(strstr(s, "Notepad") == 0){ cout << "Notepad is open! " << endl; } else{ cout << "Its not open " << endl; } } return 0; }
Problem is when the program is running it prints Notepad is open when it really isnt. Can someone help me fix this? Thanx in advance!dellthinker wrote:
Problem is when the program is running it prints Notepad is open when it really isnt.
dellthinker wrote:
if(strstr(s, "Notepad") == 0)
Shouldn't that be if(strstr(s, "Notepad")
!=
0) cout << "Notepad is open! " << endl; } ? MarkMark Salsbery Microsoft MVP - Visual C++ :java:
-
Ok so now im using GetWindowText() and it seems to capture all window titles. But i need it to look for a specific window, Notepad in this case. Here is my code:
#include #include using namespace std; int main(void) { HWND currenthwnd,hwnd; char s[1024]; hwnd=NULL; while (1){ Sleep(1000); currenthwnd=GetForegroundWindow(); if (currenthwnd!=hwnd){ hwnd=currenthwnd; GetWindowText(hwnd,s,1024); printf("%s\n",s); } if(strstr(s, "Notepad") == 0){ cout << "Notepad is open! " << endl; } else{ cout << "Its not open " << endl; } } return 0; }
Problem is when the program is running it prints Notepad is open when it really isnt. Can someone help me fix this? Thanx in advance!hi, dellthinker The string "Notepad" can be in any other window's title. strstr(s, "Notepad") == 0 doesn't mean any thing. Try this one, hope it will be helpful.
HWND hWnd = FindWindow("Notepad", NULL);
if (hWnd)
{
printf("Notepad is open! ");
}
else
{
printf("Its not open");
}============ Einstein Seeing is believing.
-
dellthinker wrote:
Problem is when the program is running it prints Notepad is open when it really isnt.
dellthinker wrote:
if(strstr(s, "Notepad") == 0)
Shouldn't that be if(strstr(s, "Notepad")
!=
0) cout << "Notepad is open! " << endl; } ? MarkMark Salsbery Microsoft MVP - Visual C++ :java:
Thanx Mark, your solution worked. :)