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