如何为Button设定热键?? 在WinForm中如何为一个Button设定热键(如Ctrl-A或F5),当按下热键产生Button的点击事件?? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你可以在button的text属性中加入"(&f)"f随便了,,这样就可以用alt+f了 首先,创建一个WinHotKey类,如下public class 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 } public WinHotKey() { // // TODO: 在此处添加构造函数逻辑 // }}然后,在程序中这样调用//快捷键定义 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) { 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,100,modifier,nowKey); } catch { //login.ShowMessage("快捷键定义错误!"); } } //激活热键 protected override void WndProc(ref Message m ) { const int WM_HOTKEY = 0x0312; switch(m.Msg) { case WM_HOTKEY: { //如果有新消息,弹出消息 if( ReceiveNewMessage == true) { for(int i=0;i<this.manInforList.Count;i++) { ManInfor searchMan = (ManInfor)this.manInforList[i]; if( searchMan.manInforID.Equals( getFriendID)) { searchMan.Clicked = true; searchMan.P2PShow(); break; } } } else { this.Show(); this.TopMost = true; this.panel_Main.Refresh(); this.WindowState = System.Windows.Forms.FormWindowState.Normal; } } break; } base.WndProc(ref m ); } 楼上已经很完整了 如果仅仅是像菜单那样 设一个ALT+X 就在TEXT属性加就是了要是像F几这样 或者一个单独热键就要设置 HOTKEY了 Tomgus(小桥流水) 通知已经告诉了答案。在下也没有什么可说的了。建议结贴。 判断客户端与服务器是否在同一局域网? String 转 int32 求助C#的异常处理的问题 asp.net得程序c#.net中不能用 按纽的名称能否用变量表示,具体如下 请教C#的安装问题 求救:如何将WINDOWS应用程序集成在WEB应用页内,以解决WINDOWS应用程序的发布问题!谢谢!急!在线等! Unicode与汉字互转 winform 中如何让checkedListBox选项横排 比较复杂的功能,如何实现呢?望大师指点!!谢谢 提一个小问题。 encoding="GBK"的XML文件, 读取出错,请问要怎样读呢??
public class 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
} public WinHotKey()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}然后,在程序中这样调用
//快捷键定义
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)
{
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,100,modifier,nowKey);
}
catch
{
//login.ShowMessage("快捷键定义错误!");
}
} //激活热键
protected override void WndProc(ref Message m )
{
const int WM_HOTKEY = 0x0312;
switch(m.Msg)
{
case WM_HOTKEY:
{
//如果有新消息,弹出消息
if( ReceiveNewMessage == true)
{
for(int i=0;i<this.manInforList.Count;i++)
{
ManInfor searchMan = (ManInfor)this.manInforList[i];
if( searchMan.manInforID.Equals( getFriendID))
{
searchMan.Clicked = true;
searchMan.P2PShow();
break;
}
}
}
else
{
this.Show();
this.TopMost = true;
this.panel_Main.Refresh();
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
}
}
break;
}
base.WndProc(ref m );
}
如果仅仅是像菜单那样 设一个ALT+X 就在TEXT属性加就是了要是像F几这样 或者一个单独热键就要设置 HOTKEY了