Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. GetForegroundWindow()

GetForegroundWindow()

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dellthinker
    wrote on last edited by
    #1

    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!

    D 1 Reply Last reply
    0
    • D dellthinker

      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!

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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 to strcmp() 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, checkout FindWindow().

      "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

      D 1 Reply Last reply
      0
      • D David Crow

        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 to strcmp() 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, checkout FindWindow().

        "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

        D Offline
        D Offline
        dellthinker
        wrote on last edited by
        #3

        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!

        M F 2 Replies Last reply
        0
        • D dellthinker

          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!

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          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; } ? Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          D 1 Reply Last reply
          0
          • D dellthinker

            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!

            F Offline
            F Offline
            fd0129002
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • M Mark Salsbery

              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; } ? Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              D Offline
              D Offline
              dellthinker
              wrote on last edited by
              #6

              Thanx Mark, your solution worked. :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups