我想在VB里用按键精灵中的IFCOLOR函数~各位高人帮帮忙~!

2024-12-19 14:58:34
推荐回答(2个)
回答1:

可以使用API函数GetPixel,返回值是一个Long,可以通过Hex()将其转化为十六进制表示的String.
我帮您编了一个Function,您可以参照试试:
Private Function ifColor(ByVal x As Long, ByVal y As Long, ByVal color As Long) As Boolean
Dim nTmpColor As Long
nTmpColor = GetPixel(GetDC(0), x, y)
ifColor = (color = nTmpColor)
End Function
输入要获取的屏幕坐标x,y,以及颜色值的long值(如果是RGB的就调用的时候用RGB(R分量,G分量,B分量)函数转换成long,如果是HEX格式的就用CLng("&h" & Hex(HEX格式字符串))来取得Long值

/////////////////////3月9日修改
昨晚太困了,今天早晨起来重新给您写了一个程序,可以获得屏幕任意一点的RGB值,以及判断与给定的颜色是否相等
您可以新建一个VB6工程,在Form1上增加一个Timer空间,设置Enabled=True,Interval=100来测试效果.
注意,我的输出在立即窗口中,如果您没有打开立即窗口,可能看不到程序运行的效果,请在IDE中按Ctrl+G打开立即窗口.
代码如下:
Option Explicit

'定义一个POINTAPI
Private Type POINTAPI
x As Long
y As Long
End Type

'定义一个GRB结构
Private Type STRUCTRGB
Red As Integer
Green As Integer
Blue As Integer
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 Sub Timer1_Timer()
On Error Resume Next
Dim myRgb As STRUCTRGB
myRgb = getColor()
Form1.BackColor = RGB(myRgb.Red, myRgb.Green, myRgb.Blue)
Debug.Print myRgb.Red; myRgb.Green; myRgb.Blue
End Sub

'测试颜色函数,给定屏幕任意坐标的x值和y值以及颜色的Long形式值,返回True or False
Private Function ifColor(ByVal x As Long, ByVal y As Long, ByVal color As Long) As Boolean
On Error Resume Next
Dim nTmpColor As Long
nTmpColor = GetPixel(GetDC(0), x, y)
ifColor = (color = nTmpColor)
End Function

'获取屏幕任意一点的颜色,返回GRB结构
Private Function getColor() As STRUCTRGB
On Error Resume Next
Dim tpTemp As POINTAPI
Call GetCursorPos(tpTemp) '获取鼠标位置
Dim myColor As Long
myColor = GetPixel(GetDC(0), tpTemp.x, tpTemp.y) '获取鼠标所在位置颜色的Long值
'分离颜色
getColor.Red = myColor And &HFF
getColor.Green = (myColor And &HFF00&) \ &H100
getColor.Blue = (myColor And &HFF0000) \ &H10000
End Function

回答2:

vb里好象没有分支结构的函数的声明,再说你有那个IFCOLOR函数得原型吗?没有原型你怎么声明?
不过自己做个这样得函数也不复杂!你可以用GetPixel函数得到点得
指定点的RGB颜色,然后再判断就行了