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() == )

        {

            objWnd = processWnd[uiPid];

            if (objWnd != null)

            {

                ptrWnd = (IntPtr)objWnd;

            }

        }

        return ptrWnd;

    }

    private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)

    {

        uint uiPid = ;

        if (GetParent(hwnd) == IntPtr.Zero)

        {

            GetWindowThreadProcessId(hwnd, ref uiPid);

            if (uiPid == lParam)    // 找到进程对应的主窗口句柄

            {

                processWnd[uiPid] = hwnd;   // 把句柄缓存起来

                SetLastError();    // 设置无错误

                return false;   // 返回 false 以终止枚举窗口

            }

        }

        return true;

    }

}

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

http://www.cnblogs.com/qiuyi21/articles/1302732.html

[转载]C#获取进程的主窗口句柄的更多相关文章

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

    通过调用Win32 API实现. public class User32API { private static Hashtable processWnd = null; public delegat ...

  2. Delphi 通得进程ID获取主窗口句柄

    只知道进程ID,获取主窗口句柄的方法如下: 通过EnumWindows枚举所有窗口 使用GetWindowThreadProcessID,通过窗口句柄获取进程ID 比便获取的进程ID与当前已知的进程I ...

  3. C# 获取进程或线程的相关信息

    信息来自: http://blog.163.com/kunkun0921@126/blog/static/169204332201293023432113/ using System; using S ...

  4. C#根据进程名称获取进程的句柄?

    C#根据进程名称获取进程的句柄或C#如何获取其他进程的句柄? 有时候标题名是动态变化的,所以不使用FindWindow方法! [StructLayout(LayoutKind.Sequential)] ...

  5. Bash Shell 获取进程 PID

    转载地址:http://weyo.me/pages/techs/linux-get-pid/ 导读 Linux 的交互式 Shell 与 Shell 脚本存在一定的差异,主要是由于后者存在一个独立的运 ...

  6. CMD魔法堂:获取进程路径和PID值的方法集

    一.前言    在开发发布更更新工具——更新Weblogic应用模块时,了解到更新Weblogic应用需要先关闭Weblogic应用窗口然后是清缓存.更新应用文件,最后再重启Weblogic应用窗口. ...

  7. C++获取进程号及窗口

    #include <TlHelp32.h> //根据进程名获取进程ID BOOL GetPidByProcessName(TCHAR *pProcess, DWORD*dwPid) { H ...

  8. 写一个Windows上的守护进程(8)获取进程路径

    写一个Windows上的守护进程(8)获取进程路径 要想守护某个进程,就先得知道这个进程在不在.我们假设要守护的进程只会存在一个实例(这也是绝大部分情形). 我是遍历系统上的所有进程,然后判断他们的路 ...

  9. C#依据进程名称获取进程的句柄?

    C#依据进程名称获取进程的句柄或C#怎样获取其它进程的句柄? 有时候标题名是动态变化的,所以不使用FindWindow方法! [StructLayout(LayoutKind.Sequential)] ...

随机推荐

  1. 多个相同script引用探索

    1.页面直接就有,或者document.write页面加载同步输出 其实就是当script是页面初始加载的一部分的情况,script是同步的,只有在上一个加载并执行完才会进行下一个script加载. ...

  2. bootstrap-fileinput初体验

    原文:http://www.htmleaf.com/html5/html5muban/201505091801.html Document & Demo: http://plugins.kra ...

  3. SQL 建表与查询 HTML计算时间差

    create database xue1 go --创建数据库 use xue1 go --引用数据库 create table xinxi ( code int, name ), xuehao ), ...

  4. App性能提升方法

    总体思路:精简请求数 1.css sprit 图像拼合,将所有可拼接的所有图像拼接为一整张图像,然后再利用css中的position定位来处理,降低图片的请求数 2.懒加载:只渲染客户端用户可见区域[ ...

  5. zynq 之u-boot

    u-boot 启动文件分析 u-boot首先执行的文件:C:\Users\summer_spinach\Desktop\zynq_linux相关\linux\uboot内核\u-boot-xarm\a ...

  6. 可以支持jQuery1.10.1 的 fancybox 1.3.4, 並現在type為Ajax時,也可以定義窗口的大小。

    官網上的 fancybox 1.3.4 太老了,不支持jQuery1.10.1,改動了一下源碼,現在可以支持了. type為Ajax時,也可以定義窗口的大小. $("#ajaxlink&qu ...

  7. 为什么要用ajax

    Ajax应用程序的优势在于:1. 通过异步模式,提升了用户体验2. 优化了浏览器和服务器之间的传输,减少不必要的数据往返,减少了带宽占用3. Ajax引擎在客户端运行,承担了一部分本来由服务器承担的工 ...

  8. php中如何防止表单的重复提交

    在php中如何防止表单的重复提交?其实也有几种解决方法. 下面小编就为大家介绍一下吧.需要的朋友可以过来参考下 代码: <?php /* * php中如何防止表单的重复提交 * by www.j ...

  9. 重拾C,一天一点点_6

    break与continuecontinue只能用于循环语句goto最常见的用法是终止程序在某些深度嵌套的结构中的处理过程,例如一次跳出两层或多层循环.break只能从最内层循环退出到上一级的循环. ...

  10. How to understand ReferenceGroup control on Form[AX2012]

    在AX2012的Form开发中,微软引入了新的控件ReferenceGroup,它用在Lookup其他表RecId的时候显示更人性化的字段,它的使用还必须从表的索引说起.AX2012的表有这些索引(h ...