27
Mar
09

[VBA] Q: How to get pixel colour

The question asked “On the image if i click anywhere, im supposed to tell the user what colour the pixel they clicked on was”

The solution:

Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (ByVal lpPoint As POINT) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long

Private Type POINT
    x As Long
    y As Long
End Type

Private Sub Image1_Click()
    Dim pLocation As POINT
    Dim lColour, lDC As Long
    lDC = GetWindowDC(0)
    Call GetCursorPos(pLocation)
    lColour = GetPixel(lDC, pLocation.x, pLocation.y)
    Me.BackColor = lColour
End Sub

Advertisements

0 Responses to “[VBA] Q: How to get pixel colour”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: