在VS 2017上用VB.NET编写WinForm项目,我在SplitContainer2.Panel1
中打开FreeRDP的实例。这很好用,但我想缩放表单以初始适合FreeRDP窗口。为此,我首先需要知道FreeRDP实例的大小。
不幸的是,我所做的每一次尝试都没有回报。我正在尝试使用Windows API中的GetClientRect()
,但我得到的所有内容都是0(或者我没想过)。这是我第一次玩API调用,所以我不确定我做错了什么。我已经确认我在VS中有一个正确的句柄
(如果这些规模很差,我很抱歉。很难判断缩放的4k)
与Spy ++相比
所以,我已经确认我有正确的hWnd句柄,但当我打电话给GetClientRect()
时,我什么也得不回来。
以下是相关代码:
Dim rdpWnd As New IntPtr
Dim proc As New Process
Private Declare Auto Function GetClientRect Lib "user32.dll" ( _
ByVal hWnd As IntPtr, ByVal lpRect As RECT) As Boolean
<StructLayout(LayoutKind.Sequential)>
Private Structure RECT
Private Left As Short
Private Top As Short
Private Right As Short
Private Bottom As Short
End Structure
Private Sub Form_Load( _
sender As Object, e As EventArgs) Handles MyBase.Load
Dim startInfo As New ProcessStartInfo With {
.FileName = """" & appPath & "\console\wfreerdp.exe""",
.Arguments = "/parent-window:" & SplitContainer2.Panel1.Handle.ToString() & " /t:" & vmId
}
proc = Process.Start(startInfo)
rdpWnd = getWindowHandle(Me.Text, vmId)
End Sub
Private Function getWindowHandle(caption As String, Guid As String) As IntPtr
Dim hWnd As IntPtr = FindWindow(Nothing, caption)
If hWnd.Equals(IntPtr.Zero) Then
Return Nothing
End If
.......
Dim hWndRdp As IntPtr = FindWindowEx(hWndChild4, IntPtr.Zero, Nothing, Guid)
If hWndRdp.Equals(IntPtr.Zero) Then
Return Nothing
End If
Return hWndRdp
End Function
此时,我正在查看表单中Hyper-V VM的控制台窗口,但是当我点击众所周知的Button1时......
Private Sub Button1_MouseClick( _
sender As Object, e As MouseEventArgs) Handles Button1.MouseClick
Dim myRect As New RECT
GetClientRect(rdpWnd, myRect)
Dim rdpWndWidth As Short = myRect.Right - myRect.Left
Dim rdpWndHeight As Short = myRect.Bottom - myRect.Top
MsgBox("Width: " & rdpWndWidth & vbCrLf &
"Height: " & rdpWndHeight)
End Sub
MsgBox()
返回:
和VS中的变量:
我做错了什么?当我能清楚地看到Spy ++可以时,为什么我无法获得客户端大小?我花了很多时间试图解决这个问题,我很感激帮助我们完成这个里程碑的检查。如果我不能很快得到它,即将转移到其他地方并稍后再回来。
答案 0 :(得分:1)
您正在将RECT
struct 按值传递给GetClientRect
。这将创建结构的副本。因此,方法调用永远不会修改原始RECT
值,因此其所有字段都将具有默认值(即零)。
将方法签名更改为
Private Declare Auto Function GetClientRect Lib "user32.dll" ( _
ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean
(请注意ByRef
关键字)。