热键注册类

 1 using System;
 2 using System.Runtime.InteropServices;
 3 using System.Windows.Forms;
 4   
 5 namespace test
 6 {
 7      /// <summary>
 8      /// 热键类
 9      /// </summary>
10      public class HotKey
11      {
12          /// <summary>
13          /// 如果函数执行成功,返回值不为0,如果执行失败,返回值为0
14          /// </summary>
15          /// <returns></returns>
16          [DllImport("user32.dll", SetLastError = true)]
17          public static extern bool RegisterHotKey(
18              IntPtr hWnd,           // 窗口的句柄, 当热键按下时,会产生WM_HOTKEY信息,该信息该会发送该窗口句柄
19              int id,                      // 定义热键ID,属于唯一标识热键的作用
20              uint fsModifiers,            // 热键只有在按下Alt、 Ctrl、Shift、Windows等键时才会生效,即才会产生WM_HOTKEY信息
21              Keys vk                   // 虚拟键,即按了Alt+Ctrl+ X ,X就是代表虚拟键
22              );
23 
24         [DllImport("user32.dll", SetLastError = true)]
25          public static extern bool UnregisterHotKey(
26              IntPtr hWnd,                   // 窗口句柄
27              int id                         // 要取消热键的ID
28              );
29      }
30 } 

文本 (2)测试方法:

 1 public partial class Form1 : Form
 2          {
 3 
 4             [Flags]
 5              public enum KeyModifiers
 6              { //定义热键值字符串(热键值是系统规定的,不能改变)
 7                  None = 0,
 8                  Alt = 1,
 9                  Ctrl = 2,
10                  Shift = 4,
11                  WindowsKey = 8
12              }
13 
14             public Form1()
15              {
16                  InitializeComponent();
17              }
18 
19             //窗体加载时-注册快捷键
20              private void Form1_Load(object sender, EventArgs e)
21              {
22                  uint ctrlHotKey = (uint)(KeyModifiers.Alt | KeyModifiers.Ctrl);
23                  // 注册热键为Alt+Ctrl+A, "100"为唯一标识热键
24                  HotKey.RegisterHotKey(Handle, 100, ctrlHotKey, Keys.A);
25              }
26 
27             //截图按钮
28              private void button1_Click(object sender, EventArgs e)
29              {
30                  if (this.WindowState != FormWindowState.Minimized)
31                  {
32                      this.WindowState = FormWindowState.Minimized;
33                      Thread.Sleep(200);
34                  }
35                  int swidth = Screen.PrimaryScreen.Bounds.Width;
36                  int sheight = Screen.PrimaryScreen.Bounds.Height;
37                  Bitmap btm = new Bitmap(swidth, sheight); //空图与屏幕同大小
38                  Graphics g = Graphics.FromImage(btm); //空图的画板
39                  g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(swidth, sheight)); //将屏幕内容复制到空图
40                  Cutter cutter = new Cutter(btm); //传送截图
41                  cutter.FormBorderStyle = FormBorderStyle.None; //截图全屏,无边框
42                  cutter.BackgroundImage = btm; //新的窗体截图做背景
43                  cutter.Show();
44              }
45              private void tiaoZ(object sender, ElapsedEventArgs e)
46              {
47              }
48 
49 
50              //窗体关闭-取消热键
51              private void Form1_FormClosing(object sender, FormClosingEventArgs e)
52              {
53                  HotKey.UnregisterHotKey(Handle, 100);
54              }
55 
56             //快捷键按下执行的事件
57              private void GlobalKeyProcess()
58              {
59                  this.WindowState = FormWindowState.Minimized;
60                  Thread.Sleep(200);
61                  button1.PerformClick();
62              }
63 
64             //重写。监视系统消息,调用对应方法
65              protected override void WndProc(ref Message m)
66              {
67                  const int WM_HOTKEY = 0x0312;
68                  //如果m.Msg的值为0x0312(我也不知道为什么是0x0312)那么表示用户按下了热键
69                  switch (m.Msg)
70                  {
71                      case WM_HOTKEY:
72                          if (m.WParam.ToString().Equals("100"))
73                          {
74                              GlobalKeyProcess();
75                          }
76                          //todo  其它热键
77                          break;
78                  }
79                  // 将系统消息传递自父类的WndProc
80                  base.WndProc(ref m);
81              }
82          }

 

=================

在网上查资料,觉得可能有用。没有实测测试,留着以后使用。

posted @ 2020-02-27 22:01  snsnet  阅读(160)  评论(0编辑  收藏  举报