VB获取窗体上的按钮控件并单击示例

前端之家收集整理的这篇文章主要介绍了VB获取窗体上的按钮控件并单击示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

例一:自动点击计算器上的个按钮Module1.bas:程序代码Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String,ByVal lpWindowName As String) As LongDeclare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long,ByVal hWnd2 As Long,ByVal lpsz1 As String,ByVal lpsz2 As String) As LongDeclare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long,ByVal wMsg As Long,ByVal wParam As Long,lParam As Any) As LongPublic Const WM_LBUTTONDOWN = &H201 '左键按下Public Const WM_LBUTTONUP = &H202 '左键弹起Public Const MK_LBUTTON = &H1Public Const BM_CLICK = &HF5 '单击form1代码:程序代码Private Sub Form_Load()Dim calc,btn_1,btn2,btn_3 As Longcalc = FindWindow("SciCalc",vbNullString) '获取计算器句柄'calc = FindWindowEx(0,"SciCalc",vbNullString) '同上btn_1 = FindWindowEx(calc,"Button","1") '获取按钮1句柄btn_2 = FindWindowEx(calc,"2") '获取按钮2句柄btn_3 = FindWindowEx(calc,"3") '获取按钮3句柄PostMessage btn_1,WM_LBUTTONDOWN,ByVal 0 '鼠标在按钮1按下PostMessage btn_1,WM_LBUTTONUP,ByVal 0 '鼠标在按钮1弹起PostMessage btn_2,ByVal 0 '鼠标在按钮2按下PostMessage btn_2,ByVal 0 '鼠标在按钮2弹起PostMessage btn_3,ByVal 0 '鼠标在按钮3按下PostMessage btn_3,ByVal 0 '鼠标在按钮3弹起'效果同上'PostMessage btn_1,BM_CLICK,0 '单击按钮1'PostMessage btn_2,0 '单击按钮2'PostMessage btn_3,0 '单击按钮3End Sub例二:自动点击扫雷程序的第一个方块Module1.bas:程序代码Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String,ByVal lpWindowName As String) As LongDeclare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long,lParam As Any) As LongPublic Const WM_LBUTTONDOWN = &H201Public Const WM_LBUTTONUP = &H202Public Const MK_LBUTTON = &H1form1代码:程序代码Private Sub Form_Load()Dim winmine As LongDim lp As Longwinmine = FindWindow("扫雷",vbNullString) '获取扫雷程序句柄lp = 60 * 65536 + 15 'Y * 65536 + XPostMessage winmine,MK_LBUTTON,ByVal lpPostMessage winmine,ByVal lpEnd Sub分析说明对于计算器程序,界面上的每个按钮都是一个控件,而扫雷程序,整个窗体就一个控件,无法单独获取到每个按钮控件,所以例一使用的是按钮句柄,而例二使用的是扫雷程序句柄,并且指定了坐标。这边就有个问题:如果例一也改为使用计算器句柄,并指定坐标,结果还会一样吗?测试结果表明:只有计算器窗体能响应鼠标消息,而按钮(子控件)无法响应鼠标消息,就不能激发按钮的点击事件,也就无法正常输入数字!

原文链接:https://www.f2er.com/vb/261177.html

猜你在找的VB相关文章