Results 1 to 6 of 6

Thread: Color under cursor ???

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2001
    Location
    Yugoslavia
    Posts
    7

    Color under cursor ???

    How can I know who color is under cursor (mouse) ???


    Thanx !
    Ko Zna Zna Ko Nezna Naucice

  2. #2
    NOMADMAN
    Guest
    Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long

    Simple as that... Just add this API to your project and call with the appropraite varibles to recieve your RGB in long form.

    VB Code:
    1. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    2.     Label1.Caption = GetPixel(Form1.hdc, X / 15, Y / 15)
    3. End Sub

    The 15 is the twips per pixel on my form. Screen.TwipsPerPixelX (Y) ...

    Hope that this helps

    NOMAD

  3. #3
    NOMADMAN
    Guest
    If you don't want to "bother" with API you can just use the point function for Picture box or Form... This will only work when you are over a form or picture box... I think, I haven't tested it just remember looking into it.

    NOMAD

  4. #4
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    To get it for the entirety of the screen, use this snippet that I've written.
    VB Code:
    1. Private Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (lpPoint As POINTAPI) As Long
    2. Private Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Long) As Long
    3. Private Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Long
    4. Private Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    5. Private Type POINTAPI
    6.         x As Long
    7.         y As Long
    8. End Type
    9.  
    10. Function ColourUnderCursor() As Long
    11. Dim lPoint As POINTAPI
    12.     GetCursorPos lPoint
    13.     ColourUnderCursor = GetPixel(GetDc(GetDesktopWindow), lPoint.x, lPoint.y)
    14. End Function
    It requires the declaration of GetCursorPos, GetDesktopWindow, GetDc, and GetPixel. It also requires the PointAPI Type, all of which are included above the function.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2001
    Location
    Yugoslavia
    Posts
    7

    Thanx

    OK thanx fo this !! but i have a prblem !
    I have 2 Picture box ( Picture1 and Picture 2) ..I need when i move mouse in Picture1 ...then Picture2.BackColor = ColourUnderCursor !!!!


    How can I do this .... Thanx
    Ko Zna Zna Ko Nezna Naucice

  6. #6
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Make a timer control and set it's refresh to, say, 50ms (the Interval property). In the timer subroutine, add this code:
    VB Code:
    1. Picture2.BackColor = ColourUnderCursor
    It's as simple as that!
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

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