Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# findwindow

作者:无法言喻   发布日期:2025-11-20   浏览:89

using System;
using System.Runtime.InteropServices;

class Program
{
    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    static void Main()
    {
        // 指定窗口类名和窗口标题
        string className = null; // 如果不知道类名,可以传入null
        string windowTitle = "无标题 - 记事本"; // 这里以记事本为例

        // 调用FindWindow函数查找窗口句柄
        IntPtr hWnd = FindWindow(className, windowTitle);

        if (hWnd == IntPtr.Zero)
        {
            Console.WriteLine("未找到指定窗口。");
        }
        else
        {
            Console.WriteLine("找到了窗口,句柄为: " + hWnd);
        }
    }
}

解释说明:

  1. DllImport:使用 DllImport 属性导入 Windows API 函数 FindWindow,该函数位于 user32.dll 中。
  2. FindWindow 函数:此函数用于根据窗口类名和窗口标题查找窗口的句柄(HWND)。如果找到匹配的窗口,则返回其句柄;否则返回 IntPtr.Zero
  3. 参数说明
    • lpClassName:窗口类名。如果不确定类名,可以传入 null
    • lpWindowName:窗口标题。需要确保提供的标题与目标窗口的标题完全匹配。
  4. 示例代码逻辑
    • 通过调用 FindWindow 查找具有特定标题的窗口(例如记事本)。
    • 如果找到窗口,则输出其句柄;否则输出未找到窗口的信息。

希望这段代码和解释对你有帮助!

上一篇:c#去掉最后一个字符

下一篇:c# 对象转json

大家都在看

c# 二进制

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

c#网络编程

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站