专栏首页VB小源码VB.NET 仿spy++拖拽图标获取窗体句柄

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

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

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

本文分享自微信公众号 - VB小源码(vb_xym),作者:VB小源码

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2020-09-08

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • VB.NET 图片和BASE64互转

    Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。可查看RFC2045~RFC2...

    巴西_prince
  • vb.net 连接MYSQL数据库,需要MySql.Data.dll连接控件!

    '---------------------------------------------------------------------------...

    巴西_prince
  • vb.net 获取CPU序列号

    巴西_prince
  • VBA实战技巧06: 复制文本到剪贴板

    注意,上述代码运行前需要添加对“Microsoft Forms 2.0 Object Library”库的引用,方法是在VBE中单击菜单“工具——引用”,在“引...

    fanjy
  • VB.NET 图片和BASE64互转

    Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。可查看RFC2045~RFC2...

    巴西_prince
  • .NET中的密钥加密

    本教程将演示如何通过System.Security.Cryptography在.NET Framework 1.1中实现对称加密/密钥加密。

    MelodyS
  • VBA解析VBAProject 04——run length encoding

    在VBAProject中,dir流以及VBA模块代码流都使用了run length encoding的算法进行压缩。

    xyj
  • VB6.0用GDI+保存图像为BMP\JPG\PNG\GIF格式终结版。

    鉴于之前在http://blog.csdn.net/laviewpbt/article/details/756547发布的代码很匆忙,也存在不少错误,现发布比较...

    用户1138785
  • MD5的介绍,算法和C、VB、Delphi实现

    http://download.winzheng.com/infoView/Article_861.htm

    阿敏总司令
  • 如果有人问你Java虚拟机执行线程同步及Synchronized实现原理,你就把这篇文章分享给他。

    在讲解Synchronized的实现原理之前,我们先了解一下Java虚拟机是如何执行线程同步的。

    格姗知识圈

扫码关注云+社区

领取腾讯云代金券