前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VB.NET 仿spy++拖拽图标获取窗体句柄

VB.NET 仿spy++拖拽图标获取窗体句柄

作者头像
一线编程
发布2020-09-16 14:38:51
1.5K0
发布2020-09-16 14:38:51
举报
文章被收录于专栏:办公魔盒办公魔盒办公魔盒

实现方式:主要是通过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

更多精彩内容请继续关注我们的动态哦!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-09-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 办公魔盒 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档