C#user32.dll内嵌方法使用详解

目录

1.GetWindowText 获取给定窗口句柄的窗口标题

2.FindWindowEx函数在指定的父窗口下查找具有指定类名和窗口标题的窗口

3.FindWindow函数根据窗口的类名和窗口标题进行查找

4.IsWindowVisible函数用于确定指定窗口是否可见

5.GetWindowTextLength函数用于获取指定窗口标题文本的长度

6.SetForegroundWindow函数用于将指定窗口设置为前台窗口,并将其激活

7.EnumWindows函数用于枚举系统中所有顶级窗口的句柄

8.EnumChildWindows函数来获取指定窗口的所有子窗口句柄

9.EnumChildWindows函数用于枚举指定父窗口的所有子窗口

10.SetWindowsHookEx、UnhookWindowsHookEx和CallNextHookEx函数安装、卸载、调用下一个勾子


1.GetWindowText 获取给定窗口句柄的窗口标题

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder lpString, int nMaxCount);

static void Main()

{

IntPtr hWnd = ...; // 要获取窗口标题的窗口句柄

// 指定字符串缓冲区的大小

const int maxCount = 256;

var sb = new System.Text.StringBuilder(maxCount);

// 调用GetWindowText函数获取窗口标题

int length = GetWindowText(hWnd, sb, maxCount);

if (length > 0)

{

string windowTitle = sb.ToString();

Console.WriteLine("窗口标题: " + windowTitle);

}

else { Console.WriteLine("无法获取窗口标题."); }

}

2.FindWindowEx函数在指定的父窗口下查找具有指定类名和窗口标题的窗口

[DllImport("user32.dll", SetLastError = true)]

public static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpszClass, string lpszWindow);

static void Main()

{

IntPtr parentHandle = ...; // 父窗口句柄

IntPtr childAfterHandle = IntPtr.Zero; // 子窗口句柄

string className = "SomeClassName"; // 窗口类名

string windowTitle = "SomeWindowTitle"; // 窗口标题

// 调用FindWindowEx函数查找指定类名和窗口标题的窗口

IntPtr foundHandle = FindWindowEx(parentHandle, childAfterHandle, className, windowTitle);

if (foundHandle != IntPtr.Zero)

{

Console.WriteLine("找到窗口!"); // 执行你的操作,例如发送消息或获取窗口句柄

}

else

{

Console.WriteLine("未找到窗口.");

}

}

3.FindWindow函数根据窗口的类名和窗口标题进行查找

[DllImport("user32.dll", SetLastError = true)]

static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

static void Main()

{

// 在此处指定窗口的类名和窗口标题

string className = null; // 如果不关心类名,可以设置为 null 或者空字符串

string windowName = "Untitled - Notepad";

// 调用 FindWindow 函数来查找窗口句柄

IntPtr windowHandle = FindWindow(className, windowName);

if (windowHandle != IntPtr.Zero)

{

Console.WriteLine("找到窗口句柄: " + windowHandle);

}

else

{

Console.WriteLine("未找到窗口句柄"); } Console.ReadLine();

}

4.IsWindowVisible函数用于确定指定窗口是否可见


    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool IsWindowVisible(IntPtr hWnd);

    static void Main()
    {
        // 在此处指定窗口的句柄
        IntPtr windowHandle = new IntPtr(123456); // 你需要替换为实际的窗口句柄

        // 调用 IsWindowVisible 函数来确定窗口是否可见
        bool isVisible = IsWindowVisible(windowHandle);

        if (isVisible)
        {
            Console.WriteLine("窗口可见");
        }
        else
        {
            Console.WriteLine("窗口不可见");
        }

        Console.ReadLine();
    }

