加入一个TextBox控件,一个Command控件
目前创新互联已为近千家的企业提供了网站建设、域名、虚拟主机、网站托管、服务器托管、企业网站设计、科尔沁左翼网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
代码:
Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Long) As Long
Private Sub Command1_Click()
Dim Color As Long
WindowDC = GetWindowDC(0) '获取屏幕的设备场景
Color = GetPixel(WindowDC, 500, 100) '获指定点的颜色
'分解RGB颜色值
R = (Color Mod 256) '红色
b = (Int(Color \ 65536)) '蓝色
G = ((Color - (b * 65536) - R) \ 256) '绿色
Text1.BackColor = RGB(R, G, b)
End Sub
没明白你意思,看看是这样么?Dim PGet As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(picture)for i=1 to picture.heightfor j=1 to picture.widthPGet .GetPixel(j, i)nextnext
FOR不会用多少时间,只是比较的话你只要算法OK,速度是很快的,比较不需要运算,不会耗时太多,几十毫秒就行了
'下面是屏幕找色实例,请根据实际情况进行验证。
Option Explicit
'定义一个POINTAPI
Private Type POINTAPI
x As Long
y As Long
End Type
'定义一个找色区域
Private Type RECT
Left As Long '区域坐标x
Top As Long '区域坐标y
Right As Long '区域宽
Bottom As Long '区域高
End Type
'Windows API 声明
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) 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 Function ifColor(x As RECT, ByVal color As Long) As POINTAPI
On Error Resume Next
Dim nTmpColor As Long, i As Long, j As Long
For i = x.Left To x.Left + x.Right
For j = x.Top To x.Top + x.Bottom
nTmpColor = GetPixel(GetDC(0), i, j)
If color = nTmpColor Then
ifColor.x = i
ifColor.y = j
Exit Function
End If
DoEvents
Next
Next
End Function
Private Sub Command1_Click() '全屏幕找色,时间花费较长
Dim t As POINTAPI, m As RECT
With m
.Top = 0
.Left = 0
.Bottom = Screen.Height / Screen.TwipsPerPixelY
.Right = Screen.Width / Screen.TwipsPerPixelX
End With
t = ifColor(m, 1447073)
Debug.Print t.x, t.y
End Sub
Private Sub Command2_Click() '某区域找色,时间花费少
Dim t As POINTAPI, m As RECT
With m
.Top = 300
.Left = 300
.Bottom = 100
.Right = 100
End With
t = ifColor(m, RGB(0, 125, 125))
Debug.Print t.x, t.y
End Sub
VB可使用Point方法来获取图片指定点的颜色。
Point 方法
按照长整数,返回在 Form 或 PictureBox 上所指定磅的红-绿-蓝 (RGB) 颜色。
语法
object.Point(x, y)
'窗体判色代码:
Private Sub Form1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1 = X
Text2 = Y
Text3 = Point(X, Y)
Text4 = (Val(Text3) Mod 65536) Mod 256 'Red
Text5 = (Val(Text3) Mod 65536) \ 256 'Green
Text6 = Val(Text3) \ 65536 'Blue
Shape1.FillColor = RGB(Val(Text4), Val(Text5), Val(Text6))
End Sub
'PictureBox判色代码:
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1 = X
Text2 = Y
Text3 = Picture1.Point(X, Y)
Text4 = (Val(Text3) Mod 65536) Mod 256 'Red
Text5 = (Val(Text3) Mod 65536) \ 256 'Green
Text6 = Val(Text3) \ 65536 'Blue
Shape1.FillColor = RGB(Val(Text4), Val(Text5), Val(Text6))
End Sub