C#获取进程的主窗口句柄的实现方法

 更新时间:2013年04月28日 15:39:40   作者:  
C#获取进程的主窗口句柄的实现方法,需要的朋友可以参考一下

通过调用Win32 API实现。

复制代码 代码如下:

public class User32API
{
    private static Hashtable processWnd = null;

    public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);

    static User32API()
    {
        if (processWnd == null)
        {
            processWnd = new Hashtable();
        }
    }

    [DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
    public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);

    [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
    public static extern IntPtr GetParent(IntPtr hWnd);

    [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
    public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);

    [DllImport("user32.dll", EntryPoint = "IsWindow")]
    public static extern bool IsWindow(IntPtr hWnd);

    [DllImport("kernel32.dll", EntryPoint = "SetLastError")]
    public static extern void SetLastError(uint dwErrCode);

    public static IntPtr GetCurrentWindowHandle()
    {
        IntPtr ptrWnd = IntPtr.Zero;
        uint uiPid = (uint)Process.GetCurrentProcess().Id;  // 当前进程 ID
        object objWnd = processWnd[uiPid];

        if (objWnd != null)
        {
            ptrWnd = (IntPtr)objWnd;
            if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd))  // 从缓存中获取句柄
            {
                return ptrWnd;
            }
            else
            {
                ptrWnd = IntPtr.Zero;
            }
        }

        bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
        // 枚举窗口返回 false 并且没有错误号时表明获取成功
        if (!bResult && Marshal.GetLastWin32Error() == 0)
        {
            objWnd = processWnd[uiPid];
            if (objWnd != null)
            {
                ptrWnd = (IntPtr)objWnd;
            }
        }

        return ptrWnd;
    }

    private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
    {
        uint uiPid = 0;

        if (GetParent(hwnd) == IntPtr.Zero)
        {
            GetWindowThreadProcessId(hwnd, ref uiPid);
            if (uiPid == lParam)    // 找到进程对应的主窗口句柄
            {
                processWnd[uiPid] = hwnd;   // 把句柄缓存起来
                SetLastError(0);    // 设置无错误
                return false;   // 返回 false 以终止枚举窗口
            }
        }

        return true;
    }
}

调用User32API.GetCurrentWindowHandle()即可返回当前进程的主窗口句柄,如果获取失败则返回IntPtr.Zero。

--EOF--

相关文章

  • C# Winform实现圆角无锯齿按钮

    C# Winform实现圆角无锯齿按钮

    这篇文章主要为大家详细介绍了C# Winform实现圆角无锯齿按钮,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-07-07
  • C#利用Windows自带gdi32.dll实现抓取屏幕功能实例

    C#利用Windows自带gdi32.dll实现抓取屏幕功能实例

    这篇文章主要介绍了C#利用Windows自带gdi32.dll实现抓取屏幕功能,是C#程序设计中常见的一个重要技巧,需要的朋友可以参考下
    2014-08-08
  • C# winForm自定义弹出页面效果

    C# winForm自定义弹出页面效果

    这篇文章主要为大家详细介绍了C# winForm自定义弹出页面效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • C#使用MSTest进行单元测试

    C#使用MSTest进行单元测试

    这篇文章介绍了C#使用MSTest进行单元测试的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • C#基于WinForm实现串口通讯

    C#基于WinForm实现串口通讯

    这篇文章主要为大家详细介绍了C#基于WinForm实现串口通讯,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • 如何使用C# Stopwatch 测量微秒级精确度

    如何使用C# Stopwatch 测量微秒级精确度

    这篇文章主要介绍了如何使用C# Stopwatch 测量微秒精确度,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03
  • Unity快速生成常用文件夹的方法

    Unity快速生成常用文件夹的方法

    这篇文章主要介绍了Unity快速生成常用文件夹的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-09-09
  • Unity为软件添加使用有效期的具体步骤

    Unity为软件添加使用有效期的具体步骤

    今天小编遇到这样一个需求需要为软件设定一个使用有效期,当超过指定时间后,程序无法执行,实现思路并不复杂,今天小编通过本文给大家分享Unity为软件添加使用有效期的具体步骤,感兴趣的朋友一起看看吧
    2022-03-03
  • C# 常用协议实现模版及FixedSizeReceiveFilter示例(SuperSocket入门)

    C# 常用协议实现模版及FixedSizeReceiveFilter示例(SuperSocket入门)

    本文主要介绍了常用协议实现模版及FixedSizeReceiveFilter示例。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • C#实现将音频PCM数据封装成wav文件

    C#实现将音频PCM数据封装成wav文件

    这篇文章主要为大家详细介绍了C#如何实现将音频PCM数据封装成wav文件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2023-10-10

最新评论