【VB编程】获取一个控件的屏幕位置坐标
来源:网络收集 点击: 时间:2024-05-12【导读】:
在编程中,编写比如具有windows xp风格的按钮时就要用到获取该按钮控件的坐标和鼠标的坐标,从而使按钮在鼠标in的时候'点亮',鼠标out的时候'还原',这就需要获取控件的屏幕坐标,这个坐标和鼠标坐标是不需要换算的。下面是科技程序迷在自己博客写的一篇文章,现在把它转过来与大家分享。工具/原料moreVisual Basic 6.0 编程工具步骤/方法1/5分步阅读打开Visual basic6.0(一下简称vb),新建一个标准EXE,设置控件如下图所示:
2/5双击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代码窗口】
3/5点击 工程菜单》添加模块》点击打开:
4/5把如下代码复制到如上图所示的窗口:'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 Function5/5下一步可以按F5运行测试效果,完成!注意事项



无注意事项。
控件屏幕版权声明:
1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。
2、本站仅提供信息发布平台,不承担相关法律责任。
3、若侵犯您的版权或隐私,请联系本站管理员删除。
4、文章链接:http://www.1haoku.cn/art_745146.html
上一篇:玉米面发糕的做法步骤
下一篇:简单易做的日行一善手抄报