1. 程式人生 > >c# winform快捷鍵實現

c# winform快捷鍵實現

@value summary display ddc del lower lis delete urn

我們在軟件中經常用到快捷鍵,這裏整理備份一下。

首先我們要定義可以作為快捷鍵的按鍵,以下是整理的 一些,自己可以根據情況來修改

技術分享圖片
  public static Dictionary<int, string> HotKeys = new Dictionary<int, string>() 
        { 
            {48,"0"},  
            {49,"1"},  
            {50,"2"},      
            {51,"3"},     
            {52,"4"
}, {53,"5"}, {54,"6"}, {55,"7"}, {56,"8"}, {57,"9"}, {65,"A"}, {66,"B"}, {67,"C"}, {68,"D"}, {69,"E"}, {70,"F"}, {
71,"G"}, {72,"H"}, {73,"I"}, {74,"J"}, {75,"K"}, {76,"L"}, {77,"M"}, {78,"N"}, {79,"O"}, {80,"P"}, {81,"Q"}, {82
,"R"}, {83,"S"}, {84,"T"}, {85,"U"}, {86,"V"}, {87,"W"}, {88,"X"}, {89,"Y"}, {90,"Z"}, {112,"F1"}, {113,"F2"}, {114,"F3"}, {115,"F4"}, {116,"F5"}, {117,"F6"}, {118,"F7"}, {119,"F8"}, {120,"F9"}, {121,"F10"}, {122,"F11"}, {123,"F12"}, {33,"PageUp"}, {34,"PageDown"}, {35,"End"}, {36,"Home"}, {37,""}, {38,""}, {39,""}, {40,""}, {45,"Insert"}, {46,"Delete"}, {186,";"}, {187,"="}, {188,","}, {189,"-"}, {190,"."}, {191,"/"}, {192,"`"}, {219,"["}, {220,"\\"}, {221,"]"} };
View Code

然後弄個數據表存一下定義功能對應的快捷鍵,表結構如下

技術分享圖片
CREATE TABLE [dbo].[HotKey](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [FrmName] [nvarchar](50) NULL,
    [Sort] [int] NULL,
    [ActionName] [nvarchar](50) NULL,
    [ActionKey] [nvarchar](50) NULL,
    [KeyValue] [nvarchar](50) NULL
) ON [PRIMARY]

GO

EXEC sys.sp_addextendedproperty @name=NMS_Description, @value=N界面 , @level0type=NSCHEMA,@level0name=Ndbo, @level1type=NTABLE,@level1name=NHotKey, @level2type=NCOLUMN,@level2name=NFrmName
GO

EXEC sys.sp_addextendedproperty @name=NMS_Description, @value=N順序 , @level0type=NSCHEMA,@level0name=Ndbo, @level1type=NTABLE,@level1name=NHotKey, @level2type=NCOLUMN,@level2name=NSort
GO

EXEC sys.sp_addextendedproperty @name=NMS_Description, @value=N功能名稱 , @level0type=NSCHEMA,@level0name=Ndbo, @level1type=NTABLE,@level1name=NHotKey, @level2type=NCOLUMN,@level2name=NActionName
GO

EXEC sys.sp_addextendedproperty @name=NMS_Description, @value=N功能編碼 , @level0type=NSCHEMA,@level0name=Ndbo, @level1type=NTABLE,@level1name=NHotKey, @level2type=NCOLUMN,@level2name=NActionKey
GO

EXEC sys.sp_addextendedproperty @name=NMS_Description, @value=N熱鍵 , @level0type=NSCHEMA,@level0name=Ndbo, @level1type=NTABLE,@level1name=NHotKey, @level2type=NCOLUMN,@level2name=NKeyValue
GO
View Code

系統啟動的時候緩存一下設置的快捷鍵

public static List<HotKey> HotKeySetting { get; set; }

基類窗體裏面寫窗體的

寫KeyUP 和KeyDown事件

