新闻详情

新闻详情

首页 / 资讯中心 / 详情

C#: 托盘+热键+截图

发布时间:2026/10/2 7:02:26来源:尧图网络
C#: 托盘+热键+截图
1.隐藏的窗口接收到热键消息截图using System; using System.Runtime.InteropServices; using System.Windows.Forms; public class HotKeyWindow : NativeWindow { // 导入 Win32 API [DllImport(user32.dll, SetLastError true)] private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); [DllImport(user32.dll, SetLastError true)] private static extern bool UnregisterHotKey(IntPtr hWnd, int id); [DllImport(user32.dll, SetLastError true)] private static extern IntPtr CreateWindowEx( uint dwExStyle, string lpClassName, string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam); // 热键消息 private const int WM_HOTKEY 0x0312; private int _hotkeyId; public event EventHandler HotKeyPressed; public HotKeyWindow() { // 创建一个隐藏的静态窗口作为热键消息的接收器 IntPtr hWnd CreateWindowEx(0, STATIC, , 0, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); this.AssignHandle(hWnd); } public bool RegisterHotKey(int id, uint modifiers, Keys key) { _hotkeyId id; uint vk (uint)key; bool success RegisterHotKey(this.Handle, id, modifiers, vk); if (!success) { // 获取错误码方便排查1409 表示热键已被占用 int error Marshal.GetLastWin32Error(); Console.WriteLine(注册热键失败错误码: error); } return success; } public void UnregisterHotKey() { if (this.Handle ! IntPtr.Zero) { UnregisterHotKey(this.Handle, _hotkeyId); } } protected override void WndProc(ref Message m) { base.WndProc(ref m); // 务必调用基类保证消息链正常 if (m.Msg WM_HOTKEY) { if (HotKeyPressed ! null) { HotKeyPressed(this, EventArgs.Empty); } } } public override void DestroyHandle() { UnregisterHotKey(); base.DestroyHandle(); } }2.系统托盘调用窗口设置热键using System; using System.Windows.Forms; using System.Drawing; using System.Diagnostics; using System.Drawing.Imaging; class Program { private static HotKeyWindow _hotkeyWindow; private const uint MOD_CONTROL 0x0002; private const uint MOD_ALT 0x0001; private const uint MOD_SHIFT 0x0004; static void Main() { NotifyIcon NI new NotifyIcon(); NI.Icon new Icon(camera.ico); NI.Text 静默截全屏 Ctrl F12\n保存到桌面; NI.Visible true; NI.DoubleClick (s, e) { NI.ShowBalloonTip(1000, 提示, 通知演示, ToolTipIcon.Info); }; ToolStripMenuItem menuItem0 new ToolStripMenuItem(网址); menuItem0.Click (s, e) { Process.Start(https://www.gitlink.org.cn/sonichy); }; ToolStripMenuItem menuItem1 new ToolStripMenuItem(关于); menuItem1.Click (s, e) { MessageBox.Show(海天鹰截图1.0\n一款基于C#的极简热键静默截图。, 关于, MessageBoxButtons.OK, MessageBoxIcon.Information); }; ToolStripMenuItem menuItem2 new ToolStripMenuItem(退出); menuItem2.Click (s, e) { Application.Exit(); NI.Dispose(); //图标立即消失 }; NI.ContextMenuStrip new ContextMenuStrip(); NI.ContextMenuStrip.Items.Add(menuItem0); NI.ContextMenuStrip.Items.Add(menuItem1); NI.ContextMenuStrip.Items.Add(menuItem2); _hotkeyWindow new HotKeyWindow(); bool ok _hotkeyWindow.RegisterHotKey(1, MOD_CONTROL, Keys.F12); if (ok) { _hotkeyWindow.HotKeyPressed (s, e) { CaptureFullScreen(); }; } else { MessageBox.Show(热键注册失败可能已被占用。, 错误, MessageBoxButtons.OK, MessageBoxIcon.Error); } Application.Run(); } static void CaptureFullScreen() { Rectangle bounds Screen.PrimaryScreen.Bounds; Bitmap bitmap new Bitmap(bounds.Width, bounds.Height); using (Graphics g Graphics.FromImage(bitmap)) { g.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size); } string filename Environment.GetFolderPath(Environment.SpecialFolder.Desktop) / DateTime.Now.ToString(yyyyMMddHHmmss) .jpg; bitmap.Save(filename, ImageFormat.Jpeg); Clipboard.SetImage(bitmap); } }3.版本信息using System.Reflection; [assembly:AssemblyTitle(一款基于C#的极简热键静默截图。)] [assembly:AssemblyProduct(HTYShot)] [assembly:AssemblyCompany(海天鹰工作室)] [assembly:AssemblyTrademark(HTY)] [assembly:AssemblyCopyright(Copyright 2026)] // 程序集的版本信息由下面四个值组成: // // 主版本 // 次版本 // 内部版本号 // 修订号 // // 可以指定所有这些值也可以使用“内部版本号”和“修订号”的默认值 // 方法是按如下所示使用“*”: [assembly:AssemblyVersion(1.0.0.0)]4.编译C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc /target:winexe /out:HTYShot.exe /win32icon:camera.ico notifyIcon.cs hotKeyWindow.cs AssemblyInfo.cs5.运行HTYShot
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

VC++6.0 MFC入门:消息映射与对话框程序实战指南 2026/10/2 7:50:17

VC++6.0 MFC入门:消息映射与对话框程序实战指南

简介:面向具备一定C/C基础、期望上手VC 6.0与MFC的初学者,这份入门教程文档从宏观学习方法讲到具体的Windows消息机制。文中先介绍学好VC的九条建议:先编写字符界面程序打牢语言基本功,多使用Help Online少依赖参考书,…

阅读更多 →
MWORKS 2025a电气模型库搭建全桥LLC电路仿真实战指南 2026/10/2 7:50:17

MWORKS 2025a电气模型库搭建全桥LLC电路仿真实战指南

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

阅读更多 →
Apple Developer App注册苹果开发者账号及证件验证失败解决方案 2026/10/2 7:50:17

Apple Developer App注册苹果开发者账号及证件验证失败解决方案

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

阅读更多 →
VS2017-MFC串口通信稳定实现:CSerialPort工业级封装详解 2026/10/2 7:50:17

VS2017-MFC串口通信稳定实现:CSerialPort工业级封装详解

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

阅读更多 →
VS Code 新突破:AHP 协议让 AI 智能体自主操作 Dev Container 2026/10/2 7:50:10

VS Code 新突破:AHP 协议让 AI 智能体自主操作 Dev Container

1. 这次更新到底改了什么:从"人操作编辑器"到"智能体操作开发环境"VS Code 最新版本把 AI 智能体接入 Dev Container 这件事,本质上不是加了一个新按钮,而是把"开发环境"从给人用的工具,变成了给智…

阅读更多 →
Mellanox UDA 加速 Hadoop 大数据:从 HDFS 到 Shuffle 的 RDMA 实践 2026/10/2 7:50:10

Mellanox UDA 加速 Hadoop 大数据:从 HDFS 到 Shuffle 的 RDMA 实践

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