Results 1 to 6 of 6

Thread: Pixels

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Posts
    96

    Question Pixels

    How would you go about finding the RGB Value of a pixel outside the forum? Like say I had it set up to display the RGB of the pixel my mouse is on, so I could move my mouse over anything and get the pixel value of it.

  2. #2
    jim mcnamara
    Guest
    Here is sample code getting pixel under the mouse - inside the project. Partial solution to your problem.

    Code:
    'This project needs:
    '- two picture boxes
    '- a button
    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 SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long
    Private Declare Function PaintDesktop Lib "user32" (ByVal hdc As Long) As Long
    Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc 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 nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
    Private Declare Function GetBkColor Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, lpPoint As POINTAPI) As Long
    Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
    
    Const ScrCopy = &HCC0020
    Const Yellow = &HFFFF&
    Private Sub Form_Load()
        'KPD-Team 1998
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        Dim Cnt1 As Byte, Cnt2 As Byte, Point As POINTAPI
        'Set the graphic mode to persistent
        Me.AutoRedraw = True
        'API uses pixels
        Me.ScaleMode = vbPixels
        Picture1.ScaleMode = vbPixels
        Picture2.ScaleMode = vbPixels
        'No borders
        Picture1.BorderStyle = 0: Picture2.BorderStyle = 0
        'Set the button's caption
        Command1.Caption = "Paint && Stretch"
        'Set the graphic mode to 'non persistent'
        Picture1.AutoRedraw = False: Picture2.AutoRedraw = False
        For Cnt1 = 0 To 100 Step 3
            For Cnt2 = 0 To 100 Step 3
                'Set the start-point's coördinates
                Point.X = Cnt1: Point.Y = Cnt2
                'Move the active point
                MoveToEx Me.hdc, Cnt1, Cnt2, Point
                'Draw a line from the active point to the given point
                LineTo Me.hdc, 200, 200
            Next Cnt2
        Next Cnt1
        For Cnt1 = 0 To 100 Step 5
            For Cnt2 = 0 To 100 Step 5
                'Draw a pixel
                SetPixel Me.hdc, Cnt1, Cnt2, Yellow
            Next Cnt2
        Next Cnt1
    End Sub
    Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim XX As Long, YY As Long, A As Long
        XX = X: YY = Y
        'Set the picturebox' backcolor
        Picture2.BackColor = GetPixel(Picture1.hdc, XX, YY)
    End Sub
    Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = 1 Then
            Dim XX As Long, YY As Long, A As Long
            XX = X: YY = Y
            'Set the picturebox' backcolor
            Picture2.BackColor = GetPixel(Picture1.hdc, XX, YY)
        End If
    End Sub
    Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Dim XX As Long, YY As Long, A As Long
        XX = X: YY = Y
        'Set the picturebox' backcolor
        Picture2.BackColor = GetPixel(Picture1.hdc, XX, YY)
    End Sub
    Private Sub Command1_Click()
        'Set the width and height
        Picture2.Width = 100: Picture2.Height = 100
        Picture1.Width = 50: Picture1.Height = 50
        'No pictures
        Picture1.Picture = LoadPicture("")
        DoEvents
        Copy the desktop to our picturebox
        PaintDesktop Picture1.hdc
        'Stretch the picture
        StretchBlt Picture2.hdc, 0, 0, 100, 100, Picture1.hdc, 0, 0, 50, 50, ScrCopy
    End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Posts
    96
    that helps but is there any way to use this outside the project?

  4. #4
    jim mcnamara
    Guest
    The SetCapture api lets you grab all mouse input, but you have to be careful using it. It will capture mouse input from anywhere on the screen.

    This link has sample code links that show setcapture

    http://www.allapi.net/apilist/apifun...ion=SetCapture

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Posts
    96

    think I got it figured out

    Using the getdc function I Remember a function that would find the hwnd of a window. this will return the hdc of a window allowing me to say GetPixal getdc(windowiwant), mouse x, mouse y mouse x and y might not work but the getdc funtion is all i need. thnx for the help

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2001
    Posts
    96

    Talking ahhhh

    Now I really got it figured out. lol All I had to do is go dadc = getdc(0) then use the getpixel function on that. And it works

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