1. 判断窗口是否存在

     private bool IsWindowExist(IntPtr handle)
{
return (!(GetWindow(new HandleRef(this, handle), ) != IntPtr.Zero) && IsWindowVisible(new HandleRef(this, handle)));
} [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetWindow(HandleRef hWnd, int uCmd); [DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool IsWindowVisible(HandleRef hWnd);

2. 获取窗口句柄

     /// <summary>
/// 获取应用程序窗口句柄
/// </summary>
/// <param name="processId"></param>
/// <returns></returns>
private IntPtr GetWindowHandle(int processId)
{
var windowHandle = IntPtr.Zero;
EnumThreadWindowsCallback windowsCallback = new EnumThreadWindowsCallback(FindMainWindow);
EnumWindows(windowsCallback, IntPtr.Zero);
//保持循环
GC.KeepAlive(windowsCallback); bool FindMainWindow(IntPtr handle, IntPtr extraParameter)
{
int num;
GetWindowThreadProcessId(new HandleRef(this, handle), out num);
if (num == processId && IsWindowExist(handle))
{
windowHandle = handle;
return true;
}
return false;
} return windowHandle;
}
public delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);

3. 关闭应用窗口

根据进程Id关闭应用窗口:

     /// <summary>
/// 关闭程序主窗口
/// </summary>
/// <param name="processId">进程ID</param>
public void CloseWindow(int processId)
{
var mwh = GetWindowHandle(processId);
SendMessage(mwh, MW_CLOSE, , );
}
const int MW_CLOSE = 0x0010; [DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

关闭所有此进程名的窗口:

         public void CloseAllWindows(string processName)
{
Process[] processList = Process.GetProcessesByName(processName);
foreach (Process process in processList)
{
CloseMainWindow(process.Id);
}
}

当然,直接杀进程,是最快的方法:

     /// <summary>
/// 关闭所有进程
/// </summary>
/// <param name="processName"></param>
/// <returns></returns>
public static bool CloseAllProcesses(string processName)
{
try
{
Process[] psEaiNotes = Process.GetProcessesByName(processName);
foreach (Process psEaiNote in psEaiNotes)
{
psEaiNote.Kill();
}
}
catch
{
return false;
} return true;
}

4. 重启程序

     string appFileName = currentClientProcess.MainModule.FileName;

     Process newClient = new Process();
newClient.StartInfo.FileName = appFileName;
// 设置要启动的进程的初始目录
newClient.StartInfo.WorkingDirectory = System.Windows.Forms.Application.ExecutablePath; //启动程序
currentClientProcess.Kill();
newClient.Start();

窗口之间发送/接收消息的处理,请参考《C# 跨进程通信

C# 通过进程名/进程Id 操作窗口/程序的更多相关文章

  1. 根据进程ID查找运行程序目录

    查看进程ID [root@hadoop03 openresty]# netstat -nltp 进入/proc目录查找相应进程ID目录并进入此目录 [root@hadoop03 usr]# cd /p ...

  2. QT_获取正在运行程序的进程id(判断程序是否正在运行)

    bool checkProcessRunning(const QString &processName, QList<quint64> &listProcessId) { ...

  3. C++ Windows 下 根据进程名获取进程ID 以及该进程下所有窗口的句柄

    #include <windows.h> #include <stdint.h> #include <tlhelp32.h> #include <stdio. ...

  4. 调用window Api 进行对特定窗口的进程ID进行操作

    /// <summary> /// 获取窗口句柄 /// </summary> /// <param name="lpClassName">&l ...

  5. 显示所有APP的进程详细信息(进程ID、进程所在UID、进程占用内存、进程名)

    main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...

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

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

  7. C#获取运行程序的进程ID

    C#获取运行程序的进程ID [DllImport("User32.dll", CharSet = CharSet.Auto)] public static extern int G ...

  8. 如何通过进程名获取进程ID

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何通过进程名获取进程ID.

  9. Linux/UNIX编程:获取指定用户所有正在运行的进程ID和进程名

    先用系统函数 `getpwnam` 获得指定用户名的 UID,然后遍历 /proc/ 中所有 PID 目录,如果 /proc/PID/status 中的 UID 是输入用户名对应的 UID 则输出该 ...

随机推荐

  1. Android系统架构及内核简介

    (来源于ThinkPHP) Android是Google公司开发的基于Linux平台的开源手机操作系统,它包括操作系统.中间件.用户界面和应用程序,而且不存在任何以往阻碍移 动产业创新的专利权障碍,并 ...

  2. codeforces 13 b

    给你三根线段判段是否组成了A 条件,两条线段交于端点并且夹角不大于90,第三条线段端点在两条线段上并且划分的大小满足 大:小<4:1 注释很全.(主要是我记不清楚了,,好像过了一个多星期了) # ...

  3. C++实验一

    实验结论 2-28 if...else #include <iostream> #include <stdlib.h> using namespace std; int mai ...

  4. 文件上传控件bootstrap-fileinput的使用

    1.插件下载地址:https://github.com/kartik-v/bootstrap-fileinput 2.插件的引用 需要引用jquery 需要结合bootstrap使用,即页面需要引入b ...

  5. 安卓开发学习笔记(三):Android Stuidio无法引用Intent来创建对象,出现cannot resolve xxx

    笔者在进行安卓开发时,发现自己的代码语法完全没有问题.尤其是创建intent对象的时候,语法完全是正确的,但是Android Stuidio却显示报错,Intent类显示为红色,如图所示: 代码如下所 ...

  6. 2018,你与 i 春秋的故事都在这

    年终岁末,深思回顾,过去的一年我们共同创造了很多回忆,有欢乐,有感动,更有收获.回首2018年,伴随着激情与挑战,我们共创了很多佳绩,一起来看看吧. 课程&实验 2018新增原创录制实战视频课 ...

  7. Android 音视频开发(七): 音视频录制流程总结

    在前面我们学习和使用了AudioRecord.AudioTrack.Camera.MediaExtractor.MediaMuxer API.MediaCodec. 学习和使用了上述的API之后,相信 ...

  8. [Swift]LeetCode24. 两两交换链表中的节点 | Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3 ...

  9. [Swift]LeetCode159.具有最多两个不同字符的最长子串 $ Longest Substring with At Most Two Distinct Characters

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

  10. [Swift]LeetCode377. 组合总和 Ⅳ | Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...