程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#中定義熱鍵

C#中定義熱鍵

編輯:關於C語言
以一個Form程序為例

public class Form1 : Form

{
            private void Form1_Load(object sender, System.EventArgs e)
            {
                        SetHotKey(false, false, false, true, Keys.Right, 100); // 設置多個熱鍵
                        SetHotKey(false, false, false, true, Keys.Space, 101);
                        SetHotKey(false, false, false, true, Keys.Up, 102);
                        SetHotKey(false, false, false, true, Keys.Down, 103);
            }
            
            private bool key_Ctrl = false;
            private bool key_Shift = false;
            private bool key_Alt = false;
            private bool key_Windows = false;
            private Keys   key_other;

            public void SetHotKey(bool bCtrl,bool bShift,bool bAlt,bool  bWindows,Keys nowKey,int keyId)
            {
                  try
                  {
                        this.key_Alt = bAlt;
                        this.key_Ctrl = bCtrl;
                        this.key_Shift = bShift;
                        this.key_Windows = bWindows;
                        this.key_other = nowKey;
           
                        WinHotKey.KeyModifiers modifier = WinHotKey.KeyModifIErs.None;
           
                        if( this.key_Ctrl )
                              modifier |= WinHotKey.KeyModifIErs.Control;
                        if(this.key_Alt )
                              modifier |= WinHotKey.KeyModifIErs.Alt;
                        if(this.key_Shift)
                              modifier |= WinHotKey.KeyModifIErs.Shift;
                        if(this.key_Windows)
                              modifier |= WinHotKey.KeyModifIErs.Windows;
           
                        WinHotKey.RegisterHotKey(Handle,keyId,modifIEr,nowKey);
                  }
                  catch
                  {
                        MessageBox.Show ("快捷鍵定義錯誤!");
                  }
            }
           
            protected override void WndProc(ref Message msg )
            {
                  const int WM_HOTKEY =  0x0312; // 熱鍵消息

                  if (msg.Msg != WM_HOTKEY)
                  {
                        base.WndProc(ref msg);
                  }
                  else
                  {
                                  //激活熱鍵,在此可以添加處理程序
                                  if (100 == (int)msg.WParam)
                                  {
                                              //DOSomething
                                  }
                                  else if (101 == (int)msg.WParam)
                                  {
                                              //DOSomething
                                  }
                                  else if (102 == (int)msg.WParam)
                                  {
                                              //DOSomething
                                  }
                                  else
                                  {
                                              //DOSomething
                                  }
                  }
           }

            private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            {
                WinHotKey.UnregisterHotKey(Handle, 100);      // 注銷熱鍵
                WinHotKey.UnregisterHotKey(Handle, 101);      // 注銷熱鍵
                WinHotKey.UnregisterHotKey(Handle, 102);      // 注銷熱鍵
                WinHotKey.UnregisterHotKey(Handle, 103);      // 注銷熱鍵
            }

public class WinHotKey
{
            public WinHotKey()
            {
           
            }

            [DllImport("user32.dll",SetLastError=true)]
            public static extern bool RegisterHotKey(
                  IntPtr hWnd,
                  int id,
                  KeyModifiers fsModifIErs,
                  Keys vk
                  );

            [DllImport("user32.dll",SetLastError=true)]
            public static extern bool UnregisterHotKey(
                  IntPtr hWnd,
                  int id
                  );

            [Flags()]
            public enum KeyModifIErs
            {
                  None = 0,
                  Alt = 1,
                  Control =2,
                  Shift = 4,
                  Windows = 8
            }

}

這樣就完成了熱鍵的定義

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved