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

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

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

【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快速查找文件
  • 热门搜索
    男士运动鞋品牌大全 葡萄籽的功效与作用 四月份有什么节日 界面剂的作用 漱口水的作用 染发什么颜色好看 什么是三角恋 运动标志 毕福剑说了什么 朋友结婚送什么