实现方式:主要是通过winapi实现窗体的捕捉,取句柄,发送信息等功能
废话不多说直接上代码:
Imports System.Runtime.InteropServices
Imports System.Text
Public Class Form1
'获取窗口句柄1
<DllImport("User32.dll", EntryPoint:="WindowFromPoint", SetLastError:=True, CharSet:=CharSet.Auto)>
Public Shared Function WindowFromPoint(pt As Point) As IntPtr
End Function
'获取窗口句柄2
<DllImport("user32.dll", EntryPoint:="GetForegroundWindow", SetLastError:=True, CharSet:=CharSet.Auto)>
Public Shared Function GetForegroundWindow() As IntPtr
End Function
'发送信息(同步)
<DllImport("User32.dll", EntryPoint:="SendMessage", SetLastError:=True, CharSet:=CharSet.Auto)>
Public Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As StringBuilder) As Integer
End Function
'发送消息(异步)
<DllImport("User32.dll", EntryPoint:="PostMessage", SetLastError:=True, CharSet:=CharSet.Auto)>
Public Shared Function PostMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As StringBuilder) As Integer
End Function
'获取窗体类名
<DllImport("user32.dll", EntryPoint:="GetClassName", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function GetClassName(hWnd As IntPtr, lpClassName As StringBuilder, nMaxCount As Integer) As Integer
End Function
'获取窗体文本
<DllImport("user32.dll", EntryPoint:="GetWindowText", SetLastError:=True, CharSet:=CharSet.Auto)>
Public Shared Function GetWindowText(hWnd As IntPtr, lpString As StringBuilder, nMaxCount As Integer) As Integer
End Function
'查找窗体句柄
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)>
Public Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
End Function
'调整窗体(大小位置)
<DllImport("user32.dll", EntryPoint:="MoveWindow", SetLastError:=True, CharSet:=CharSet.Auto)>
Public Shared Function MoveWindow(hWnd As IntPtr, x As Integer, y As Integer, nWidth As Integer, nHeight As Integer, BRePaint As Boolean) As Integer
End Function
'窗体显示隐藏
<DllImport("user32.dll", EntryPoint:="ShowWindow", SetLastError:=True, CharSet:=CharSet.Auto)>
Public Shared Function ShowWindow(hwnd As IntPtr, nCmdShow As Integer) As Integer
End Function
'获取窗口大小
<DllImport("user32.dll", EntryPoint:="GetWindowRect", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function GetWindowRect(hWnd As IntPtr, ByRef lpRect As RECT) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<StructLayout(LayoutKind.Sequential)>
Public Structure RECT
Public Left As Integer '最左坐标
Public Top As Integer '最上坐标
Public Right As Integer '最右坐标
Public Bottom As Integer '最下坐标
End Structure
'查找子窗体句柄
<DllImport("user32.dll", EntryPoint:="FindWindowEx", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function FindWindowEx(hwndParent As IntPtr, hwndChildAfter As IntPtr, lpszClass As String, lpszWindow As String) As IntPtr
End Function
'======================================
Dim Hwd As IntPtr = IntPtr.Zero
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MaximizeBox = False
MinimizeBox = False
End Sub
Private Sub Pic_Wnd_MouseDown(sender As Object, e As MouseEventArgs) Handles Pic_Wnd.MouseDown
If e.Button = MouseButtons.Left Then
TextBox1.Text = ""
Cursor.Current = New Cursor(Application.StartupPath & "\szj.cur")
Pic_Wnd.Capture = True
End If
End Sub
Private Sub Pic_Wnd_MouseUp(sender As Object, e As MouseEventArgs) Handles Pic_Wnd.MouseUp
Pic_Wnd.Capture = False
End Sub
Private Sub Pic_Wnd_MouseMove(sender As Object, e As MouseEventArgs) Handles Pic_Wnd.MouseMove
If Not Bounds.Contains(Cursor.Position) Then
'获取窗口句柄
Hwd = WindowFromPoint(Cursor.Position)
Hwd = Hwd.ToString("D").PadLeft(8, "0"c)
TextBox1.Text = Hwd.ToString
'------------------------
'获取窗口类名
Dim Classname As New StringBuilder(256)
GetClassName(Hwd, Classname, 256)
TextBox2.Text = Classname.ToString
''------------------------
'获取窗口标题
Dim title As New StringBuilder(256)
Dim titlea As New StringBuilder(256)
SendMessage(Hwd, &HD&, 256, title)
GetWindowText(Hwd, titlea, 256)
If titlea.ToString <> "" Then
TextBox3.Text = title.ToString
Else
TextBox3.Text = titlea.ToString
End If
''-------------------------
''获取窗口大小
Dim rc As New RECT
GetWindowRect(Hwd, rc)
TextBox4.Text = (rc.Right - rc.Left) & " X " & rc.Bottom - rc.Top
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SendMessage(Hwd, &HC&, 256, New StringBuilder(TextBox5.Text))
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ShowWindow(Hwd, 1)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
ShowWindow(Hwd, 0)
End Sub
End Class
示例下载:
https://vbee.lanzous.com/i4EOogged4d
更多精彩内容请继续关注我们的动态哦!