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. Retrieve HWND of a control that has focus

Retrieve HWND of a control that has focus

Scheduled Pinned Locked Moved C / C++ / MFC
questionvisual-studiocom
12 Posts 4 Posters 1 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.
  • _ _Flaviu

    Hi. How can I retrieve the HWND of a control inside CDialog, and this control has the focus ? To get the next HWND control I can use GetNextWindow[^], but I can retrieve the HWND of the next or previous control that has the focus ... In fact, I wish to use (while I am in CDialog):

    HWND hWnd = ::FindWindow(NULL, _T("Some dialog"));
    if(NULL != hWnd)
    {
    ::SetWindowText(GetFocus()->GetSafeHwnd(), sText);
    }

    but without GetFocus ...

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

    _Flaviu wrote:

    How can I retrieve the HWND of a control inside CDialog, and this control has the focus ?

    Does the control have a member variable associated with it?

    _Flaviu wrote:

    but without GetFocus ...

    What is your aversion to using GetFocus()?

    "One man's wage rise is another man's price increase." - Harold Wilson

    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

    "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

    _ 3 Replies Last reply
    0
    • D David Crow

      _Flaviu wrote:

      How can I retrieve the HWND of a control inside CDialog, and this control has the focus ?

      Does the control have a member variable associated with it?

      _Flaviu wrote:

      but without GetFocus ...

      What is your aversion to using GetFocus()?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      _ Offline
      _ Offline
      _Flaviu
      wrote on last edited by
      #3

      No. In fact, it a CDialog which belong to another app.

      1 Reply Last reply
      0
      • D David Crow

        _Flaviu wrote:

        How can I retrieve the HWND of a control inside CDialog, and this control has the focus ?

        Does the control have a member variable associated with it?

        _Flaviu wrote:

        but without GetFocus ...

        What is your aversion to using GetFocus()?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        _ Offline
        _ Offline
        _Flaviu
        wrote on last edited by
        #4

        I have found a little workaround, I don't know if is ok:

        	HWND hWndChild = ::GetNextWindow(hwnd, GW\_HWNDNEXT);
        	hWndChild = ::GetNextWindow(hWndChild, GW\_HWNDPREV);
        	::SetWindowText(hWndChild, sText);
        
        1 Reply Last reply
        0
        • D David Crow

          _Flaviu wrote:

          How can I retrieve the HWND of a control inside CDialog, and this control has the focus ?

          Does the control have a member variable associated with it?

          _Flaviu wrote:

          but without GetFocus ...

          What is your aversion to using GetFocus()?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          _ Offline
          _ Offline
          _Flaviu
          wrote on last edited by
          #5

          "What is your aversion to using GetFocus()?" Because this dialog is not belong to my app, and if there is another window in foreground, this GetFocus() will not work.

          D 1 Reply Last reply
          0
          • _ _Flaviu

            "What is your aversion to using GetFocus()?" Because this dialog is not belong to my app, and if there is another window in foreground, this GetFocus() will not work.

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

            Understood. Does this thread even remotely talk about what it is that you are attempting to do?

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            1 Reply Last reply
            0
            • _ _Flaviu

              Hi. How can I retrieve the HWND of a control inside CDialog, and this control has the focus ? To get the next HWND control I can use GetNextWindow[^], but I can retrieve the HWND of the next or previous control that has the focus ... In fact, I wish to use (while I am in CDialog):

              HWND hWnd = ::FindWindow(NULL, _T("Some dialog"));
              if(NULL != hWnd)
              {
              ::SetWindowText(GetFocus()->GetSafeHwnd(), sText);
              }

              but without GetFocus ...

              L Offline
              L Offline
              leon de boer
              wrote on last edited by
              #7

              GetForegroundWindow and GetActiveWindow are the goto functions. GetActiveWindow give you the active window of your app if it was activated and topmost. GetForegroundWindow function is the special function specifically designed for obtaining windows from other processes. It is the windows system topmost window, so if a window has focus it will be that window. All windows recieving focus get WM_SETFOCUS messages and those losing it receive WM_KILLFOCUS messages which is the other way to track focus changes.

              In vino veritas

              _ 1 Reply Last reply
              0
              • L leon de boer

                GetForegroundWindow and GetActiveWindow are the goto functions. GetActiveWindow give you the active window of your app if it was activated and topmost. GetForegroundWindow function is the special function specifically designed for obtaining windows from other processes. It is the windows system topmost window, so if a window has focus it will be that window. All windows recieving focus get WM_SETFOCUS messages and those losing it receive WM_KILLFOCUS messages which is the other way to track focus changes.

                In vino veritas

                _ Offline
                _ Offline
                _Flaviu
                wrote on last edited by
                #8

                GetForegroundWindow and GetActiveWindow are doing the job when this dialog has the focus ... but in my case this dialog is very possible to haven't focus, and

                ::SetWindowText(GetFocus()->GetSafeHwnd(), sText); // this edit control is gaining the mouse cursor

                is not working ... and I guess GetForegroundWindow and GetActiveWindow are useless ...

                L L 2 Replies Last reply
                0
                • _ _Flaviu

                  GetForegroundWindow and GetActiveWindow are doing the job when this dialog has the focus ... but in my case this dialog is very possible to haven't focus, and

                  ::SetWindowText(GetFocus()->GetSafeHwnd(), sText); // this edit control is gaining the mouse cursor

                  is not working ... and I guess GetForegroundWindow and GetActiveWindow are useless ...

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #9

                  You would be better using EnumWindows function | Microsoft Docs[^] and using the text of the Window to identify it.

                  _ 1 Reply Last reply
                  0
                  • _ _Flaviu

                    GetForegroundWindow and GetActiveWindow are doing the job when this dialog has the focus ... but in my case this dialog is very possible to haven't focus, and

                    ::SetWindowText(GetFocus()->GetSafeHwnd(), sText); // this edit control is gaining the mouse cursor

                    is not working ... and I guess GetForegroundWindow and GetActiveWindow are useless ...

                    L Offline
                    L Offline
                    leon de boer
                    wrote on last edited by
                    #10

                    GetForegroundWindow doesn't give a rats if your dialog has focus or not. At a guess you have the dialog in modal mode and the message queue is shutdown and your code isn't executing when it isn't focused :-) https://msdn.microsoft.com/en-us/library/windows/desktop/ms633505(v=vs.85).aspx[^] It is quite specific it's the window that's currently getting input regardless of its relationship to the calling thread (aka your little APP). It has two choices the focus window (or the windows that contains the focus window depends on class) or null if the desktop itself has focus. If you doubt it download and use spy++ and click on the focus window with the mouse to get its details Introducing Spy++ - Visual Studio | Microsoft Docs[^] You should also be able to confirm it with GetLastActivePopup. GetLastActivePopup function (Windows)[^] The call they have closed down is its sister call SetForegroundWindow() to steal focus from another process (unless that process has explicitly given permission via AllowSetForegroundWindow) because lots of web browser popups were abusing it.

                    In vino veritas

                    1 Reply Last reply
                    0
                    • L Lost User

                      You would be better using EnumWindows function | Microsoft Docs[^] and using the text of the Window to identify it.

                      _ Offline
                      _ Offline
                      _Flaviu
                      wrote on last edited by
                      #11

                      I have tried:

                      HWND hWnd = ::FindWindow(NULL, \_T("Dialog Test"));
                      if(NULL != hWnd) // the dialog is founded
                      {
                      	CString sTemp;
                      	hWnd = ::GetTopWindow(hWnd);
                      	while(NULL != hWnd)
                      	{
                      		hWnd = ::GetNextWindow(hWnd, GW\_HWNDNEXT);
                      		int nID = ::GetDlgCtrlID(hWnd);
                      		if(1001 == nID) // the edit box has been founded
                      		{
                      			::SetWindowText(hWnd, \_T("ABC ABC"));
                      			sTemp.Format(\_T("%d"), nID);
                      			MessageBox(sTemp);
                      		}
                      	}
                      }
                      

                      but ::SetWindowText simply not set the text (is not changing the text) ... strange ...

                      L 1 Reply Last reply
                      0
                      • _ _Flaviu

                        I have tried:

                        HWND hWnd = ::FindWindow(NULL, \_T("Dialog Test"));
                        if(NULL != hWnd) // the dialog is founded
                        {
                        	CString sTemp;
                        	hWnd = ::GetTopWindow(hWnd);
                        	while(NULL != hWnd)
                        	{
                        		hWnd = ::GetNextWindow(hWnd, GW\_HWNDNEXT);
                        		int nID = ::GetDlgCtrlID(hWnd);
                        		if(1001 == nID) // the edit box has been founded
                        		{
                        			::SetWindowText(hWnd, \_T("ABC ABC"));
                        			sTemp.Format(\_T("%d"), nID);
                        			MessageBox(sTemp);
                        		}
                        	}
                        }
                        

                        but ::SetWindowText simply not set the text (is not changing the text) ... strange ...

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #12

                        Some comments: 1.) If you are not the author of the target process then from a purely conceptual point of view you are attacking the process. The [modern UWP applications](https://docs.microsoft.com/en-us/windows/uwp/get-started/universal-application-platform-guide) will limit these types of cross process interactions. You should check if the target process supports [Active Accessibility, UI Automation, or IAccessibleEx](https://docs.microsoft.com/en-us/windows/desktop/WinAuto/microsoft-active-accessibility-and-ui-automation-compared). 2.) If you need to send input such as WM_CHAR to a window owned by another process you may need to use the [AttachThreadInput function](https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-attachthreadinput) before sending the WM_CHAR or other input messages. 3.) The [SetWindowText function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633546(v=vs.85).aspx) can only be used in the current process. It cannot be used to set the text in a window owned by another process. 4.) To set the text of a window in another process you will need to send the WM_SETTEXT message directly. 5.) The [SendMessage function](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644950(v=vs.85).aspx) can cause your program to hang. Use [SendMessageTimeout](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644952(v=vs.85).aspx) instead. 6.) You will need to make sure that the process setting the text is of greater or equal [integrity levels](https://msdn.microsoft.com/en-us/library/bb625963.aspx?f=255). For example... a process running at medium level cannot set the text in a window owned by a process running at high integrity level. Best Wishes, -David Delaune P.S. The solution to your problem is in bold.

                        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