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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Find control on dialog

Find control on dialog

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++comtools
11 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.
  • G Offline
    G Offline
    Giorgi Dalakishvili
    wrote on last edited by
    #1

    Hello, Let's say I have a window created bu external application and I know handle to that window. I need to find control on the dialog with specified class name. So, I know the handle of the dialog which contains the control and I know class name of the control. How can I find it? One possible way I'm thinking of is enumerating all controls on the form using GetNextWindow and checking control's class using GetClassName function. Am I on the right track? Is there any better/easier way to do it? Sorry if my question is stupid, I'm not experienced in Visual c++ Thanks.

    Giorgi Dalakishvili #region signature my articles #endregion

    D F R 3 Replies Last reply
    0
    • G Giorgi Dalakishvili

      Hello, Let's say I have a window created bu external application and I know handle to that window. I need to find control on the dialog with specified class name. So, I know the handle of the dialog which contains the control and I know class name of the control. How can I find it? One possible way I'm thinking of is enumerating all controls on the form using GetNextWindow and checking control's class using GetClassName function. Am I on the right track? Is there any better/easier way to do it? Sorry if my question is stupid, I'm not experienced in Visual c++ Thanks.

      Giorgi Dalakishvili #region signature my articles #endregion

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

      How about FindWindowEx()?

      "Love people and use things, not love things and use people." - Unknown

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      G 1 Reply Last reply
      0
      • D David Crow

        How about FindWindowEx()?

        "Love people and use things, not love things and use people." - Unknown

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        G Offline
        G Offline
        Giorgi Dalakishvili
        wrote on last edited by
        #3

        Thanks, I'll try it. As I have practically no experience in win api, I was not sure whether it would find control or not. Thanks again.

        Giorgi Dalakishvili #region signature my articles #endregion

        1 Reply Last reply
        0
        • G Giorgi Dalakishvili

          Hello, Let's say I have a window created bu external application and I know handle to that window. I need to find control on the dialog with specified class name. So, I know the handle of the dialog which contains the control and I know class name of the control. How can I find it? One possible way I'm thinking of is enumerating all controls on the form using GetNextWindow and checking control's class using GetClassName function. Am I on the right track? Is there any better/easier way to do it? Sorry if my question is stupid, I'm not experienced in Visual c++ Thanks.

          Giorgi Dalakishvili #region signature my articles #endregion

          F Offline
          F Offline
          Force Code
          wrote on last edited by
          #4

          Giorgi Dalakishvili wrote:

          One possible way I'm thinking of is enumerating all controls on the form using GetNextWindow and checking control's class using GetClassName function.

          I would use EnumChildWindows and check for the ctrlID for each Child Window using GetDlgCtrlID. The Ctrl ID will be unique and won't change.

          Giorgi Dalakishvili wrote:

          I'm not experienced in Visual c++

          Well, it seems you already knew about EnumChildWindows and even how to invoke it from C#: http://www.codeproject.com/script/Forums/View.aspx?fid=1649&msg=2558300[^]

          G 1 Reply Last reply
          0
          • F Force Code

            Giorgi Dalakishvili wrote:

            One possible way I'm thinking of is enumerating all controls on the form using GetNextWindow and checking control's class using GetClassName function.

            I would use EnumChildWindows and check for the ctrlID for each Child Window using GetDlgCtrlID. The Ctrl ID will be unique and won't change.

            Giorgi Dalakishvili wrote:

            I'm not experienced in Visual c++

            Well, it seems you already knew about EnumChildWindows and even how to invoke it from C#: http://www.codeproject.com/script/Forums/View.aspx?fid=1649&msg=2558300[^]

            G Offline
            G Offline
            Giorgi Dalakishvili
            wrote on last edited by
            #5

            Force Code wrote:

            I would use EnumChildWindows and check for the ctrlID for each Child Window using GetDlgCtrlID. The Ctrl ID will be unique and won't change.

            GetDlgCtrlID returns int, how would you check for class name?

            Giorgi Dalakishvili #region signature my articles #endregion

            F 1 Reply Last reply
            0
            • G Giorgi Dalakishvili

              Force Code wrote:

              I would use EnumChildWindows and check for the ctrlID for each Child Window using GetDlgCtrlID. The Ctrl ID will be unique and won't change.

              GetDlgCtrlID returns int, how would you check for class name?

              Giorgi Dalakishvili #region signature my articles #endregion

              F Offline
              F Offline
              Force Code
              wrote on last edited by
              #6

              Giorgi Dalakishvili wrote:

              GetDlgCtrlID returns int, how would you check for class name?

              I assumed you were just trying to locate a specific control, and using the classname for that purpose. If you're just looking for any control of a certain class, then of course you can use GetClassName. Why would you think to use GetNextWindow as opposed to EnumChildWindows?

              G 1 Reply Last reply
              0
              • F Force Code

                Giorgi Dalakishvili wrote:

                GetDlgCtrlID returns int, how would you check for class name?

                I assumed you were just trying to locate a specific control, and using the classname for that purpose. If you're just looking for any control of a certain class, then of course you can use GetClassName. Why would you think to use GetNextWindow as opposed to EnumChildWindows?

                G Offline
                G Offline
                Giorgi Dalakishvili
                wrote on last edited by
                #7

                Yes, I'm trying to locate a specific control but I won't know its id in advance, I'll only know its class name.

                Force Code wrote:

                Why would you think to use GetNextWindow as opposed to EnumChildWindows?

                I just forgot about EnumChildWindows. Should I prefer it to GetNextWindow? Is there any significant difference between them?

                Giorgi Dalakishvili #region signature my articles #endregion

                F R 2 Replies Last reply
                0
                • G Giorgi Dalakishvili

                  Yes, I'm trying to locate a specific control but I won't know its id in advance, I'll only know its class name.

                  Force Code wrote:

                  Why would you think to use GetNextWindow as opposed to EnumChildWindows?

                  I just forgot about EnumChildWindows. Should I prefer it to GetNextWindow? Is there any significant difference between them?

                  Giorgi Dalakishvili #region signature my articles #endregion

                  F Offline
                  F Offline
                  Force Code
                  wrote on last edited by
                  #8

                  Giorgi Dalakishvili wrote:

                  Yes, I'm trying to locate a specific control but I won't know its id in advance, I'll only know its class name.

                  You can get the control ID with Spy++, or I imagine lots of other utilities as well. If you know enough about the application, then I guess the classname would suffice (also a unique control ID is just convention, and not something that's enforced.)

                  Giorgi Dalakishvili wrote:

                  I just forgot about EnumChildWindows. Should I prefer it to GetNextWindow? Is there any significant difference between them?

                  You mentioned it three days ago in the C# thread, and even supplied the msdn link. AS far as differences, to me its just more compact than calling GetWindow or GetNextWindow in a loop. Also, the documentation says, "[EnumChildWindows] is more reliable than calling the GetWindow function in a loop. An application that calls GetWindow to perform this task risks being caught in an infinite loop or referencing a handle to a window that has been destroyed. " EnumChildWindows does actually return all descendants of a given window and not just immediate children.

                  1 Reply Last reply
                  0
                  • G Giorgi Dalakishvili

                    Hello, Let's say I have a window created bu external application and I know handle to that window. I need to find control on the dialog with specified class name. So, I know the handle of the dialog which contains the control and I know class name of the control. How can I find it? One possible way I'm thinking of is enumerating all controls on the form using GetNextWindow and checking control's class using GetClassName function. Am I on the right track? Is there any better/easier way to do it? Sorry if my question is stupid, I'm not experienced in Visual c++ Thanks.

                    Giorgi Dalakishvili #region signature my articles #endregion

                    R Offline
                    R Offline
                    Rajesh R Subramanian
                    wrote on last edited by
                    #9

                    Hi Giorgi, I chose the Start-->>Run dialog. I then found out the handle of the Run dialog window with Spy++ (You said that you will know it in your case). I am trying to find the OK button with code, using FindWindowEx(). And this small program works for me:

                    #include "stdafx.h"
                    #include "CPConsole.h"

                    #ifdef _DEBUG
                    #define new DEBUG_NEW
                    #endif

                    CWinApp theApp;

                    using namespace std;

                    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
                    {
                    int nRetCode = 0;

                    // initialize MFC and print and error on failure
                    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
                    {
                    	// TODO: change error code to suit your needs
                    	\_tprintf(\_T("Fatal Error: MFC initialization failed\\n"));
                    	nRetCode = 1;
                    }
                    else
                    {
                    	HWND hTarget = FindWindowEx((HWND)0x010403CE, //Hard coded handle value 
                                                          NULL, //Let the search begin with the first child
                                                          \_T("Button"), //Class name
                                                          \_T("OK")); //Window title
                    	if(hTarget)
                    		AfxMessageBox(\_T("OK button was found"));
                    	else
                    		AfxMessageBox(\_T("Unable to find the OK button"));
                    }
                    return nRetCode;
                    

                    }

                    Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                    G 1 Reply Last reply
                    0
                    • R Rajesh R Subramanian

                      Hi Giorgi, I chose the Start-->>Run dialog. I then found out the handle of the Run dialog window with Spy++ (You said that you will know it in your case). I am trying to find the OK button with code, using FindWindowEx(). And this small program works for me:

                      #include "stdafx.h"
                      #include "CPConsole.h"

                      #ifdef _DEBUG
                      #define new DEBUG_NEW
                      #endif

                      CWinApp theApp;

                      using namespace std;

                      int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
                      {
                      int nRetCode = 0;

                      // initialize MFC and print and error on failure
                      if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
                      {
                      	// TODO: change error code to suit your needs
                      	\_tprintf(\_T("Fatal Error: MFC initialization failed\\n"));
                      	nRetCode = 1;
                      }
                      else
                      {
                      	HWND hTarget = FindWindowEx((HWND)0x010403CE, //Hard coded handle value 
                                                            NULL, //Let the search begin with the first child
                                                            \_T("Button"), //Class name
                                                            \_T("OK")); //Window title
                      	if(hTarget)
                      		AfxMessageBox(\_T("OK button was found"));
                      	else
                      		AfxMessageBox(\_T("Unable to find the OK button"));
                      }
                      return nRetCode;
                      

                      }

                      Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                      G Offline
                      G Offline
                      Giorgi Dalakishvili
                      wrote on last edited by
                      #10

                      Thanks :)

                      Giorgi Dalakishvili #region signature my articles #endregion

                      1 Reply Last reply
                      0
                      • G Giorgi Dalakishvili

                        Yes, I'm trying to locate a specific control but I won't know its id in advance, I'll only know its class name.

                        Force Code wrote:

                        Why would you think to use GetNextWindow as opposed to EnumChildWindows?

                        I just forgot about EnumChildWindows. Should I prefer it to GetNextWindow? Is there any significant difference between them?

                        Giorgi Dalakishvili #region signature my articles #endregion

                        R Offline
                        R Offline
                        Rajesh R Subramanian
                        wrote on last edited by
                        #11

                        Giorgi Dalakishvili wrote:

                        I just forgot about EnumChildWindows. Should I prefer it to GetNextWindow? Is there any significant difference between them?

                        In this case, FindWindowEx() should be best for you. Because, you just don't need an enumerated list of all the child windows of the dialog. FindWindowsEx() will just return a handle which pinpoints at the control of your interest. :)

                        Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                        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