Loading
0

C#设置热键(快捷键)实现隐藏和显示窗体的方法
被墙跳转TG:@qianhenetwork QQ 851617266

301免备案跳转微信公众号
腾讯云服务器大促销。
华为服务器

实现目的:按住某组合键时,窗体隐藏或显示,本教程代码为完整代码,直接复制即可使用,

1、创建一个类,类名命名为 Hotkey 在类中写入如下代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Runtime.InteropServices;
  9. using System.Collections;
  10.  
  11. namespace WindowsFormsApplication1
  12. {
  13. public delegate void HotkeyEventHandler(int HotKeyID);
  14.  
  15.  
  16.  
  17. public class Hotkey : System.Windows.Forms.IMessageFilter
  18. {
  19. Hashtable keyIDs = new Hashtable();
  20. IntPtr hWnd;
  21.  
  22. static public int Hotkey1;
  23.  
  24. public event HotkeyEventHandler OnHotkey;
  25.  
  26. public enum KeyFlags
  27. {
  28. MOD_ALT = 0x1,
  29. MOD_CONTROL = 0x2,
  30. MOD_SHIFT = 0x4,
  31. MOD_WIN = 0x8
  32. }
  33. [DllImport("user32.dll")]
  34. public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk);
  35.  
  36. [DllImport("user32.dll")]
  37. public static extern UInt32 UnregisterHotKey(IntPtr hWnd, UInt32 id);
  38.  
  39. [DllImport("kernel32.dll")]
  40. public static extern UInt32 GlobalAddAtom(String lpString);
  41.  
  42. [DllImport("kernel32.dll")]
  43. public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);
  44.  
  45. public Hotkey(IntPtr hWnd)
  46. {
  47. this.hWnd = hWnd;
  48. Application.AddMessageFilter(this);
  49. }
  50.  
  51. public int RegisterHotkey(Keys Key, KeyFlags keyflags)
  52. {
  53. UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString());
  54. RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);
  55. keyIDs.Add(hotkeyid, hotkeyid);
  56. return (int)hotkeyid;
  57. }
  58.  
  59. public void UnregisterHotkeys()
  60. {
  61. Application.RemoveMessageFilter(this);
  62. foreach (UInt32 key in keyIDs.Values)
  63. {
  64. UnregisterHotKey(hWnd, key);
  65. GlobalDeleteAtom(key);
  66. }
  67. }
  68.  
  69. public bool PreFilterMessage(ref System.Windows.Forms.Message m)
  70. {
  71. if (m.Msg == 0x312)
  72. {
  73. if (OnHotkey != null)
  74. {
  75. foreach (UInt32 key in keyIDs.Values)
  76. {
  77. if ((UInt32)m.WParam == key)
  78. {
  79. OnHotkey((int)m.WParam);
  80. return true;
  81. }
  82. }
  83. }
  84. }
  85. return false;
  86. }
  87. }
  88. }

2、在窗体中创建过程,并在加载时间中写入代码,完整代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18.  
  19. public void OnHotkey(int HotkeyID) //alt+z隐藏窗体,再按显示窗体。
  20. {
  21. if (HotkeyID == Hotkey.Hotkey1)
  22. {
  23. if (this.Visible == true)
  24. this.Visible = false;
  25. else
  26. this.Visible = true;
  27. }
  28. else
  29. {
  30. this.Visible = false;
  31. }
  32. }
  33.  
  34. private void Form1_Load(object sender, EventArgs e)
  35. {
  36. Hotkey hotkey;
  37. hotkey = new Hotkey(this.Handle);
  38. Hotkey.Hotkey1 = hotkey.RegisterHotkey(System.Windows.Forms.Keys.Z, Hotkey.KeyFlags.MOD_ALT); //定义快键(alt+z)
  39. hotkey.OnHotkey += new HotkeyEventHandler(OnHotkey);
  40. }
  41. }
  42. }


 
301免备案跳转微信公众号
华为服务器
腾讯云服务器大促销。

声明:站长码字很辛苦啊,转载时请保留本声明及附带文章链接:https://www.zfcdn.xyz/showinfo-23-363-0.html
亲爱的:被墙域名跳转TG:@qianhenetwork QQ:851617266,可否收藏+评论+分享呢?

最后编辑于:2019-01-13 16:39:04作者:

上一篇:C#通过百度Ai接口实现图片文字识别核心代码
下一篇:C# winform中一个类中如何调用另一个窗体的控件或方法