技術分享圖片
 bool m_blnCtrl = false;
 bool m_blnAlt = false;
 bool m_blnShift = false;

        [Description("定義的熱鍵列表"), Category("自定義")]
        public Dictionary<int, string> HotKeys { get; set; }
        //如果事件返回true則表示已經找到了快捷鍵事件,則不再向下傳遞
        public delegate bool HotKeyEventHandler(string strHotKey);
        /// <summary>
        /// 熱鍵事件
        /// </summary>
        [Description("熱鍵事件"), Category("自定義")]
        public event HotKeyEventHandler HotKeyDown;


 protected void FrmBase_KeyUp(object sender, KeyEventArgs e)
        {
            if (HotKeyDown != null && HotKeys != null)
            {
                if (e.KeyCode == Keys.Control || e.KeyCode == Keys.ControlKey)
                    m_blnCtrl = false;
                if (e.KeyCode == Keys.Alt || e.KeyCode == Keys.Menu)
                    m_blnAlt = false;
                if (e.KeyCode == Keys.Shift || e.KeyCode == Keys.ShiftKey)
                    m_blnShift = false;
            }
        }

        protected void FrmBase_KeyDown(object sender, KeyEventArgs e)
        {
            if (HotKeyDown != null && HotKeys != null)
            {
                if (e.KeyCode == Keys.Control || e.KeyCode == Keys.ControlKey)
                    m_blnCtrl = true;
                if (e.KeyCode == Keys.Alt || e.KeyCode == Keys.Menu)
                    m_blnAlt = true;
                if (e.KeyCode == Keys.Shift || e.KeyCode == Keys.ShiftKey)
                    m_blnShift = true;
                if (HotKeys.ContainsKey(e.KeyValue))
                {
                    string strKey = string.Empty;
                    if (m_blnCtrl)
                    {
                        strKey += "Ctrl+";
                    }
                    if (m_blnAlt)
                    {
                        strKey += "Alt+";
                    }
                    if (m_blnShift)
                    {
                        strKey += "Shift+";
                    }
                    strKey += HotKeys[e.KeyValue];

                    if (HotKeyDown(strKey))
                    {
                        e.Handled = true;
                        e.SuppressKeyPress = true;
                    }
                }
            }
        }            
View Code

你會註意到keyup和kedown事件的修飾符是protected,為什麽要用protected呢?是因為窗體的一些輸入控件的按鍵事件是不會傳遞到窗體的,比如textbox這些,所有需要手動指定一下

子窗體中註冊事件就可以了,如果你沒有用基類窗體,那麽直接寫在一起就可以了

技術分享圖片
this.HotKeys = HotKeys;
            this.HotKeyDown += FrmMain_HotKeyDown;
            //將輸入框的2個事件指向窗體的2個事件
            this.txtSearch.KeyDown += this.FrmBase_KeyDown;
            this.txtSearch.KeyUp += this.FrmBase_KeyUp;

/// <summary>
        /// 熱鍵按下事件
        /// </summary>
        /// <param name="strHotKey">熱鍵值</param>
        /// <returns>當返回true則表示不再向下傳遞按鍵事件,通常當匹配到熱鍵操作後返回true</returns>
        bool FrmMain_HotKeyDown(string strHotKey)
        {
            Console.WriteLine(strHotKey);
            string strHotAction = GetHotKeyAction("FrmMain", strHotKey);
            if (string.IsNullOrEmpty(strHotAction))
                return false;
            else
            {
                //這裏根據返回的功能名稱做操作就可以了
                return true;
            }
        }


public static string GetHotKeyAction(string strFrom, string strHotKey)
        {
            var entity = HotKeySetting.Find(p => p.FrmName.ToLower() == strFrom.ToLower() && p.KeyValue.ToStringExt().ToLower() == strHotKey.ToLower());
            if (entity != null)
                return entity.ActionKey;
            else
                return string.Empty;
        }
View Code

這樣就可以了

c# winform快捷鍵實現