温总 发表于 2014-4-17 08:47

屏幕取色鼠标点击 VB源码

源码如下;
Option Explicit
Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Const MOUSEEVENTF_MIDDLEDOWN = &H20
Const MOUSEEVENTF_MIDDLEUP = &H40
Const MOUSEEVENTF_MOVE = &H1 '移动鼠标
Const MOUSEEVENTF_ABSOLUTE = &H8000
Const MOUSEEVENTF_RIGHTDOWN = &H8
Const MOUSEEVENTF_RIGHTUP = &H10
Dim dwX As Long, dwY As Long
Dim X As Long, Y As Long
Private Const BI_RGB = 0&
Private Const DIB_RGB_COLORS = 0 ' color table in RGBs
Private Type POINTAPI
    X As Long
    Y As Long
End Type
Private Type BITMAPINFOHEADER '40 bytes
      biSize As Long
      biWidth As Long
      biHeight As Long
      biPlanes As Integer
      biBitCount As Integer
      biCompression As Long
      biSizeImage As Long
      biXPelsPerMeter As Long
      biYPelsPerMeter As Long
      biClrUsed As Long
      biClrImportant As Long
End Type
Private Type RGBQUAD
      rgbBlue As Byte
      rgbGreen As Byte
      rgbRed As Byte
      rgbReserved As Byte
End Type
Private Type BITMAPINFO
      bmiHeader As BITMAPINFOHEADER
      bmiColors As RGBQUAD
End Type
Private Type RGBCOLOR
      rgbRed As Byte
      rgbGreen As Byte
      rgbBlue As Byte
      rgbReserved As Byte
End Type

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 CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateDIBSection Lib "gdi32" (ByVal hdc As Long, pBitmapInfo As BITMAPINFO, ByVal un As Long, lplpVoid As Long, ByVal handle As Long, ByVal dw As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject 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 Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
'Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Sub Command1_Click()
Dim hmemDC As Long, hmemBMP As Long, bmp_info As BITMAPINFO, lpBits As Long

Dim ygl As POINTAPI

Dim PicData() As Byte
Dim ScreenDC As Long
Dim TargetColor As Long
Dim crColor As RGBCOLOR

'要查找的颜色
TargetColor = &HFF&

'把要查找的颜色复制到一个RGBCOLOR结构体中
CopyMemory crColor, TargetColor, 4

'获取屏幕DC
ScreenDC = GetDC(0)

'设置一下bmp_info的内容供CreateDIBSection使用
With bmp_info.bmiHeader
.biSize = LenB(bmp_info.bmiHeader)
.biWidth = Screen.Width / Screen.TwipsPerPixelX
.biHeight = Screen.Height / Screen.TwipsPerPixelY
.biPlanes = 1
.biBitCount = 32
.biCompression = BI_RGB
.biSizeImage = .biHeight * (((.biWidth * .biBitCount + 31) And &HFFFFFFE0) \ 8)
End With

'创建一个DC和一个GDI对象,并把创建的GDI对象选到DC中
hmemDC = CreateCompatibleDC(ScreenDC)
hmemBMP = CreateDIBSection(ScreenDC, bmp_info, DIB_RGB_COLORS, lpBits, 0, 0)
SelectObject hmemDC, hmemBMP

'从屏幕DC中复制图片到创建DC中
BitBlt hmemDC, 0, 0, bmp_info.bmiHeader.biWidth, bmp_info.bmiHeader.biHeight, ScreenDC, 0, 0, vbSrcCopy

'设置动态数组大小
ReDim PicData(3, bmp_info.bmiHeader.biWidth - 1, bmp_info.bmiHeader.biHeight - 1) As Byte

'将创建的GDI对象中的图像数据复制到数组中
CopyMemory PicData(0, 0, 0), ByVal lpBits, bmp_info.bmiHeader.biSizeImage

'循环查找数据
Debug.Print "查找坐标范围:(0,0) - (" & CStr(bmp_info.bmiHeader.biWidth - 1) & "," & CStr(bmp_info.bmiHeader.biHeight - 1) & ")"
For dwY = 0 To bmp_info.bmiHeader.biHeight - 1
For dwX = 0 To bmp_info.bmiHeader.biWidth - 1
If (PicData(0, dwX, dwY) = crColor.rgbBlue) And (PicData(1, dwX, dwY) = crColor.rgbGreen) And (PicData(2, dwX, dwY) = crColor.rgbRed) Then

X = dwX
Y = (bmp_info.bmiHeader.biHeight - dwY - 1)

SetCursorPos X, Y
mouse_event MOUSEEVENTF_MOVE, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
Debug.Print "找到目标颜色,坐标:" & CStr(X) & "," & CStr(Y)
End If
Next
Next
Debug.Print "查找结束"

'删除创建的DC和GDI对象
DeleteDC hmemDC
DeleteObject hmemBMP
'释放获取的屏幕DC
ReleaseDC 0, ScreenDC
End Sub

linglong2013 发表于 2014-4-17 08:57

不明觉厉

791898456 发表于 2014-4-17 09:17

不明觉厉

OllyDbg丶 发表于 2014-4-17 11:27

这个貌似可以做游戏的叉叉,谢谢了{:301_987:}

linglong2013 发表于 2014-4-17 13:12

linglong2013 发表于 2014-4-17 08:57
不明觉厉

是么?呵呵

随风而行 发表于 2015-5-4 12:53

大赞楼主的分享精神
页: [1]
查看完整版本: 屏幕取色鼠标点击 VB源码