getWindowRect返回错误的坐标

时间:2018-03-29 19:04:36

标签: vb.net

我正在使用GetWindowRect来获取屏幕截图,但坐标基本上没有了。

Private Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow" () As IntPtr
Private Declare Function GetWindowRect Lib "user32" _
                        (ByVal hwnd As IntPtr,
                        ByRef lpRect As RECT) _
                        As Integer

Private Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
End Structure

Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim r As New RECT
        GetWindowRect(GetActiveWindow, r)
        MsgBox(d.Left & " " & d.Right & " " & d.Top & " " & d.Bottom)
End Sub

I suspect it has something to do with Aero desktop.然而,由于这篇文章和其他在线资源(这个主题似乎很少),我无法对如何实现它做出正面或反面。

这是非最小的例子:

  Private Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow" () As IntPtr
	Private Declare Function GetWindowRect Lib "user32" _
							(ByVal hwnd As IntPtr,
							ByRef lpRect As RECT) _
							As Integer
	Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
	Public Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Integer) As Integer
  Public Declare Function IsIconic Lib "user32.dll" (ByVal hwnd As Integer) As Boolean
  Public Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
    
	
	Private Structure RECT
		Public Left As Integer
		Public Top As Integer
		Public Right As Integer
		Public Bottom As Integer
	End Structure
							
	Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
		Dim r As New RECT
		GetWindowRect(GetActiveWindow, r)
		MsgBox(d.Left & " " & d.Right & " " & d.Top & " " & d.Bottom)
	End Sub

	Private Function activeWindowCapture()
		Dim r As New RECT
		Dim hwnd As Integer = FocusWindow("TeamViewer", Nothing)
		GetWindowRect(hwnd, r)
		Dim img As New Bitmap(r.Right - r.Left, r.Bottom - r.Top)
		Dim gr As Graphics = Graphics.FromImage(img)
		gr.CopyFromScreen(New Point(r.Left, r.Top), Point.Empty, img.Size)
		Dim sysImg As Image = DirectCast(img, Image)
		PictureBox1.Image = sysImg
		Return sysImg
	End Function
	
	Private Function FocusWindow(ByVal strWindowCaption As String, ByVal strClassName As String)
		Dim hWnd As Integer
		hWnd = FindWindow(strClassName, strWindowCaption)
		If hWnd > 0 Then
			SetForegroundWindow(hWnd)
			If IsIconic(hWnd) Then  'Restore if minimized
				ShowWindow(hWnd, SW_RESTORE)
			Else
				ShowWindow(hWnd, SW_SHOW)
			End If
		End If
		Return hWnd
	End Function

0 个答案:

没有答案