## 如何确定绝对或相对位置?
我们经常会使用 [ClickPoint](ClickPoint方法.md) 来点击按钮或输入框等控件。那么就会涉及到目标对象的坐标位置问题。例如下面的代码
~~~
w.ClickPoint 200, 200 '点击坐标200,200
w.ClickPoint 200, 200, relative '默认就是relative,即相对模式,如果要用绝对模式就用absolute参数
~~~
这个坐标具体数值靠猜是不行的,靠不停调整尝试也是费时费力的,告诉你最简单的一个办法,打开qq聊天对话框,点里面的截图按钮,如果你要用相对模式,那么就从目标窗口的左上角开始往下截图,截图到目标位置停下,长宽是实时显示的,把这个坐标输入到你的代码里即可,如果用绝对模式那就从屏幕的左上角顶点开始下拉截图,到目标位置时候看下坐标是啥就好。
**下面以具体例子来说明下:** GetElementHwndByText
比如下面这个软件,我们想点击它里面的 **“打开串口”** 这个按钮 。我们有以下几种方法:
1. 根据按钮文字确定它然后点击,使用方法:ClickElementByText
2. 根据按钮类名确定它然后点击,使用方法:ClickElementByClassName
3. 根据按钮的坐标位置直接点击,使用方法:ClickPoint
这3种方法当然是前两种方法最好,方法3主要适用于没有句柄的按钮,比如WPF、网页等界面。我们这里就讲下如何简便的取坐标然后点击。
随便点开一个QQ聊天对话框,然后点击截图按钮:
![](https://img.kancloud.cn/b0/74/b0748d3039210c66a4e536bca77375bb_566x163.png)
这个时候从目标窗口的左上角开始截图。
![](https://img.kancloud.cn/d1/0c/d10cf488033941b549d41847a0f65255_1356x1299.png)
我们可以看到实时的长宽(250x684),这个长宽就可以看做相对于窗口左顶点的坐标。那么我们就可以写代码:
`w.ClickPoint 250, 684`
等价于:
`w.ClickPoint 250, 684, relative`
如果要用绝对坐标,也就是相对于桌面的左上角开始截图,那么就要带上absolute参数,看下面的截图:
![](https://img.kancloud.cn/3d/86/3d864571d9c901c14f776337e64731b4_1377x1296.png)
可以看到实时的长宽为(311x951),那么我们就可以写代码:
`w.ClickPoint 311, 951, absolute`
完整代码为:
~~~
If w.GetWindowByTitleEx("sscom4.2测试版").hWnd <> 0 Then
w.ClickPoint 250, 684 '推荐
'// w.ClickPoint 250, 684, relative '与上一行代码效果同等
'// w.ClickPoint 311, 951, absolute '采用绝对模式,同等效果
End If
~~~
小提示:推荐使用relative方法,也就是相对本窗口的方式,因为程序窗口相对于桌面中的位置不固定的,这样你的代码就没有通用性,窗口拖动下你用absolute绝对模式的话就点不到按钮了,用相对模式的话随便你拖动到哪儿都可以点击。当然有些程序是全屏的,比如游戏之类,具体还是看使用场景了。
- 关于clsWindow
- 一、获取窗口
- GetWindowByTitle 函数
- GetWindowByTitleEx 函数
- GetWindowByClassName 函数
- GetWindowByClassNameEx 函数
- GetWindowByPoint 函数
- GetWindowByAppName 函数
- GetWindowByAppNameEx 函数
- GetWindowByCursorPos 函数
- GetWindowByPID 函数
- GetWindowByHwnd 函数
- Load 函数(同GetWindowByHwnd)
- 二、操作窗口
- Focus 方法
- SetFocus 方法(同Focus)
- FadeIn 方法
- FadeOut 方法
- Transparent 函数
- SetTransparent 函数(同Transparent)
- MakeTransparent 函数(同Transparent)
- Shake 方法
- CloseWindow 函数
- ClassName 属性
- Caption 属性
- Text 属性(同Caption)
- Title 属性(同Caption)
- Value 属性(同Caption)
- ZOrder 方法
- Left 属性
- Top 属性
- Width 属性
- Height 属性
- Visible 属性
- Enabled 属性
- WindowState 属性
- AlphaBlend 属性
- TransparentColor 属性
- CheckWindow 函数
- Move 函数
- MoveToCenter 函数
- Show 函数
- Hide 函数
- Refresh 方法
- SetPosBottom 函数
- SetPosFront 函数
- SetPosNormal 函数
- SetTop 函数
- IsTopMost 函数
- IsForegroundWindow 属性
- Maximized 函数
- Minimized 函数
- Normal 函数
- Restore 函数(同Normal)
- 三、操作控件
- GetElementHwndByText 函数
- GetElementHwndByClassName 函数
- GetElementTextByText 函数
- GetElementTextByClassName 函数
- GetClassNameByHwnd 函数
- GetTextByHwnd 函数
- GetCaptionByHwnd 函数(同GetTextByHwnd)
- SetElementTextByText 函数
- SetElementTextByClassName 函数
- SelectComboBoxIndex 函数
- AppendElementTextByClassName 函数
- Click 函数
- ClickElementByText 函数
- ClickElementByClassName 函数
- SendKeys 方法
- Paste 方法
- FileToClipboard 函数
- ClipboardSetText 函数
- 四、鼠标坐标相关
- ClickCurrentPoint 方法
- ClickPoint 方法
- ClickPointBackground 方法
- SetCursor 方法
- MoveCursor 方法(同SetCursor)
- MoveCursorTo 方法(同SetCursor)
- SetPoint 方法(同SetCursor)
- GetCursorPoint 函数
- GetCursorPosCurrent 函数(同GetCursorPoint)
- MouseLeftDown 方法
- MouseLeftUp 方法
- MouseRightDown 方法
- MouseRightUp 方法
- DragTo 方法
- DragToEx 方法
- 五、应用程序相关
- AppPath 函数
- AppName 函数
- IsWin64 函数
- CommandLine 函数
- AppCommandLine 函数(同CommandLine)
- CloseApp 函数
- ProcessID 函数
- PID 函数(同ProcessID)
- 六、公有方法属性等
- hWnd 公有变量
- DebugMe 公有变量
- Wait 方法
- Version 方法
- 使用范例
- 01 控制微信发送消息
- 02 控制QQ发送消息
- 03 QQ登录器
- 04 花式调用计算器
- 05 用鼠标在画图工具上画圆
- 06 控制记事本输入并保存
- 07 随机移动鼠标防锁屏
- 08 根据ID进入腾讯课堂
- 09 限制程序重复打开
- 10 控制c2p软件将ceb转换为pdf格式
- 11 实战:微便签-窗口特效
- 12 实战:窗口图钉-窗口置顶
- 13 实战:微型按键精灵,录制和播放鼠标动作
- 一些Q&A
- 01 这个框架是免费的吗?
- 02 无句柄的控件元素如何操作?
- 03 目标窗体中有相同类名输入框如何区分填写?
- 04 为什么win10下最大化不生效?
- 05 如何确定绝对或相对坐标?
- 06 相同标题的窗口如何得到所有句柄?
- 联系我们