[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


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

Posted on 2004-09-06
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.top, _rect.right - _rect.left, _rect.bottom - _rect.top)
    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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


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 800 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

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.

Question has a verified solution.

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

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

656 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