【VB编程】获取一个控件的屏幕位置坐标

 时间:2024-10-15 00:36:57

1、打开Visual basic6.0(一下简称vb),新建一个标准EXE,设置控件如下图所示:

【VB编程】获取一个控件的屏幕位置坐标

2、双击Form1窗体:复制如下代码粘贴到Form1代码窗口:Private Sub Command1_Click()Dim Rects As RECT, ExecuteValue As BooleanDim MousePoint As POINTAPIExecuteValue = GetDesktopWindowRect(Command2.Hwnd, Rects, MousePoint)Debug.Print "ExecuteValue=" & ExecuteValueDebug.Print "Rects.Top=" & Rects.TopDebug.Print "Rects.Left=" & Rects.LeftDebug.Print "Rect.Bottom=" & Rects.BottomDebug.Print "Rect.Right=" & Rects.RightDebug.Print "MousePoint.X= " & MousePoint.XDebug.Print "MousePoint.Y=" & MousePoint.Y'MsgBox "执行结果请看立即窗口!", 48, "提示"End SubPrivate Sub Command2_Click()SetCursorToControl Command1.HwndEnd SubPrivate Sub Label2_Click()Shell "Explorer http://hi.baidu.com/kejisoft/home"End Sub【图:复制代码到Form1代码窗口】

【VB编程】获取一个控件的屏幕位置坐标

3、点击 工程菜单》添加模块》点击打开:

【VB编程】获取一个控件的屏幕位置坐标

4、把如下代码复制到如上图所示的窗口:'Programming By Kejisoft (Http://Hi.Baidu.com/Kejisoft)'Date:2011-02-10'Functions:' GetDesktopWindowRect' 获取一个控件在屏幕用Rect描述的位置' SetCursorToControl' 设置鼠标指针到一个控件'Version:1.0.0.0'IDE:Microsoft Visual Basic 6.0企业版Declare Function GetDesktopWindow Lib "user32" () As LongDeclare Function GetWindowRect Lib "user32" (ByVal Hwnd As Long, lpRect As RECT) As LongType RECTLeft As LongTop As LongRight As LongBottom As LongEnd TypeDeclare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As LongType POINTAPIX As LongY As LongEnd TypeDeclare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As LongPublic Function GetDesktopWindowRect(Hwnd As Long, Rct As RECT, MousePos As POINTAPI) As BooleanDim execute As Integerexecute = GetWindowRect(Hwnd, Rct)GetDesktopWindowRect = IIf(execute = 0, False, True)GetCursorPos MousePosEnd FunctionPublic Function SetCursorToControl(ControlHwnd As Long) As BooleanDim Rect2 As RECT, MousePos2 As POINTAPIDim ControlX As Long, ControlY As LongSetCursorToControl = GetDesktopWindowRect(ControlHwnd, Rect2, MousePos2)'计算控件的中心位置坐标ControlX = Rect2.Left + ((Rect2.Right - Rect2.Left) / 2)ControlY = Rect2.Top + ((Rect2.Bottom - Rect2.Top) / 2)SetCursorPos ControlX, ControlYEnd Function

5、下一步可以按F5运行测试效果,完成!

  • Excel中如何添加自定义加载项
  • 如何用Minitab17进行综合自回归移动平均预测
  • 在VBA编程中怎样知道打开的工作薄的数量?
  • excel中,怎么使用VBA批量生成工作簿?
  • 如何用total commander快速查找文件
  • 热门搜索
    普法知识手抄报 为国家富强而奋斗的杰出人物手抄报 中秋节手抄报简单又好看 七十周年手抄报 关于感恩老师的手抄报 关于科技的手抄报资料 儿童手抄报模板 数学手抄报内容三年级 经典诵读手抄报图片 关于诗词的手抄报