原文:http://www.cnblogs.com/oraclejava/articles/1549025.html

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);

[DllImport("User32.dll", EntryPoint = "SendMessage")]

private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

Process[] processes;

const int WM_CLOSE = 0x0010;

public void GetCurrentWindowHandle()

{

Microsoft.Win32.RegistryKey oRegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Amada Software");

string[] subKeys = oRegistryKey.GetSubKeyNames();

IntPtr ptrWnd = IntPtr.Zero;

foreach (string subkey in subKeys)

{

processes = Process.GetProcessesByName(subkey);

foreach (Process proc in processes)

{

uint uiPid = (uint)proc.Id;

object objWnd = processWnd[uiPid];

if (objWnd != null)

{

ptrWnd = (IntPtr)objWnd;

if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd))

{

SendMessage(ptrWnd, WM_CLOSE, 0, 0);

}

else

{

ptrWnd = IntPtr.Zero;

}

}

bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);

if (!bResult && Marshal.GetLastWin32Error() == 0)

{

objWnd = processWnd[uiPid];

if (objWnd != null)

{

ptrWnd = (IntPtr)objWnd;

SendMessage(ptrWnd, WM_CLOSE, 0, 0);

}

}

}

}

}

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;

}

}

return true;

}

}

C# 获取窗口句柄并且关闭应用程序的更多相关文章

  1. VC中获取窗口句柄的各种方法

    AfxGetMainWndAfxGetMainWnd获取自身窗口句柄HWND hWnd = AfxGetMainWnd()->m_hWnd; GetTopWindow函数功能:该函数检查与特定父 ...

  2. C#-WinForm-跨窗体 构造函数传值 及应用—登录式窗口传值、如何关闭主页面时关闭应用程序、如何打开唯一窗口—★★★★★五星重量级

    构造函数可以传任意类型的值,并可以同时传多个值 结构函数传值的初步应用--简单的登陆式界面 现在我有两个窗体Form3和Form4,如下,如何点击Form3中的按钮后,打开Form4并将Form3中的 ...

  3. C#-WinForm-★★★★★跨窗体 构造函数传值 及应用—登录式窗口传值、如何关闭主页面时关闭应用程序、如何打开唯一窗口★★★★★

    构造函数可以传任意类型的值,并可以同时传多个值 结构函数传值的初步应用——简单的登陆式界面 现在我有两个窗体Form3和Form4,如下,如何点击Form3中的按钮后,打开Form4并将Form3中的 ...

  4. C# 最基本的涉及模式(单例模式) C#种死锁:事务(进程 ID 112)与另一个进程被死锁在 锁 | 通信缓冲区 资源上,并且已被选作死锁牺牲品。请重新运行该事务,解决方案: C#关闭应用程序时如何关闭子线程 C#中 ThreadStart和ParameterizedThreadStart区别

    C# 最基本的涉及模式(单例模式) //密封,保证不能继承 public sealed class Xiaohouye    { //私有的构造函数,保证外部不能实例化        private  ...

  5. [WinAPI] 获取窗口句柄的几种方法

    1.使用FindWindow函数获取窗口句柄 示例:使用FindWindow函数获取窗口句柄,然后获得窗口大小,并且移动窗口到指定位置. 我们想获得酷我音乐盒的窗口句柄并移动它,该怎么办呢? 首先打开 ...

  6. QT5笔记:关闭应用程序和窗口的函数

    23333 QT一坨,求一门面向傻瓜的语言. QT中 quit(),exit()以及close():常用的三个槽   对主程序的退出,可以调用成员函数exit(),同时也可以调用槽quit(),二者此 ...

  7. Win7 远程桌面 错误代码:5 异常处理(您的远程桌面会话即将结束 此计算机的虚拟内存可能不足。请关闭其他程序,然后重试连接远程计算机。如果问题仍然存在,请联系网络管理员或技术支持。)

    问题表现: 在用windows7 远程桌面连接其他电脑时,出现错误提示对话框—-标题为“严重错误(错误代码:5)”,内容为“您的远程桌面会话即将结束 此计算机的虚拟内存可能不足.请关闭其他程序,然后重 ...

  8. WPF关闭应用程序方法

    很多人认为关闭应用程序应该很简单,例如WindowsForm里一个Application.Exit();方法就可以解决问题,但在WPF里面可别滥用,因为WPF里Application类没有该方法,倒是 ...

  9. JAVA文件中获取路径及WEB应用程序获取路径方法

    JAVA文件中获取路径及WEB应用程序获取路径方法 1. 基本概念的理解 `绝对路径`:你应用上的文件或目录在硬盘上真正的路径,如:URL.物理路径 例如: c:/xyz/test.txt代表了tes ...

随机推荐

  1. nginx windows 安装为服务.

    安装Nginx 下载windows版nginx (http://nginx.org/download/nginx-1.10.0.zip),之后解压到需要放置的位置(D:\xampp\nginx) 将N ...

  2. 4KM

    ip addr add 10.9.8.100/24 broadcast + dev eth0 /etc/sysconfig/network-scripts/ifcfg-eth0 ifdown eth0 ...

  3. Android开发多媒体应用之SoundPool的使用的代码

    内容过程中,把写内容过程中比较好的内容段记录起来,下面的内容是关于Android开发多媒体应用之SoundPool的使用的内容,希望对各位也有用途. public class MainActivity ...

  4. GIF图制作

    一.安装image 首先在cmd中敲入代码pip install imageio,以便制作动图 二.安装完之后便可读取gif了 在idle中输入代码 import imageio savename = ...

  5. Windows下代替自带cmd的开源软件cmder

    cmder cmder是一个增强型命令行工具,不仅可以使用windows下的所有命令,更爽的是可以使用linux的命令,shell命令 下载 Cmder官网 下载的时候,会有两个版本,分别是mini与 ...

  6. python web开发小结

    书籍 <python基础教程> <流畅的python> web框架 flask django tornado ORM sqlalchemy orator 消息队列 celery ...

  7. PHP Variable handling 函数

    Variable handling 函数: boolval — 获取变量的布尔值debug_zval_dump — 将内部zend值的字符串表示转储为输出doubleval — floatval 的别 ...

  8. 【CSS】 布局之圣杯布局

    在看众多大神的css布局指南时,经常看到一个布局:圣杯布局(也有称为双飞翼布局的).今天我们也来剖析一下. 其实,对于众多css布局,我们只要明确理解了3种技术,那么基本上大多数布局都难不倒我们了: ...

  9. <a>标签里面嵌图片<img>下面出现一小段空白的原因

    今天做项目的时候,发现在a标签,里面嵌入<img>会出现空白 css 内容: a{ border:1px solid black; } img{ width:200px; } html内容 ...

  10. c# Time类

    直接上代码 public KBehaviour() { //间隔时间 System.Timers.Timer t = ); t.Elapsed += new System.Timers.Elapsed ...