Solved

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

Posted on 2004-09-06
10
412 Views
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?
0
Comment
Question by:sangack
  • 6
  • 4
10 Comments
 
LVL 8

Expert Comment

by:wguerram
Comment Utility
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

    Structure GUITHREADINFO
        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 = _
            System.Runtime.InteropServices.Marshal.SizeOf(GetType(GUITHREADINFO))

        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
            MsgBox(System.Runtime.InteropServices.Marshal.GetLastWin32Error.ToString)
        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
0
 
LVL 8

Expert Comment

by:wguerram
Comment Utility
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.
0
 

Author Comment

by:sangack
Comment Utility
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.
0
 
LVL 8

Expert Comment

by:wguerram
Comment Utility
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.
0
 

Author Comment

by:sangack
Comment Utility
Sorry we don't have .NET, but only Visual Studio 6.0

Can u help me on this using VB 6.0, please.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 8

Expert Comment

by:wguerram
Comment Utility
No problem i will post the vb6 code , but the next time be aware on posting on the exact programming channel.
0
 

Author Comment

by:sangack
Comment Utility
Thanks

Waiting for your code
0
 
LVL 8

Accepted Solution

by:
wguerram earned 200 total points
Comment Utility
'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
Private Type GUITHREADINFO
    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 RetInfo As GUITHREADINFO
   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
0
 

Author Comment

by:sangack
Comment Utility
Good Work wguerram !!

It works great!!!

Sanjay Kumar
sanjaykumar.b@gmail.com

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

0
 
LVL 8

Expert Comment

by:wguerram
Comment Utility
Great!

this my email
wguerram@hotmail.com

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now