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

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?
Who is Participating?
wguerramConnect With a Mentor Commented:
'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
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
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.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

sangackAuthor Commented:
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.
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.
sangackAuthor Commented:
Sorry we don't have .NET, but only Visual Studio 6.0

Can u help me on this using VB 6.0, please.
No problem i will post the vb6 code , but the next time be aware on posting on the exact programming channel.
sangackAuthor Commented:

Waiting for your code
sangackAuthor Commented:
Good Work wguerram !!

It works great!!!

Sanjay Kumar

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


this my email

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.