ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 11855|回复: 19

[求助] 一个VB版的全屏幕区域找图找色源码转VBA

[复制链接]

TA的精华主题

TA的得分主题

发表于 2012-3-22 20:17 | 显示全部楼层 |阅读模式
如下代码是VB的,请问有没有高手,可以转变到VBA里使用,

Option Explicit
Dim mousestep As POINTAPI
Dim moubegin As POINTAPI
    '获得当前光标的坐标。
    'GetCursorPos moubegin
   ' mousestep = moubegin
   '鼠标移到 SetCursorPos moubegin.X, moubegin.Y
'====================================================
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 SetDIBits Lib "gdi32" (ByVal HDC 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 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 Declare Function GetDC Lib "user32" (ByVal hwnd 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 ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal HDC As Long) As Long '释放DC
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal HDC 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 Function DeleteDC Lib "gdi32" (ByVal HDC As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal HDC As Long, ByVal hObject As Long) As Long
Private Const SRCCOPY = &HCC0020 ' (DWORD) dest = source
Dim intX As Long
Dim intY As Long
'颜色表
Private Type RGBQUAD
    rgbBlue As Byte
    rgbGreen As Byte
    rgbRed As Byte
    rgbAlpha As Byte   '透明通道
End Type
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 BITMAPINFO
    bmiHeader As BITMAPINFOHEADER
    bmiColors As RGBQUAD
End Type
'图片文件头
Dim BI As BITMAPINFO
Dim BI1 As BITMAPINFO
Dim PP As New Form1

'在图片1中查找图片2,是否找出全部
Public Function FindPic(Left As Long, Top As Long, Right As Long, Bottom As Long, fileurl As String)
Dim P2 As Picture, P2W, P2H, P2Handle
Set P2 = LoadPicture(fileurl)
P2W = P2.Width
P2H = P2.Height
P2Handle = P2.Handle

Dim W As Long, H As Long, I As Long, J As Long
Dim W2 As Long, H2 As Long, I2 As Long, J2 As Long
Dim zPic() As Byte, fPic() As Byte
Dim R As Byte, G As Byte, B As Byte
'1 获得图片2数据
W2 = ScaleX(P2W, vbHimetric, vbPixels)
H2 = ScaleY(P2H, 8, 3)
With BI.bmiHeader
    .biSize = Len(BI.bmiHeader)
    .biWidth = W2
    .biHeight = -H2
    .biBitCount = 32
    .biPlanes = 1
End With
ReDim zPic(3, W2 - 1, H2 - 1)
I = GetDIBits(HDC, P2Handle, 0, H2, zPic(0, 0, 0), BI, 0)
Set P2 = Nothing
'Debug.Print I
'如果在这里处理一下,图像大的话,可能会快一点。
'2 获得图片1数据
W = Right
H = Bottom
With BI1.bmiHeader
    .biSize = Len(BI1.bmiHeader)
    .biWidth = W
    .biHeight = -H
    .biBitCount = 32
    .biPlanes = 1
End With
        For J2 = 0 To H2 - 2 '循环判断小图片
            For I2 = 0 To W2 - 2
                PP.PSet (I2, J2), RGB(zPic(2, I2, J2), zPic(1, I2, J2), zPic(0, I2, J2))
            Next I2
        Next J2
        PP.Refresh
        
ReDim fPic(3, W - 1, H - 1)
     Dim hBMPhDC
     Dim hDCmem As Long
     Dim Pic1Handle As Long
     Dim hBmpPrev As Long
     hBMPhDC = GetDC(0)
     '常规抓图代码,得到一个hBmp:
     hDCmem = CreateCompatibleDC(hBMPhDC)
     Pic1Handle = CreateCompatibleBitmap(hBMPhDC, Right, Bottom)
     hBmpPrev = SelectObject(hDCmem, Pic1Handle)
     BitBlt hDCmem, 0, 0, Right, Bottom, hBMPhDC, Left, Top, SRCCOPY
     'SelectObject hDCmem, hBmpPrev
     DeleteDC hDCmem
I = GetDIBits(hBMPhDC, Pic1Handle, 0, H, fPic(0, 0, 0), BI1, 0)
ReleaseDC 0, hBMPhDC

'Debug.Print I
'分析查找
For J = 0 To H - H2 - 1
VBA.DoEvents
    For I = 0 To W - W2 - 1
        
        For J2 = 0 To H2 - 2 '循环判断小图片
            For I2 = 0 To W2 - 2
               
                If fPic(2, I + I2, J + J2) <> zPic(2, I2, J2) Then GoTo ExitLine: 'R
                If fPic(1, I + I2, J + J2) <> zPic(1, I2, J2) Then GoTo ExitLine: 'G
                If fPic(0, I + I2, J + J2) <> zPic(0, I2, J2) Then GoTo ExitLine: 'B
            Next I2
        Next J2
        'Debug.Print "发现:", I, J
        intX = I
        intY = J
     
ExitLine:
    Next I
Next J
    '获得当前光标的坐标。
    'GetCursorPos moubegin
    'mousestep = moubegin
    '鼠标移到
End Function
Public Function MoveTo(X As Long, Y As Long)
SetCursorPos X, Y
End Function
Private Sub Cmd1_Click()
Dim TimerMsg
Dim sTimer As Single         '''定义操作时间 计时变量
sTimer = Timer               '''记录遍历图片内容的开始时间
FindPic CLng(Text1.Text), CLng(Text2.Text), CLng(Text3.Text), CLng(Text4.Text), Text5.Text
If intX > 0 And intY > 0 Then
    MoveTo intX, intY
    mouse_event &H4 Or &H2, 0, 0, 0, 0 '左键单击
    TimerMsg = "找到坐标: " & intX & "," & intY
        intX = 0
        intY = 0
    Else
    TimerMsg = "沒有找到"
End If
sTimer = Timer - sTimer      '''计时结束,并记录用时长度
TimerMsg = TimerMsg & vbCrLf & " 用时: " & sTimer * 1000 & "毫秒" '''显示异点,和耗时
Label2.Caption = TimerMsg
        
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2012-3-22 23:08 | 显示全部楼层
怕沉底,自己好好顶一下。{:soso_e163:}

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-1-1 17:01 | 显示全部楼层
找图片,就差一步.zip (25.87 KB, 下载次数: 81)
就差一步,先放在这里,好找一点。
现在就差,数组
那个高手能出手一下?》?

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2013-4-27 09:28 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
okok7845 发表于 2013-1-1 17:01
就差一步,先放在这里,好找一点。
现在就差,数组
那个高手能出手一下?》?

LZ好强大啊!!!!

我最近也被这个找图的问题搞死了,请问您解决了问题了吗?

非常希望您能指导下我啊,非常感谢!!!

TA的精华主题

TA的得分主题

发表于 2013-4-27 09:36 | 显示全部楼层
用按键精灵得了,不用自己写代码

TA的精华主题

TA的得分主题

发表于 2013-4-27 09:58 | 显示全部楼层
okok7845 发表于 2013-1-1 17:01
就差一步,先放在这里,好找一点。
现在就差,数组
那个高手能出手一下?》?

    Dim fPic()    <--加上这句就不会数组错误了

    ReDim fPic(3, w - 1, h - 1) '找不到工程或库


但是还是找不到图哦 而且运行好慢  原来在VB上运行的很快的~~不知道是什么原因?

TA的精华主题

TA的得分主题

发表于 2013-4-28 08:12 | 显示全部楼层
liucqa 发表于 2013-4-27 09:36
用按键精灵得了,不用自己写代码

liucqa您好,就是希望让VBA也实现这个呢!:)

TA的精华主题

TA的得分主题

发表于 2013-4-28 08:38 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
等API高手出现!!

TA的精华主题

TA的得分主题

发表于 2016-6-25 16:09 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2016-7-26 20:35 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
时间过得真快,2012年贴子了,当年已解决了,因为速度不够快,一直没有上传而已。
有需要的朋友请收藏一下。
那个数组高手能出手,把代码优化一下,就更好了。

找图.zip (20.24 KB, 下载次数: 223)

————————————————————————————————
Sub fdfdf()
Dim t0, xx, t1
t0 = Time
xx = FindPic(0, 0, 600, 600, "1.bmp")  ‘文件同一路径,命名为1.bmp
t1 = Time
MoveTo intX, intY
End Sub
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-16 23:52 , Processed in 0.052465 second(s), 16 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表