Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleTrack the mouse's movement and display its position in a tooltip-like window
Description
Keywordsmouse, cursor, movement, track mouse
CategoriesTips and Tricks, API
 
Thanks to Parag Paithankar.

Thanks also to David for pointing out a memory leak resulting from not freeing the Device Context.

The program uses a timer to check the mouse's position. The Timer event handler uses the GetDC API function to get the desktop's device context (DC). It uses GetCursorPos to get the mouse's position and uses GetPixel to get the color of that point on the desktop's DC.

The program then displays the mouse's position and the color of the point under the mouse in another form positioned to look like a tooltip.

 
Private Sub Timer1_Timer()
    Dim dc, pnt As POINTAPI, str As String, colr
    Dim YesAll As Boolean
    Dim desktop_handle As Long
    
    If Enab = True Then
        desktop_handle = GetDesktopWindow()
        dc = GetWindowDC(desktop_handle)
        GetCursorPos pnt
        colr = GetPixel(dc, pnt.x, pnt.y)
        ReleaseDC desktop_handle, dc
        
        str = ""
        YesAll = True
        
        If Form1.Check1(1).Value = 1 Then
            str = "Ypos: " & Format(pnt.y, "0###")
            YesAll = False
        End If
        
        If Form1.Check1(0).Value = 1 Then
            If Form1.Check1(1).Value = 1 Then
                str = str & ", Xpos: " & Format(pnt.x, _
                    "0###")
            Else
                str = str & "Xpos: " & Format(pnt.x, "0###")
            End If
            
            YesAll = False
        End If
            
        If Form1.Check1(2).Value = 1 Then
            If Form1.Check1(1).Value = 1 Or _
                Form1.Check1(0).Value = 1 Then
                str = str & ", Color: " & Format(Hex(colr), _
                    "0#####") & Space(5)
            Else
                str = str & "Color: " & Format(Hex(colr), _
                    "0#####") & Space(5)
            End If
            
            YesAll = False
        End If
        
        If YesAll = True Then
            Form2.Visible = False
            Exit Sub
        Else
            Form2.Visible = True
        End If
        
        Form2.Label1 = str
        Form2.Width = Form2.Label1.Width + 50
        Form2.Left = (pnt.x * Screen.TwipsPerPixelX) + 100
        Form2.Top = (pnt.y * Screen.TwipsPerPixelY) + 100
    Else
        Form2.Visible = False
    End If
End Sub
 
This program uses subclassing so be sure you close it by right-clicking on its system tray icon, not by halting the program in the development environment. The program's tray menus also let you disable the program so it temporarily stops displaying the mouse's position.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated