Solved

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

Posted on 2004-09-06
10
455 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
[X]
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
10 Comments
 
LVL 8

Expert Comment

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

    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
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.
0
 

Author Comment

by:sangack
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.
0
Industry Leaders: 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!

 
LVL 8

Expert Comment

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

Author Comment

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

Expert Comment

by:wguerram
ID: 12006870
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
ID: 12013147
Thanks

Waiting for your code
0
 
LVL 8

Accepted Solution

by:
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
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
ID: 12023720
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
ID: 12026852
Great!

this my email
wguerram@hotmail.com

0

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

Suggested Solutions

Title # Comments Views Activity
countTriple  challenge 8 88
static class 3 86
FizzBuzz challenge 9 87
MaxSpan challenge 9 137
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.
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

738 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