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. Which window has the focus [modified]

Which window has the focus [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 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.
  • S Offline
    S Offline
    SujayG
    wrote on last edited by
    #1

    I have to return the path of the selected file , EITHER in the Desktop or in the Windows Explorer. I can get the path of the selected file both on the Windows Explorer and the desktop My problem is that I am unable to find which window has the current focus 1) The user has selected a file on the desktop 2) The user has selected a file in the Windows explorer. ( So this has the focus now ) How do I programatically find that out I am using the following code HWND hWndDesktop = GetDesktopHandle(); // Gets the handle of the SyslistView32 window DWORD dwThread = ::GetWindowThreadProcessId(hWndDesktop, NULL); GUITHREADINFO gtf ; gtf.cbSize = sizeof(GUITHREADINFO); BOOL b = GetGUIThreadInfo(dwThread, >f); if(hWndDesktop == gtf.hwndFocus) { // this window currently has the focus } But I am getting different handles for hWndDesktop and gtf.hwndFocus . Please let me know if I am missing something EDIT I am trying to get the Window handle across processes . Thanks Sujay

    modified on Friday, December 4, 2009 12:40 AM

    _ V 2 Replies Last reply
    0
    • S SujayG

      I have to return the path of the selected file , EITHER in the Desktop or in the Windows Explorer. I can get the path of the selected file both on the Windows Explorer and the desktop My problem is that I am unable to find which window has the current focus 1) The user has selected a file on the desktop 2) The user has selected a file in the Windows explorer. ( So this has the focus now ) How do I programatically find that out I am using the following code HWND hWndDesktop = GetDesktopHandle(); // Gets the handle of the SyslistView32 window DWORD dwThread = ::GetWindowThreadProcessId(hWndDesktop, NULL); GUITHREADINFO gtf ; gtf.cbSize = sizeof(GUITHREADINFO); BOOL b = GetGUIThreadInfo(dwThread, >f); if(hWndDesktop == gtf.hwndFocus) { // this window currently has the focus } But I am getting different handles for hWndDesktop and gtf.hwndFocus . Please let me know if I am missing something EDIT I am trying to get the Window handle across processes . Thanks Sujay

      modified on Friday, December 4, 2009 12:40 AM

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Use GetFocus to get the handle to the window that has the focus.

      «_Superman_» I love work. It gives me something to do between weekends.
      Microsoft MVP (Visual C++)

      S M 2 Replies Last reply
      0
      • _ _Superman_

        Use GetFocus to get the handle to the window that has the focus.

        «_Superman_» I love work. It gives me something to do between weekends.
        Microsoft MVP (Visual C++)

        S Offline
        S Offline
        SujayG
        wrote on last edited by
        #3

        HI , GetFocus does return me the correct window handle also. I had tried with GetFoucs,GetActiveWindow, none of them are working

        1 Reply Last reply
        0
        • _ _Superman_

          Use GetFocus to get the handle to the window that has the focus.

          «_Superman_» I love work. It gives me something to do between weekends.
          Microsoft MVP (Visual C++)

          M Offline
          M Offline
          Madhu Nair 0
          wrote on last edited by
          #4

          Superman, Does the GetTopWindow help in this regard ? Or, What about using File Change Notification[^] to get the changes user made in file system ? Not sure these are relevant for the OP.

          modified on Friday, December 4, 2009 1:37 AM

          1 Reply Last reply
          0
          • S SujayG

            I have to return the path of the selected file , EITHER in the Desktop or in the Windows Explorer. I can get the path of the selected file both on the Windows Explorer and the desktop My problem is that I am unable to find which window has the current focus 1) The user has selected a file on the desktop 2) The user has selected a file in the Windows explorer. ( So this has the focus now ) How do I programatically find that out I am using the following code HWND hWndDesktop = GetDesktopHandle(); // Gets the handle of the SyslistView32 window DWORD dwThread = ::GetWindowThreadProcessId(hWndDesktop, NULL); GUITHREADINFO gtf ; gtf.cbSize = sizeof(GUITHREADINFO); BOOL b = GetGUIThreadInfo(dwThread, >f); if(hWndDesktop == gtf.hwndFocus) { // this window currently has the focus } But I am getting different handles for hWndDesktop and gtf.hwndFocus . Please let me know if I am missing something EDIT I am trying to get the Window handle across processes . Thanks Sujay

            modified on Friday, December 4, 2009 12:40 AM

            V Offline
            V Offline
            vasu_sri
            wrote on last edited by
            #5

            i have tried what u have done . that GetFocus function is retirning is NULL when iam calling in thread. so, that i tried one more function GetForegroundWindow(); this also returns HWND.

            CWnd \*wnd=NULL;
            HWND hWnd = NULL;
            CString strwndName;
            
            while (1)
            {
            	hWnd=::GetFocus();
            	if(hWnd==NULL)
            		hWnd=GetForegroundWindow();
            	GetWindowText(hWnd,strwndName.GetBuffer(),20);
            	//displaying in edit box.
            	dlg->m\_ed1.SetWindowText(strwndName);
            }
            

            just try it.. it will be work...

            Regards, Srinivas

            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