VB.NET想用GetWindowRECT获取某窗口的坐标,但测试结果是L,T,R,B显示都是0,0,0,0

这个是一个,按照TEXTBOX名称选择窗口,获取窗口在屏幕坐标的程序===================================================... 这个是一个,按照TEXTBOX名称选择窗口,获取窗口在屏幕坐标的程序
========================================================
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Integer

Private Structure RECT
Dim Left As Integer
Dim Top As Integer
Dim Right As Integer
Dim Bottom As Integer

End Structure

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ksWND As Long
Dim ksTitle As String
Dim winS As RECT

If TextBox1.Text <> "" Then

ksTitle = TextBox1.Text
ksWND = FindWindow(vbNullString, ksTitle)

GetWindowRect(ksWND, winS)

MsgBox("左上角坐标(" & winS.Left & "," & winS.Top & ")" & vbCrLf & "右下角坐标(" & winS.Right & "," & winS.Bottom & ")" & vbCrLf & "窗口高" & winS.Bottom - winS.Top & "窗口宽" & winS.Right - winS.Left)

Else

MsgBox("请填写窗口名称")
End If
End Sub
=====================================================

调试还出现 “调用导致堆栈不对称”

生成EXE测试,能运行,但还是获取不了坐标

究竟哪里出了问题?本人新手,求解答。
展开
 我来答
Leon1355
2015-09-29 · 超过16用户采纳过TA的回答
知道答主
回答量:77
采纳率:100%
帮助的人:23.5万
展开全部
很简单,原因有二。第一,VB里long是32位,但是VB.NET里是64位,Dim ksWND As Integer才对。第二,VB在API里默认传址,而VB.NET默认传值,所以API里要添加Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long,添加"ByRef" lpRect As RECT) As Integer

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long,byref lpRect As RECT) As Integer

Private Structure RECT
Dim Left As Integer
Dim Top As Integer
Dim Right As Integer
Dim Bottom As Integer

End Structure

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ksWND As integer
Dim ksTitle As String
Dim winS As RECT

If TextBox1.Text <> "" Then

ksTitle = TextBox1.Text
ksWND = FindWindow(vbNullString, ksTitle)

GetWindowRect(ksWND, winS)

MsgBox("左上角坐标(" & winS.Left & "," & winS.Top & ")" & vbCrLf & "右下角坐标(" & winS.Right & "," & winS.Bottom & ")" & vbCrLf & "窗口高" & winS.Bottom - winS.Top & "窗口宽" & winS.Right - winS.Left)

Else

MsgBox("请填写窗口名称")
End If
End Sub
匿名用户
2015-04-07
展开全部
仅提供解决思路。不提供代码,API请自己找齐。
这样以后你遇到类似问题可以自行解决。
以下为思路,看不懂请自己琢磨相关逻辑和API作用。

1.GetCursorPos 获取鼠标坐标 设获得的坐标为t_pos
2.WindowFromPoint 获取t_pos坐标指向的窗体 设获得的窗体句柄为hwnd
3.GetWindowRect 根据hwnd获取对应窗体坐标。此时t_pos与窗体坐标相减可以初步计算得相对坐标(包括标题栏等),设此时计算得坐标为pos
4.GetWindowLong 获取样式,判断是否存在WS_BORDER(是否有边框)
方法:
假设GetWindowLong,获取的样式保存在style中,则if style or WS_BORDER=style then 存在WS_BORDER else 不存在WS_BORDER

如果存在WS_BORDER即有边框,则再调用GetSystemMetrics得到边框和标题栏宽度高度,然后pos与之相减(减掉边框)得到坐标相对于客户区的坐标
如果不存在WS_BORDER,则pos直接就是相对于客户区的坐标(无边框)
追问
从另一篇复制过来的答案。

这是提供了另一种思路的方法,但依然无法解答我当前的疑问。
1、GetWindowRECT得出的结果都是0是否正常?
2、但返回值是0应该都是获取失败,既然获取失败了怎能与坐标相减呢?问题出在哪里了?
3、图2那个调用导致堆栈不对称是哪一句造成的?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式