C# winform 程序在后台运行 点击快捷键使程序做出相应反应。
发布网友
发布时间:2022-04-21 14:47
我来回答
共4个回答
懂视网
时间:2022-04-21 19:08
网页制作 将一个网站的网页下载下来后进行修改,为什么点击确定键没有响应??我只需要它在浏览器上打开状态栏或地址栏有响应,即使报错也行
热心网友
时间:2022-04-21 17:34
//初始化函数里加入以下两条就可以用了,我现在定义的是ALT+X与ALT+S
//这两种组合键,你自己可以改
public Frmain()
{
InitializeComponent();
RegisterHotKey(this.Handle, 10, MODKEY.ALT, Keys.X);
RegisterHotKey(this.Handle, 11, MODKEY.ALT, Keys.S);
}
protected override void WndProc(ref Message m)
{
if(m.Msg == 0x0312)
{
switch (m.WParam.ToInt32())
{
case 10:
退出ToolStripMenuItem_Click(null,null);
break;
case 11:
notifyIcon1_Click(null,null);
break;
default:
break;
}
return;
}
base.WndProc(ref m);
}
//要定义热键的窗口的句柄
//定义热键ID(不能与其它ID重复)int id,
//标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效KeyModifiers fsModifiers, Keys vk //定义热键的内容
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr wnd, int id, MODKEY mode, Keys vk);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr wnd, int id);
[Flags()]
public enum MODKEY
{
None = 0,
ALT = 0x0001,
CTRL = 0x0002,
SHIFT = 0x0004,
WIN = 0x0008,
}
刚好写完现成的。还热着呢,给你了,试过可用了。不过不能开同个窗口或是与这热键有冲突的程序。要不然会有其中的一个程序会没有响应的
热心网友
时间:2022-04-21 19:09
热键是在界面不在后台的情况下使用的 你应该是用windows消息回调函数来判断按下了组合键没有
1、向Windows窗口发送Alt组合键
先看WM_SYSKEYDOWN的help:The WM_SYSKEYDOWN message is posted to the window with the keyboard focus when the user holds down the ALT key and then presses another key. It also occurs when no window currently has the keyboard focus; in this case, the WM_SYSKEYDOWN message is sent to the active window. The window that receives the message can distinguish between these two contexts by checking the context code in the lKeyData parameter.WM_SYSKEYDOWN nVirtKey = (int) wParam; // virtual-key code lKeyData = lParam; // key data ParametersnVirtKeyValue of wParam. Specifies the virtual-key code of the key being pressed. lKeyDataValue of lParam. Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table:Value Description0-15 Specifies the repeat count. The value is the number of times the keystroke is repeated as a result of the user holding down the key.16-23 Specifies the scan code. The value depends on the original equipment manufacturer (OEM).24 Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.25-28 Reserved; do not use.29 Specifies the context code. The value is 1 if the ALT key is down while the key is pressed; it is 0 if the WM_SYSKEYDOWN message is posted to the active window because no window has the keyboard focus.30 Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is 0 if the key is up.31 Specifies the transition state. The value is always 0 for a WM_SYSKEYDOWN message.之前曾经修改过keyData的16-23位为VK_MENU,第30位参数为1,但没效果请看位29的说明!!The value is 1 if the ALT key is down while the key is pressed; 当值为1时表示ALT键被按下!这不正是我需要的吗?于是把29位设置为1,函数调用变成PostMessage(hWnd,WM_SYSKEYDOWN,0x41,1<<29);经过测试,发现这个就是Alt+A的效果!!追问太乱了。。。。看不清。。。
追答http://blog.csdn.net/pjl1119/article/details/6914902