CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2003
    Location
    India
    Posts
    17

    Question Finding the HWND which has the focus

    When a user selects a file , I want to know which Window has the current focus .

    Just to explain, I am providing a scnearios.

    1) User selects abc.txt on the Desktop
    2) User selects xyz.doc in the Explorer.

    Though both files have the LVM_SELECTED flag set, I want to get the handle of the window in which the file has been selected.

    I tried with GetForegroundWindow, and GetGUIThreadInfo , but its not working.

    Any sort of help would be appreicated

    Sujay

  2. #2
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: Finding the HWND which has the focus

    is this what you seek?:
    Code:
    HWND hFocusWnd = GetFocus();
    GetFocus()

  3. #3
    Join Date
    Dec 2003
    Location
    India
    Posts
    17

    Re: Finding the HWND which has the focus

    I dont think it shall do.

    Intially I thought of this, but if you see selecting a file on the desktop , need the windows of the Listview of the destkop , not just the desktop.

    After that I want to compare the two desktop handles and accordingly let the user know.
    Last edited by sujayg; December 27th, 2009 at 10:44 AM. Reason: Updated info

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Finding the HWND which has the focus

    and GetGUIThreadInfo , but its not working
    "It's not working" explains not much. A code would do a lot better.
    Last edited by Igor Vartanov; December 27th, 2009 at 04:39 PM.
    Best regards,
    Igor

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Finding the HWND which has the focus

    Quote Originally Posted by sujayg View Post
    After that I want to compare the two desktop handles and accordingly let the user know.
    What are you hoping to let the user know?

  6. #6
    Join Date
    Dec 2003
    Location
    India
    Posts
    17

    Re: Finding the HWND which has the focus

    Quote Originally Posted by Arjay View Post
    What are you hoping to let the user know?
    Let me explain the situation . I think it would be better than the technical terms .

    1) User selects abc.txt on the Desktop
    2) User selects xyz.doc in the Explorer.

    If the user presses Ctrl+C, the file xyz.doc gets copied ONLY . That is Windows copies only the file which has the current focus .

    How does Windows knows which one has the current focus? I have do this programatically .

  7. #7
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    Re: Finding the HWND which has the focus

    Windows knows it because only one window has the focus at a time, and the window in focus gets the copy command and it places it's contents inside the clipboard, and second app gets it by pasting operation from clipboard.
    Regards,
    Ramkrishna Pawar

  8. #8
    Join Date
    Dec 2003
    Location
    India
    Posts
    17

    Re: Finding the HWND which has the focus

    You are right.

    For example, if one has to get the desktop HWND, one has to find the handle of the Syslistview32 within the Program Manager .

    In my case I want to compare the HWND's and see which HWND has the focus

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Finding the HWND which has the focus

    Quote Originally Posted by sujayg View Post
    Let me explain the situation . I think it would be better than the technical terms .

    1) User selects abc.txt on the Desktop
    2) User selects xyz.doc in the Explorer.

    If the user presses Ctrl+C, the file xyz.doc gets copied ONLY . That is Windows copies only the file which has the current focus .

    How does Windows knows which one has the current focus? I have do this programatically .
    Windows knows this because the listcontrol receives the CTRL+C message and the control itself knows which item(s) has been selected.

    If you tell us what you are actually trying to accomplish we may be able to suggest a solution.

  10. #10
    Join Date
    Dec 2003
    Location
    India
    Posts
    17

    Re: Finding the HWND which has the focus

    Hi Arjay,

    I want to find the path of the selected file.

    The user can select a file in the desktop and also on the explorer.

    But I want to get the path of the file which has the focus.

    Sujay
    Last edited by sujayg; December 28th, 2009 at 02:13 PM. Reason: Updated info

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Finding the HWND which has the focus

    The hwnd to the window in focus isn't going to help because the best you can do is get the hwnd to the listcontrol containing the selected file, but not the selected file itself.

    What you need to do is to register for a shell change event. This will tell you when items have been selected or deselected. See this link http://msdn.microsoft.com/en-us/libr...20(VS.85).aspx. At the bottom of the link there is another link to an sdk sample called "Change Notify Watcher Sample". Once you get the notification you should be able to retrieve the path of the selected item with IShellLink:GetPath.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured