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:
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.Code:Private Sub Command1_Click() Form2.Show End Sub
Now add the following code to Form2
Try it out...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




Reply With Quote