Option Explicit
Private Const BI_RGB = 0&
Private Const DIB_RGB_COLORS = 0&
Private Type BITMAPINFOHEADER
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 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, ByVal lplpVoid As Long, ByVal handle As Long, ByVal dw As Long) As Long
Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
Private Declare Function SetDIBitsToDevice Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal Scan As Long, ByVal NumScans As Long, Bits As Any, BitsInfo As BITMAPINFO, ByVal wUsage As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject 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 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 GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight 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 GetCurrentObject Lib "gdi32" (ByVal hdc As Long, ByVal uObjectType As Long) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Const OBJ_BITMAP As Long = &H7
Dim iBitmap As Long, iDC As Long
Private m_Color() As Long
Private Sub GetColor()
On Error GoTo ToEnd
Dim bi24BitInfo As BITMAPINFO, bBytes() As Byte, Cnt As Long
Dim TmpColor As Long
Dim W As Long, h As Long
Dim X As Long, Y As Long
Dim R As Long
Dim hdc As Long
Dim TmpX As Long, TmpY As Long
Err.Clear
Erase m_Color
W = Screen.Width / Screen.TwipsPerPixelX
h = Screen.Height / Screen.TwipsPerPixelY
With bi24BitInfo.bmiHeader
.biBitCount = 24
.biCompression = BI_RGB
.biPlanes = 1
.biSize = Len(bi24BitInfo.bmiHeader)
.biWidth = W
.biHeight = h
End With
hdc = GetDC(0)
ReDim bBytes(bi24BitInfo.bmiHeader.biWidth * bi24BitInfo.bmiHeader.biHeight * 3 - 1) As Byte
iDC = CreateCompatibleDC(0)
iBitmap = CreateDIBSection(iDC, bi24BitInfo, DIB_RGB_COLORS, ByVal 0&, ByVal 0&, ByVal 0&)
SelectObject iDC, iBitmap
BitBlt iDC, 0, 0, bi24BitInfo.bmiHeader.biWidth, bi24BitInfo.bmiHeader.biHeight, GetDC(0), 0, 0, vbSrcCopy
GetDIBits iDC, iBitmap, 0, bi24BitInfo.bmiHeader.biHeight, bBytes(0), bi24BitInfo, DIB_RGB_COLORS
ReDim m_Color(W, h)
For Y = h To 1 Step -1
TmpX = 0
For X = 1 To W
CopyMemory TmpColor, bBytes(R), 3
m_Color(X, Y) = TmpColor
R = R + 3
TmpX = TmpX + 1
Next
TmpY = TmpY + 1
Next
ToEnd:
If Err.Number <> 0 Then MsgBox Err.Description
Erase bBytes
DeleteDC iDC
DeleteDC hdc
DeleteObject iBitmap
End Sub
Public Sub SCanColor(StartX, StartY, EndX, EndY, Color, RGBPoor, SCanType, P, ReturnX, ReturnY)
'If CshPd Then MsgBox "试用到期!", 0, "": Exit Function
'RGBPoor = RBG各项的差值
'SCanType = 查找方式,值 1=左上到右下,2=右下到左上,3=右上到左下,4=左下到右上
'P = 是否从新获取屏幕,值 1=从新获取,0=不从新获取
'On Error Resume Next
'If UBound(m_Color(), 1) <= 1 Then GetColor
Color = Replace(Color, "#", "")
Color = CLng("&h" & Color)
Color = (&HFF And Color) * &H10000 + (&HFF00& And Color) + (&HFF0000 And Color) / &H10000
Dim R As Long, G As Long, B As Long
Dim R1 As Long, G1 As Long, B1 As Long
Dim TColor As Long
Dim I As Long, j As Long
Dim Ei As Long, Ej As Long
Dim Si As Long, Sj As Long
Dim StepC As Long, StepC1 As Long
Dim TP As Boolean
B = (Color \ 65536) And &HFF
G = (Color \ 256) And &HFF
R = Color And &HFF
If CBool(P) Then GetColor
Select Case Val(SCanType)
Case 1
Si = StartY
Sj = StartX
Ei = EndY
Ej = EndX
StepC = 1
StepC1 = 1
Case 2
Si = EndY
Sj = EndX
Ei = StartY
Ej = StartX
StepC = -1
StepC1 = -1
Case 3
Si = EndX
Sj = StartY
Ei = StartX
Ej = EndY
StepC = -1
StepC1 = 1
TP = True
Case 4
Si = StartX
Sj = EndY
Ei = EndX
Ej = StartY
StepC = 1
StepC1 = -1
TP = True
Case Else
ReturnX = -1
ReturnY = -1
Exit Sub
End Select
For I = Si To Ei Step StepC
For j = Sj To Ej Step StepC1
If TP Then
TColor = m_Color(I, j)
B1 = (TColor \ 65536) And &HFF
G1 = (TColor \ 256) And &HFF
R1 = TColor And &HFF
If Abs(B1 - B) < RGBPoor And Abs(G1 - G) < RGBPoor And Abs(R1 - R) < RGBPoor Then
ReturnX = I - 1
ReturnY = j - 1
Exit Sub
End If
Else
TColor = m_Color(j, I)
B1 = (TColor \ 65536) And &HFF
G1 = (TColor \ 256) And &HFF
R1 = TColor And &HFF
If Abs(B1 - B) < RGBPoor And Abs(G1 - G) < RGBPoor And Abs(R1 - R) < RGBPoor Then
ReturnX = j - 1
ReturnY = I - 1
Exit Sub
End If
End If
Next
Next
ReturnX = -1
ReturnY = -1
End Sub
Public Function GetPixelColor(X, Y, P) As String
On Error GoTo er
Dim T As Long
If CBool(P) Then GoTo er
T = m_Color(X, Y)
T = (&HFF And T) * &H10000 + (&HFF00& And T) + (&HFF0000 And T) / &H10000
GetPixelColor = "#" & Hex(T)
Exit Function
er:
GetColor
T = m_Color(X, Y)
T = (&HFF And T) * &H10000 + (&HFF00& And T) + (&HFF0000 And T) / &H10000
GetPixelColor = "#" & Hex(T)
End Function