5.GetWindowTextLength函数用于获取指定窗口标题文本的长度

     [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern int GetWindowTextLength(IntPtr hWnd);

    static void Main()
    {
        // 在此处指定窗口的句柄
        IntPtr windowHandle = new IntPtr(123456); // 你需要替换为实际的窗口句柄

        // 调用 GetWindowTextLength 函数来获取窗口标题文本的长度
        int titleLength = GetWindowTextLength(windowHandle);

        if (titleLength > 0)
        {
            Console.WriteLine("窗口标题文本长度: " + titleLength);
        }
        else
        {
            Console.WriteLine("无法获取窗口标题文本长度");
        }

        Console.ReadLine();
    }

6.SetForegroundWindow函数用于将指定窗口设置为前台窗口,并将其激活

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetForegroundWindow(IntPtr hWnd);

    static void Main()
    {
        // 在此处指定窗口的句柄
        IntPtr windowHandle = new IntPtr(123456); // 你需要替换为实际的窗口句柄

        // 调用 SetForegroundWindow 函数将指定窗口设置为前台窗口
        bool success = SetForegroundWindow(windowHandle);

        if (success)
        {
            Console.WriteLine("窗口设置为前台成功");
        }
        else
        {
            Console.WriteLine("窗口设置为前台失败");
        }

        Console.ReadLine();
    }

7.EnumWindows函数用于枚举系统中所有顶级窗口的句柄

    // 定义回调函数的委托类型
    delegate bool EnumWindowsCallback(IntPtr hWnd, IntPtr lParam);

    // 导入 user32.dll 库中的 EnumWindows 函数
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool EnumWindows(EnumWindowsCallback lpEnumFunc, IntPtr lParam);

    static void Main()
    {
        // 调用 EnumWindows 函数来枚举系统中的所有顶级窗口
        List<IntPtr> windowHandles = new List<IntPtr>();
        EnumWindows((hWnd, lParam) =>
        {
            windowHandles.Add(hWnd);
            return true; // 返回 true 继续枚举,返回 false 停止枚举
        }, IntPtr.Zero);

        // 输出枚举到的窗口句柄
        foreach (IntPtr windowHandle in windowHandles)
        {
            Console.WriteLine("窗口句柄: " + windowHandle);
        }

        Console.ReadLine();
    }

8.EnumChildWindows函数来获取指定窗口的所有子窗口句柄

    // 定义回调函数的委托类型
    delegate bool EnumChildWindowsCallback(IntPtr hWnd, IntPtr lParam);

    // 导入 user32.dll 库中的 EnumChildWindows 函数
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildWindowsCallback lpEnumFunc, IntPtr lParam);

    static void Main()
    {
        // 在此处指定父窗口的句柄
        IntPtr parentWindowHandle = new IntPtr(123456); // 你需要替换为实际的父窗口句柄

        // 调用 EnumChildWindows 函数来获取指定窗口的所有子窗口句柄
        List<IntPtr> childWindowHandles = new List<IntPtr>();
        EnumChildWindows(parentWindowHandle, (hWnd, lParam) =>
        {
            childWindowHandles.Add(hWnd);
            return true; // 返回 true 继续枚举,返回 false 停止枚举
        }, IntPtr.Zero);

        // 输出枚举到的子窗口句柄
        foreach (IntPtr childWindowHandle in childWindowHandles)
        {
            Console.WriteLine("子窗口句柄: " + childWindowHandle);
        }

        Console.ReadLine();
    }

9.EnumChildWindows函数用于枚举指定父窗口的所有子窗口

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool EnumChildWindows(IntPtr hWndParent, EnumChildWindowsCallback lpEnumFunc, IntPtr lParam);

    // 定义回调函数的委托类型
    delegate bool EnumChildWindowsCallback(IntPtr hWnd, IntPtr lParam);

    static void Main()
    {
        // 在此处指定父窗口的句柄
        IntPtr parentWindowHandle = new IntPtr(123456); // 你需要替换为实际的父窗口句柄

        // 调用 EnumChildWindows 函数来枚举指定父窗口的所有子窗口
        EnumChildWindows(parentWindowHandle, EnumChildWindowsCallbackMethod, IntPtr.Zero);

        Console.ReadLine();
    }

    // 回调函数
    static bool EnumChildWindowsCallbackMethod(IntPtr hWnd, IntPtr lParam)
    {
        // 在这里对每个子窗口进行操作
        // 例如获取子窗口标题、样式等信息

        // 输出子窗口句柄
        Console.WriteLine("子窗口句柄: " + hWnd);

        // 返回 true 继续枚举,返回 false 停止枚举
        return true;
    }

10.SetWindowsHookExUnhookWindowsHookExCallNextHookEx函数安装、卸载、调用下一个勾子

    // 导入 user32.dll 库中的 SetWindowsHookEx 函数
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, uint dwThreadId);

    // 导入 user32.dll 库中的 UnhookWindowsHookEx 函数
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool UnhookWindowsHookEx(IntPtr hhk);

    // 导入 user32.dll 库中的 CallNextHookEx 函数
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

    // 定义钩子过程的委托类型
    delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);

    // 定义钩子 ID
    const int WH_KEYBOARD_LL = 13;

    // 定义钩子过程
    static IntPtr KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
    {
        // 在这里对键盘事件进行处理
        // 例如拦截按键、记录按键等

        // 调用下一个钩子过程
        return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam);
    }

    static void Main()
    {
        // 安装键盘钩子
        IntPtr hookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProc, IntPtr.Zero, 0);

        if (hookHandle != IntPtr.Zero)
        {
            Console.WriteLine("键盘钩子已安装,按任意键退出...");
            Console.ReadKey();
            
            // 卸载钩子
            UnhookWindowsHookEx(hookHandle);
        }
        else
        {
            Console.WriteLine("键盘钩子安装失败");
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_33790894/article/details/131721512