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. Find the windows

Find the windows

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
11 Posts 2 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.
  • C Offline
    C Offline
    Cvaji
    wrote on last edited by
    #1

    Hello, I want to find the handle of same windows currently opened. E.g: I have started 2 notepads and I want to get handle of both notepads using some windows APIs. I tried FindWindow(). But its only return the first notepad window handle. Any other APIs available for getting the handle of second Window? Please help. -Cvaji

    C 1 Reply Last reply
    0
    • C Cvaji

      Hello, I want to find the handle of same windows currently opened. E.g: I have started 2 notepads and I want to get handle of both notepads using some windows APIs. I tried FindWindow(). But its only return the first notepad window handle. Any other APIs available for getting the handle of second Window? Please help. -Cvaji

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      Maybe try EnumWindows[^].

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

      C 1 Reply Last reply
      0
      • C Code o mat

        Maybe try EnumWindows[^].

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

        C Offline
        C Offline
        Cvaji
        wrote on last edited by
        #3

        Code-o-mat wrote:

        Maybe try EnumWindows[^].

        But this will enumerate all the top level windows right? I have the window name/window class of my required window. I need to get only that specific windows. -Cvaji

        C 1 Reply Last reply
        0
        • C Cvaji

          Code-o-mat wrote:

          Maybe try EnumWindows[^].

          But this will enumerate all the top level windows right? I have the window name/window class of my required window. I need to get only that specific windows. -Cvaji

          C Offline
          C Offline
          Code o mat
          wrote on last edited by
          #4

          You can implement the filtering in the callback function, you can get the window's "name" by calling GetWindowText[^] and the class name by calling GetClassName[^], althorough i am not sure that GetClassName works on windows in different processes... If you know the Process ID's of the notepad apps you started you can also use GetWindowthreadProcessId[^] to recognize them.

          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

          C 1 Reply Last reply
          0
          • C Code o mat

            You can implement the filtering in the callback function, you can get the window's "name" by calling GetWindowText[^] and the class name by calling GetClassName[^], althorough i am not sure that GetClassName works on windows in different processes... If you know the Process ID's of the notepad apps you started you can also use GetWindowthreadProcessId[^] to recognize them.

            > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

            C Offline
            C Offline
            Cvaji
            wrote on last edited by
            #5

            If there is no other simple way, i have to go with this option. Thank you for the reply.

            C 1 Reply Last reply
            0
            • C Cvaji

              If there is no other simple way, i have to go with this option. Thank you for the reply.

              C Offline
              C Offline
              Code o mat
              wrote on last edited by
              #6

              You could also try using FindWindow as you did, and once you find a window, do whatever you want with it and then add a space at the end of its name (title) and then use FindWindow again until it does not find any such windows anymore, you might want to remove the extra added space when you are done, of course this would be quite an unelegant and ugly solution...

              > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

              C 1 Reply Last reply
              0
              • C Code o mat

                You could also try using FindWindow as you did, and once you find a window, do whatever you want with it and then add a space at the end of its name (title) and then use FindWindow again until it does not find any such windows anymore, you might want to remove the extra added space when you are done, of course this would be quite an unelegant and ugly solution...

                > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

                C Offline
                C Offline
                Cvaji
                wrote on last edited by
                #7

                You mean the below code will return the handle for second notepad ? :confused: hWnd = ::FindWindow( 0, L"Untitled - Notepad " ); -Cvaji

                C 1 Reply Last reply
                0
                • C Cvaji

                  You mean the below code will return the handle for second notepad ? :confused: hWnd = ::FindWindow( 0, L"Untitled - Notepad " ); -Cvaji

                  C Offline
                  C Offline
                  Code o mat
                  wrote on last edited by
                  #8

                  No, i mean something like this:

                  HWND hWnd;
                  while ((hWnd = ::FindWindow(0, L"Untitled - Notepad")) != NULL)
                  {
                  //Do what you need with hWnd
                  ::SetWindowText(hWnd, L"Untitled - Notepad ");
                  }

                  Since you add a space at the end of the window text, the next time you call FindWindow, it will not find this window you already used (since it's title does not mach your search string anymore) and go on looking for the next one, if you find that next one, you add a space to that one too and so on untill you get no more windows...

                  > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

                  C 1 Reply Last reply
                  0
                  • C Code o mat

                    No, i mean something like this:

                    HWND hWnd;
                    while ((hWnd = ::FindWindow(0, L"Untitled - Notepad")) != NULL)
                    {
                    //Do what you need with hWnd
                    ::SetWindowText(hWnd, L"Untitled - Notepad ");
                    }

                    Since you add a space at the end of the window text, the next time you call FindWindow, it will not find this window you already used (since it's title does not mach your search string anymore) and go on looking for the next one, if you find that next one, you add a space to that one too and so on untill you get no more windows...

                    > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

                    C Offline
                    C Offline
                    Cvaji
                    wrote on last edited by
                    #9

                    Wow. Hats off you man :) even though its an ugly method, i congratulate your brilliancy :thumbsup: But I got another method.

                    HWND hWnd = ::FindWindow( L"Notepad", 0 );
                    hWnd = ::FindWindowEx( 0, hWnd, L"Notepad", 0 );

                    -Cvaji

                    C 1 Reply Last reply
                    0
                    • C Cvaji

                      Wow. Hats off you man :) even though its an ugly method, i congratulate your brilliancy :thumbsup: But I got another method.

                      HWND hWnd = ::FindWindow( L"Notepad", 0 );
                      hWnd = ::FindWindowEx( 0, hWnd, L"Notepad", 0 );

                      -Cvaji

                      C Offline
                      C Offline
                      Code o mat
                      wrote on last edited by
                      #10

                      Gee, thanks :) FindWindowEx, i better rememeber that for the next time :)

                      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

                      C 1 Reply Last reply
                      0
                      • C Code o mat

                        Gee, thanks :) FindWindowEx, i better rememeber that for the next time :)

                        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

                        C Offline
                        C Offline
                        Cvaji
                        wrote on last edited by
                        #11

                        If FindwindowEx was not provided by MS, i would go for your suggestion. :thumbsup:

                        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