在 VS 2017 的VB.NET中编写一个 WinForm 项目,我在SplitContainer2.Panel1
. 这工作得很好,但我想缩放表单以最初适合 FreeRDP 窗口。为此,我首先需要知道 FreeRDP 实例的大小。
不幸的是,我所做的每一次尝试都没有回报。我试图GetClientRect()
从 Windows API 中使用,但我得到的只是 0(或者什么都没有,我想)。这是我第一次玩 API 调用,所以我不确定我做错了什么。我已经验证我有正确的句柄,但在 VS 中有一个中断
(如果这些缩放效果不佳,我深表歉意。很难判断缩放的 4k)
与间谍++相比
所以,我已经确认我有正确的 hWnd 句柄,但是当我调用 a 时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++ 可以得到客户端大小?我花了很多时间试图弄清楚这一点,我希望能帮助我完成这个里程碑。如果我不能很快得到它,就要转移到别的东西上,然后再回来。