当前位置: 首页 > news >正文

C#进程管理程序

1.功能

鼠标点到应用图标,显示该进程号,并可以强制关闭此进程

2.程序

using System; using System.Diagnostics; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; namespace ProcessKiller { public partial class MainForm : Form { // Windows API 导入 [DllImport("user32.dll")] private static extern IntPtr WindowFromPoint(Point point); [DllImport("user32.dll")] private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId); [DllImport("user32.dll")] private static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder text, int count); [DllImport("user32.dll")] private static extern int GetWindowTextLength(IntPtr hWnd); [DllImport("user32.dll")] private static extern bool IsWindowVisible(IntPtr hWnd); [DllImport("user32.dll")] private static extern IntPtr GetShellWindow(); // 控件声明 private Label lblStatus; private Button btnKill; private Button btnRefresh; private ListBox lstProcesses; private Timer timer; private uint selectedProcessId = 0; public MainForm() { InitializeComponent(); StartMouseHook(); } private void InitializeComponent() { this.Text = "进程终结者 - 点击图标显示PID并强制关闭"; this.Size = new Size(400, 500); this.StartPosition = FormStartPosition.CenterScreen; this.TopMost = true; // 状态标签 lblStatus = new Label() { Location = new Point(12, 12), Size = new Size(360, 50), Text = "将鼠标移动到任意窗口或任务栏图标上...", BackColor = Color.LightYellow, BorderStyle = BorderStyle.FixedSingle }; // 进程列表 lstProcesses = new ListBox() { Location = new Point(12, 70), Size = new Size(360, 300), Font = new Font("Consolas", 9) }; // 关闭按钮 btnKill = new Button() { Text = "强制关闭选中的进程", Location = new Point(12, 380), Size = new Size(170, 40), BackColor = Color.IndianRed, Enabled = false }; btnKill.Click += BtnKill_Click; // 刷新进程列表按钮 btnRefresh = new Button() { Text = "刷新进程列表", Location = new Point(202, 380), Size = new Size(170, 40), BackColor = Color.LightBlue }; btnRefresh.Click += BtnRefresh_Click; // 定时器用于实时更新鼠标下的进程信息 timer = new Timer(); timer.Interval = 200; // 200毫秒检测一次 timer.Tick += Timer_Tick; this.Controls.Add(lblStatus); this.Controls.Add(lstProcesses); this.Controls.Add(btnKill); this.Controls.Add(btnRefresh); } private void StartMouseHook() { timer.Start(); } private void Timer_Tick(object sender, EventArgs e) { Point cursorPos = Control.MousePosition; IntPtr hWnd = WindowFromPoint(cursorPos); if (hWnd != IntPtr.Zero) { // 获取窗口标题 int textLen = GetWindowTextLength(hWnd); string windowTitle = ""; if (textLen > 0) { System.Text.StringBuilder sb = new System.Text.StringBuilder(textLen + 1); GetWindowText(hWnd, sb, sb.Capacity); windowTitle = sb.ToString(); } // 获取进程ID GetWindowThreadProcessId(hWnd, out uint pid); // 更新状态栏 if (pid > 0) { try { Process proc = Process.GetProcessById((int)pid); string processName = proc.ProcessName; lblStatus.Text = $"当前指向: {windowTitle} (窗口句柄: {hWnd})\n进程名: {processName} PID: {pid}"; selectedProcessId = pid; btnKill.Enabled = true; } catch { lblStatus.Text = $"指向对象无法获取进程信息 (PID: {pid})"; selectedProcessId = 0; btnKill.Enabled = false; } } else { lblStatus.Text = $"指向: {windowTitle} (无法获取进程ID)"; selectedProcessId = 0; btnKill.Enabled = false; } } } private void BtnKill_Click(object sender, EventArgs e) { if (selectedProcessId == 0) { MessageBox.Show("没有选中的进程", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } try { Process processToKill = Process.GetProcessById((int)selectedProcessId); DialogResult result = MessageBox.Show( $"确定要强制关闭进程 \"{processToKill.ProcessName}\" (PID: {selectedProcessId}) 吗?\n\n这将立即终止该进程及其所有线程。", "确认关闭", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result == DialogResult.Yes) { processToKill.Kill(); processToKill.WaitForExit(1000); MessageBox.Show($"进程 {processToKill.ProcessName} (PID: {selectedProcessId}) 已被终止。", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); selectedProcessId = 0; btnKill.Enabled = false; BtnRefresh_Click(null, null); // 刷新列表 } } catch (Exception ex) { MessageBox.Show($"无法终止进程: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); selectedProcessId = 0; btnKill.Enabled = false; } } private void BtnRefresh_Click(object sender, EventArgs e) { lstProcesses.Items.Clear(); Process[] processes = Process.GetProcesses(); foreach (Process proc in processes) { try { string title = string.IsNullOrEmpty(proc.MainWindowTitle) ? "[无窗口]" : proc.MainWindowTitle; lstProcesses.Items.Add($"{proc.ProcessName,-30} PID:{proc.Id,-8} 窗口:{title}"); } catch { lstProcesses.Items.Add($"{proc.ProcessName,-30} PID:{proc.Id,-8} [无法获取窗口信息]"); } } lstProcesses.Items.Add("----------------------------------------------"); lstProcesses.Items.Add("提示: 将鼠标移动到任务栏图标或窗口上即可检测"); } protected override void OnFormClosed(FormClosedEventArgs e) { timer.Stop(); base.OnFormClosed(e); } } static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } } }

3.使用方法与功能说明:

  1. 实时检测:程序运行后,将鼠标移动到任何窗口标题栏或任务栏图标上,状态栏会立即显示该窗口的标题、所属进程名称和进程ID (PID)。

  2. 强制关闭

    • 当鼠标指向一个可识别的进程窗口时,窗口底部的“强制关闭选中的进程”按钮会被启用。

    • 点击该按钮后,程序会弹出确认对话框,确认后将立即终止该进程。

    • 注意:关闭系统关键进程(如explorer.exe、csrss.exe等)可能导致系统不稳定,请谨慎操作。

  3. 进程列表:右侧的进程列表显示了当前系统中所有正在运行的进程及其PID,方便您查看和选择。

4.核心API说明:

  • WindowFromPoint:获取鼠标当前位置的窗口句柄。

  • GetWindowThreadProcessId:通过窗口句柄获取对应的进程ID。

  • Process.Kill():立即终止进程(强制关闭)。

http://www.jsqmd.com/news/1006382/

相关文章:

  • Grammarly for VS Code深度解析:技术原理与实战应用指南
  • 2026年 南京瓦面防水厂家推荐排行榜:老房翻新/别墅屋面/沥青瓦专用防水品牌深度解析与选购指南! - 品牌发掘
  • Windows 11 LTSC 一键安装微软商店终极指南:3分钟恢复完整应用生态
  • 【JAVA毕设源码分享】基于springboot的手机数码售卖系统的设计与实现(程序+文档+代码讲解+一条龙定制)
  • AppleRa1n深度指南:iOS 15-16激活锁绕过终极解决方案
  • 2026年6月最新|专业滚筒输送机制造厂家 专注输送设备研发 技术实力雄厚 - 商业新知
  • 保姆级教程:用Python+OpenCV搞定Intel Realsense D435深度视频录制与H5格式保存
  • 深入解析NXP LS1046A AXI时序检查机制:从总线延迟监控到SoC性能优化
  • 官方最新发布|武汉市智工职业技术学校2026年招生简章 - 善良的阿良
  • Python多线程如何操作全局变量:从踩坑到最佳实践
  • PX4无人机集群控制:新手也能快速搭建多机协同系统
  • 免费开源音乐播放器终极指南:5分钟掌握LX Music桌面版
  • 郑州翡翠回收靠谱门店 TOP 榜|2026 实测避坑指南 - 讯息早知道
  • 3个技巧让Windows电脑变身手游神器:APK安装器终极指南
  • T5-Base:重新定义NLP任务的通用文本转换引擎
  • 5个场景告诉你为什么BepInEx是Unity游戏插件框架的终极选择
  • 3分钟搞定Windows安卓应用安装:告别模拟器的极简方案
  • 社交图网络聚类:从关系结构到用户分群的工程实践
  • 指纹浏览器内存泄漏的梦魇:成百上千实例并发的内存优化与生命周期管理
  • 如何永久保存微信聊天记录?WeChatMsg完全指南帮你轻松实现数据自由
  • MMDS0508仿真器:嵌入式调试中的实时总线分析与硬件断点实战
  • MC68377 BIM模块详解:寄存器映射、引脚配置与低功耗管理
  • Simulink仿真避坑指南:搞定BPSK/BFSK/BASK解调中的滤波器设计与比较器阈值
  • 3分钟快速掌握OpenIM企业级即时通讯系统:从零开始搭建私有化聊天平台
  • 如何快速掌握so-vits-svc:AI人声转换与角色混合的终极指南
  • 3个突破性功能:让Windows直接运行安卓应用的革命性方案
  • MC9S08LL64 GPIO与KBI模块配置实战:从寄存器到低功耗设计
  • 如何用OBS源独立录制插件实现多轨录制?3个高效工作流彻底改变你的视频制作体验
  • 深入解析OpenCore Legacy Patcher:让旧款Mac焕发新生的技术实践
  • 不止于计算:用Python模拟莱布尼茨级数,可视化理解π的收敛过程(Matplotlib版)