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. Java_Math/Date

    这篇文章我们来研究下java中两个常用的工具类,Math和Date Math: Math是java中各种函数计算集合类,进入到Math类中,可以看到Math是java.lang包下的,被final修饰 ...

  2. lodash 实现一些常见的功能

    排序 const sorted = _.orderBy(filtered, [sortColumn.path], [sortColumn.order]); 数组切片 普通的 slice 可传递两个参数 ...

  3. eclipse上的maven,添加依赖后无法自动下载相应的jar包

    报错信息: Failed to read artifact descriptor for org.quartz-scheduler:quartz-jobs:jar:2.2.3  org.eclipse ...

  4. 第一次app经验

    第一次做一个app 发现 需要和前端沟通好而且 还要注意细节  效果图细节不要忘记 尽量多穿数据不要少传数据 而且 对接 注意细节

  5. python获取函数注释 __doc__

    使用 help  函数 可以查看 函数的注释内容 但是它也有点"添油加醋" 其实函数的注释被保存在 __doc__属性里面  PS 双下划线 def f(): "&quo ...

  6. c# 钩子类

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  7. Shadow Copying导致ASP.NET应用启动很慢的解决办法

    What's Shadow Copying? 我们安装一个应用程序并启动后,我们是无法更新应用程序安装目录中程序集文件的.如果强制替换会提示文件正在使用,如下图所示. 那你可能会问,为什么会无法更新呢 ...

  8. 微信小程序setData()方法的详解以及对数组/json操作

    此篇文章是本人对setData方法的一些理解,是查阅文档和查找一些其他资料综述的,有所不足希望指正! 直接进入正题! 一.setData()方法: 1.参数接受一个对象,以key,value的形式表示 ...

  9. [Swift]LeetCode190. 颠倒二进制位 | Reverse Bits

    Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 ...

  10. [Swift]LeetCode398. 随机数索引 | Random Pick Index

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...