Results 1 to 3 of 3

Thread: GetColor at CursorPoint

  1. #1

    Thread Starter
    Lively Member James Bond 007's Avatar
    Join Date
    May 2000
    Location
    London
    Posts
    116
    Anyone knows how to get the current color at the cursor point? And I don't mean just for my application but for desktop and other applications too. There must be an API somewhere for this.

    I am using p.set to get the current color for my application.
    License to Program

    007

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    There might be a better way but here's one way of doing it.
    You BitBlt the desktop to a borderless form that you size to the screen size.

    Then when you click on this form you use GetPixel to get the color.

    To try this example start a new standard EXE project and add a CommandButton the Form1. Then add the following code:
    Code:
    Private Sub Command1_Click()
        Form2.Show
    End Sub
    Now add a new form and set the BorderStyle to 0 - None and the AutoRedraw property to True. Also set the ScaleMode to 3 - Pixel.

    Now add the following code to Form2
    Code:
    Private Type POINTAPI
        x As Long
        y As Long
    End Type
    
    Private Declare Function GetPixel _
     Lib "gdi32" ( _
     ByVal hDC As Long, _
     ByVal x As Long, _
     ByVal y As Long) As Long
    
    Private Declare Function GetDesktopWindow _
     Lib "user32" () As Long
    
    Private Declare Function GetCursorPos _
     Lib "user32" ( _
     lpPoint As POINTAPI) As Long
    
    Private Declare Function GetDC _
     Lib "user32" ( _
     ByVal hwnd As Long) As Long
    
    Private Declare Function ReleaseDC _
     Lib "user32" ( _
     ByVal hwnd As Long, _
     ByVal hDC As Long) As Long
    
    Private Declare Function BitBlt _
     Lib "gdi32" ( _
     ByVal hDestDC As Long, _
     ByVal x As Long, _
     ByVal y As Long, _
     ByVal nWidth As Long, _
     ByVal nHeight As Long, _
     ByVal hSrcDC As Long, _
     ByVal xSrc As Long, _
     ByVal ySrc As Long, _
     ByVal dwRop As Long) As Long
    
    Private Const SRCCOPY = &HCC0020
    
    Private Sub Form_Click()
        Dim p As POINTAPI
        GetCursorPos p
        Form1.BackColor = GetPixel(Me.hDC, p.x, p.y)
        Unload Me
    End Sub
    
    Private Sub Form_Load()
        Dim hwnd As Long
        Dim hDC As Long
        Dim lngColor As Long
            
        Me.MousePointer = vbUpArrow
        Me.Move 0, 0, Screen.Width, Screen.Height
        hwnd = GetDesktopWindow
        hDC = GetDC(hwnd)
        BitBlt Me.hDC, 0&, 0&, Form2.ScaleWidth, Form2.ScaleHeight, hDC, 0&, 0&, SRCCOPY
        Me.Refresh
        ReleaseDC hwnd, hDC
    End Sub
    Try it out...

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Put this in a module and call coloratcursor and it will return the color at the cursor point
    Code:
    Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function GetDesktopWindow Lib "user32" () As Long
    Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
    Type POINTAPI
            X As Long
            Y As Long
    End Type
    
    Function coloratcursor()
    Dim pa As POINTAPI
    GetCursorPos pa
    coloratcursor = GetPixel(GetWindowDC(GetDesktopWindow), pa.X, pa.Y)
    End Function
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width