Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

Options
Go to last post Go to first unread
Fedor  
#1 Posted : Sunday, February 8, 2004 3:41:00 PM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)
I have made a code sample in which we can pick color from any part of screen.

Screenshot:

UserPostedImage

Use this code:

Code:
Option Explicit

Private Type POINTAPI
        x As Long
        y As Long
End Type

' Export WinAPI functions
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () 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 GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Private blnMouseDown

' This variable is a HDC of the desktop.
' It is used to grab color under the mouse cursor at any part of
' the screen (even out of the application window).
Private lngDesktopDC As Long

Private Sub Form_Load()
    lngDesktopDC = 0
End Sub

Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)

    ' SetCapture WinAPI function is necessary to get mouse move events even
    ' if we leave application window.
    SetCapture Command1.hwnd

    ' GetDC with 0 as parameter will return HDC of the desktop.
    lngDesktopDC = GetDC(0)

    Command1.MousePointer = 14
End Sub

Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    If lngDesktopDC <> 0 Then

        ' Display a current cursor position
        Dim Point As POINTAPI
        GetCursorPos Point
        TextXY.Text = "X: " & Point.x & "  Y" & Point.y

        ' Grab a color under the cursor
        Dim lngColor As Long
        lngColor = GetPixel(lngDesktopDC, Point.x, Point.y)
        TextColor.Text = Hex(lngColor)
        Picture1.BackColor = lngColor
    End If
End Sub

Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)

        ' "Unsubscribe" from mouse events capturing.
    ReleaseCapture

        ' We must release a desktop DC to avoid GDI leak.
    ReleaseDC 0, lngDesktopDC

    Command1.MousePointer = 0
    lngDesktopDC = 0
End Sub

Full project code you can download in attachments.

Edited by user Monday, December 21, 2009 3:18:49 AM(UTC)  | Reason: Not specified

File Attachment(s):
GraphicsMill_ColorPicker.zip (6kb) downloaded 67 time(s).
Fedor attached the following image(s):
GraphicsMill_ColorPicker.png
Best regards,

Fedor Skvortsov

Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.