關於我自己

2012年5月5日 星期六

用滑鼠座標偵測顏色

目的:
可以用滑鼠的座標取的顏色RGB
可利用在遊戲血條偵測
然後網頁可以利用只有火狐 ~ 因為火狐的CSS顏色是最標準的


程式碼:
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
    Private Declare Function GetDC Lib "user32" (ByVal hwnd 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 ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Type POINTAPI
        X As Long
        y As Long
    End Type
     Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Sub Timer1_Timer()
    Dim lngDC As Long
    Dim lngColor As Long
    Dim PtMouse As POINTAPI
    Dim hwnd As Long
    Dim lngX As Long
    Dim lngY As Long
     Dim r As Integer
     Dim g As Integer
     Dim b As Integer
     Dim IntR As Long
IntR = SetWindowPos(Me.hwnd, -1, 0, 0, 0, 0, 3)
    GetCursorPos PtMouse            '取滑鼠位置
    hwnd = WindowFromPoint(PtMouse.X - 20, PtMouse.y)  '取滑鼠位置元件的Handle 值
    lngX = PtMouse.X
    lngY = PtMouse.y
    lngDC = GetDC(hwnd)         '取該Handle值的DC
    lngColor = GetPixel(lngDC, lngX, lngY)  '取該滑鼠位置的顏色
    Text1 = "位置: x:" & lngX & ", y:" & lngY & " ; 顏色:" & lngColor & " ; hwnd : " & hwnd & vbCrLf
   
      r = lngColor Mod 256
    b = Int(lngColor / 65536)
    g = (lngColor - (b * 65536) - r) / 256
     If r = -1 Then
r = 0
End If
If g = -1 Then
g = 0
End If
If b = -1 Then
b = 0
End If
    Label1.Caption = r
    Label2.Caption = g
    Label3.Caption = b
    Picture1.BackColor = RGB(r, g, b)
    ReleaseDC hwnd, lngDC       '將DC 釋放
      
    End Sub

沒有留言:

張貼留言