Is there any API which gives the current focus area's coordinates?

Posted on 2004-09-06
Last Modified: 2010-08-11
I need get the coordinates of the current focus area in the screen.

For Example, in the desktop if a folder is selected ie., highlighted I need the coordinates of the highlighted folder

I don't want the coordinates of the mouse. I need the coordinates of the focus position.

There is a method GetCursorPos in user32.dll which gives the mouse coordinates.

Is there any API which gives the current focus area's  coordinates?
Question by:sangack
  • 6
  • 4

Expert Comment

ID: 11999780
The GetFocusedControlRect method of this class returns the rect of the focused control in the active window.

In the case of the desktop you have to work more.  remember that the desktop is a ListView Control and the listitems are threated as separate controls.

So you would have to use the SendMessage API to iterate through all items in the list view to find which one has the focus.

One you find the item you can use the LVM_GETITEMRECT to get the coordinates of the item.

Hope this help.

Public Class Functions
    Structure RECT
        Public left, top, right, bottom As Integer
    End Structure

        Public cbSize As Integer
        Public flags As Integer
        Public hwndActive As IntPtr
        Public hwndFocus As IntPtr
        Public hwndCapture As IntPtr
        Public hwndMenuOwner As IntPtr
        Public hwndMoveSize As IntPtr
        Public hwndCaret As IntPtr
        Public rcCaret As RECT
    End Structure

    Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr

    Declare Function GetWindowThreadProcessId Lib "user32.dll" ( _
            ByVal hwnd As IntPtr, ByRef lpdwProcessId As Int32) As Int32

    Declare Function GetFocus Lib "user32.dll" () As IntPtr

    Declare Function GetWindowRect Lib "user32.dll" ( _
        ByVal hwnd As IntPtr, ByRef lpRect As RECT) As Int32

    Declare Function GetGUIThreadInfo Lib "user32.dll" _
        (ByVal idThread As Integer, ByRef lpgui As GUITHREADINFO) As Boolean

    Public Shared Function GetFocusedControlRect() As Rectangle
        Dim _ThreadInfo As New GUITHREADINFO
        'Initialize structure size
        _ThreadInfo.cbSize = _

        Dim WndHandle As IntPtr
        Dim _SelectedPID As Int32
        Dim _rect As RECT

        'Get active window
        WndHandle = GetForegroundWindow()
        'Get GUI PID of the selected thread
        _SelectedPID = GetWindowThreadProcessId(WndHandle, 0)

        'Get thread info
        If GetGUIThreadInfo(_SelectedPID, _ThreadInfo) = False Then
        End If

        'Get window rectangle
        GetWindowRect(_ThreadInfo.hwndFocus, _rect)

        Return New System.Drawing.Rectangle(_rect.left,, _rect.right - _rect.left, _rect.bottom -
    End Function

End Class

Expert Comment

ID: 11999807
I mean to say

 listitems are not threated as separate controls.

Probable there is way to get focus item in the listview, but i don't know the way.

Iterate is the only way i could come up with.

Author Comment

ID: 12003482
Thanks a lot. But I am very sorry, I don't possess good programming knowledge. I tried your code in VB, but is this a VB code, coz it throws me too many errors. Can you help me out please.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 12006379
What are the errors you are getting?

what lines?

You have to add a reference to  System.Drawing.dll, ca you do this?

The method of the class is shared, so you don't need to instanciate the class.
 Public Shared Function GetFocusedControlRect() As Rectangle

Just call it like this:

dim MyRect as System.Drawing.Rectangle
MyRect = Functions.GetFocusedControlRect()

Now my MyRect has the x,y screen coordinates and also the width and height of the focused control.

Author Comment

ID: 12006775
Sorry we don't have .NET, but only Visual Studio 6.0

Can u help me on this using VB 6.0, please.

Expert Comment

ID: 12006870
No problem i will post the vb6 code , but the next time be aware on posting on the exact programming channel.

Author Comment

ID: 12013147

Waiting for your code

Accepted Solution

wguerram earned 200 total points
ID: 12016840
'Here is the code as i promised:
'I also put a messagebox that shows the name of the class of the selected window.
'for desktop is a SysListView32, so you have to check the occurance of ListView in the class name.

'If it is a listview you have to iterate throw the items until you find the one with the focus and get the rect of the item in order to obtain the coordinates.

'you will have to use SendMessage API to get the info from the ListView.

'This method will return 0,0 when you the desktop activated
'Add this code to a module
'Call the procedure like this in you form:

Private Sub Timer1_Timer()
   Dim x As Integer
   Dim y As Integer

   GetFocusedControlPos x, y
   Me.Caption = x & " " & y
End Sub
'I am using a timer just to see it work.
'Interval 5000 to let me select other windows
'the parameters are passed byref, after the call they will have the coordinates.

'Hope this help.

Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
    cbSize As Long
    flags As Long
    hwndActive As Long
    hwndFocus As Long
    hwndCapture As Long
    hwndMenuOwner As Long
    hwndMoveSize As Long
    hwndCaret As Long
    rcCaret As RECT
End Type

Declare Function GetForegroundWindow Lib "user32" () As Long
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Declare Function GetFocus Lib "user32" () As Long
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Private Declare Function GetGUIThreadInfo Lib "user32" (ByVal dwthreadid As Long, lpguithreadinfo As GUITHREADINFO) As Long

Public Sub GetFocusedControlPos(ByRef x As Integer, ByRef y As Integer)
   Dim WndHandle As Long
   Dim SelectedPID As Long
   Dim MyRect As RECT
   Dim lpClassName As String
   'Get active window
   WndHandle = GetForegroundWindow()
   'Get GUI PID of the selected thread
   SelectedPID = GetWindowThreadProcessId(WndHandle, 0)

   RetInfo.cbSize = LenB(RetInfo)
   If GetGUIThreadInfo(SelectedPID, RetInfo) = 0 Then
        MsgBox "Error while calling the function!"
        Exit Sub
    End If
     'Get window rectangle
   GetWindowRect RetInfo.hwndFocus, MyRect

    lpClassName = Space(256)
    'retrieve the class name
    RetVal = GetClassName(RetInfo.hwndFocus, lpClassName, 256)

    MsgBox lpClassName
   x = MyRect.Left
   y = MyRect.Top
End Sub

Author Comment

ID: 12023720
Good Work wguerram !!

It works great!!!

Sanjay Kumar

Can you mail me....ur email id.


Expert Comment

ID: 12026852

this my email


Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
seriesUp challenge 7 191
splitOdd10 challenge 5 125
base64 decode encode 12 161
Re-position sub-options beneath the TAB 7 106
If you haven’t already, I encourage you to read the first article ( in my series to gain a basic foundation of R and R Studio.  You will also find the …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question