怎么给工具栏中的按钮设置快捷键?

解决方案 »

  1.   

    在快捷键名字中输入:
    文件(&F)
    F就是快捷键
      

  2.   

    设置text属性为:&V
    显示: (V)
    使用:Alt+V
      

  3.   

    我要设置的快捷键为Ctrl + F3要怎么办?
    菜单项中有个属性shortcutkeys可以为菜单项设置,如果工具栏也想这样要怎么办?
      

  4.   

    那就需要注册了  
    using System;  
    using System.Collections.Generic;  
    using System.Text;  
    using System.Runtime.InteropServices;  
    using System.Windows.Forms;  
     
    namespace WindowsApplication1  
    {  
        public class HotKey  
        {  
            //如果函数执行成功,返回值不为0。  
            //如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。  
            [DllImport("user32.dll", SetLastError = true)]  
            public static extern bool RegisterHotKey(  
                IntPtr hWnd,                //要定义热键的窗口的句柄  
                int id,                     //定义热键ID(不能与其它ID重复)             
                KeyModifiers fsModifiers,   //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效  
                Keys vk                     //定义热键的内容  
                );  
     
            [DllImport("user32.dll", SetLastError = true)]  
            public static extern bool UnregisterHotKey(  
                IntPtr hWnd,                //要取消热键的窗口的句柄  
                int id                      //要取消热键的ID  
                );  
     
            //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)  
            [Flags()]  
            public enum KeyModifiers  
            {  
                None = 0,  
                Alt = 1,  
                Ctrl = 2,  
                Shift = 4,  
                WindowsKey = 8  
            }  
     
        }  
    } 调用方式一:
    using System;  
    using System.Collections.Generic;  
    using System.ComponentModel;  
    using System.Data;  
    using System.Drawing;  
    using System.Text;  
    using System.Windows.Forms;  
     
    namespace WindowsApplication1  
    {  
        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
            }  
     
            private void Form1_Load(object sender, EventArgs e)  
            {  
                //注册热键Shift+S,Id号为100。HotKey.KeyModifiers.Shift也可以直接使用数字4来表示。  
                HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.Shift, Keys.S);  
                //注册热键Ctrl+B,Id号为101。HotKey.KeyModifiers.Ctrl也可以直接使用数字2来表示。  
                HotKey.RegisterHotKey(Handle, 101, HotKey.KeyModifiers.Ctrl, Keys.B);  
                //注册热键Ctrl+Alt+D,Id号为102。HotKey.KeyModifiers.Alt也可以直接使用数字1来表示。  
                HotKey.RegisterHotKey(Handle, 102, HotKey.KeyModifiers.Alt | HotKey.KeyModifiers.Ctrl, Keys.D);  
                //注册热键F5,Id号为103。  
                HotKey.RegisterHotKey(Handle, 103, HotKey.KeyModifiers.None, Keys.F5);  
            }  
     
            protected override void WndProc(ref Message m)  
            {  
                const int WM_HOTKEY = 0x0312;  
                //按快捷键   
                switch (m.Msg)  
                {  
                    case WM_HOTKEY:  
                        switch (m.WParam.ToInt32())  
                        {  
                            case 100:    //按下的是Shift+S  
                                this.Text = "按下的是Shift+S";  
                                break;  
                            case 101:    //按下的是Ctrl+B  
                                //此处填写快捷键响应代码  
                                this.Text = "按下的是Ctrl+B";  
                                break;  
                            case 102:    //按下的是Alt+D  
                                //此处填写快捷键响应代码  
                                this.Text = "按下的是Ctrl+Alt+D";  
                                break;  
                            case 103:  
                                this.Text = "F5";  
                                break;  
                        }  
                        break;  
                }  
                base.WndProc(ref m);  
            }  
        }  
    }  调用方式二:
     public Form1()
            {
                InitializeComponent();
            }
            private void ProcessHotkey(Message m) //按下设定的键时调用该函数
            {
                IntPtr id = m.WParam; //IntPtr用于表示指针或句柄的平台特定类型
                //MessageBox.Show(id.ToString());
                string sid = id.ToString();
                switch (sid)
                {
                    case "100":
                        MessageBox.Show("调用A函数");
                        break;
                    case "200":
                        MessageBox.Show("调用B函数");
                        break;
                }
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                //Handle为当前窗口的句柄,继续自Control.Handle,Control为定义控件的基类
                //RegisterHotKey(Handle, 100, 0, Keys.A); //注册快捷键,热键为A
                //RegisterHotKey(Handle, 100, KeyModifiers.Alt | KeyModifiers.Control, Keys.B);//这时热键为Alt+CTRL+B
                //RegisterHotKey(Handle, 100, 1, Keys.B); //1为Alt键,热键为Alt+B
                RegisterHotKey(Handle, 100, 2,Keys.A); //定义热键为Alt+Tab,这里实现了屏幕系统Alt+Tab键
                RegisterHotKey(Handle, 200, 2, Keys.B); //注册2个热键,根据id值100,200来判断需要执行哪个函数
            }
     
            private void button1_Click(object sender, EventArgs e) //重新设置热键
            {
                UnregisterHotKey(Handle, 100);//卸载快捷键
                RegisterHotKey(Handle, 100, 2, Keys.C); //注册新的快捷键,参数0表示无组合键
            }
     
            private void Form1_FormClosing(object sender, FormClosingEventArgs e) //退出程序时缷载热键
            {
                UnregisterHotKey(Handle, 100);//卸载第1个快捷键
                UnregisterHotKey(Handle, 200); //缷载第2个快捷键
            }
     
            //重写WndProc()方法,通过监视系统消息,来调用过程
            protected override void WndProc(ref Message m)//监视Windows消息
            {
                const int WM_HOTKEY = 0x0312;//如果m.Msg的值为0x0312那么表示用户按下了热键
                switch (m.Msg)
                {
                    case WM_HOTKEY:
                        ProcessHotkey(m);//按下热键时调用ProcessHotkey()函数
                        break;
                }
                base.WndProc(ref m); //将系统消息传递自父类的WndProc
            }
    辅助键说明:None = 0,
    Alt = 1,
    crtl= 2,   
    Shift = 4,
    Windows = 8//如果有多个辅助键则,例如 alt+crtl是3  直接相加就可以了。