如何解决C#Winforms最小化焦点时接受键盘输入
我正在编写C#桌面应用程序,当我与discord / skype / teamspeak等玩smtng时,它允许我使热键上的麦克风静音。因此,我需要我的应用程序接受我的热键(例如:ctrl + B)没有我的情况下alt +退出我的游戏。
public partial class Form1 : Form
{
private const int APPCOMMAND_MICROPHONE_VOLUME_MUTE = 0x180000;
private const int APPCOMMAND_VOLUME_MUTE = 0x80000;
private const int WM_APPCOMMAND = 0x319;
[DllImport("user32.dll")]
public static extern IntPtr SendMessageW(IntPtr hWnd,int Msg,IntPtr wParam,IntPtr lParam);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,EventArgs e)
{
return;
}
private void button1_Click(object sender,EventArgs e)
{
SendMessageW(this.Handle,WM_APPCOMMAND,this.Handle,(IntPtr)APPCOMMAND_VOLUME_MUTE);
}
protected override bool ProcessCmdKey(ref Message msg,Keys keyData) {
if (keyData == (Keys.Control | Keys.B)) {
SendMessageW(this.Handle,(IntPtr)APPCOMMAND_VOLUME_MUTE);
}
return base.ProcessCmdKey(ref msg,keyData);
}
private void Form1_Resize_1(object sender,EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}
}
private void notifyIcon1_MouseDoubleClick(object sender,MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
}
我的表单只是一个测试环境,它有1个用于打开/关闭麦克风的按钮,它读取Ctrl + B热键以打开/关闭麦克风,并在最小化时进入托盘。
所以,主要要点是:当我的应用程序最小化并且我的应用程序不能从当前正在使用的程序(游戏)中窃取焦点时,我需要它接受Ctrl + B组合键。
解决方法
感谢史蒂夫的答案 Set global hotkeys using C#
using System.Windows.Forms;
using System;
using System.Diagnostics;
using System.Threading;
using System.Drawing.Printing;
// 1. Import the InteropServices type
using System.Runtime.InteropServices;
namespace Sandbox
{
public partial class Form1 : Form
{
// 2. Import the RegisterHotKey Method
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd,int id,int fsModifiers,int vlc);
public Form1()
{
InitializeComponent();
// 3. Register HotKey
// Set an unique id to your Hotkey,it will be used to
// identify which hotkey was pressed in your code to execute something
int UniqueHotkeyId = 1;
// Set the Hotkey triggerer the F9 key
// Expected an integer value for F9: 0x78,but you can convert the Keys.KEY to its int value
// See: https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
int HotKeyCode = (int)Keys.F9;
// Register the "F9" hotkey
Boolean F9Registered = RegisterHotKey(
this.Handle,UniqueHotkeyId,0x0000,HotKeyCode
);
// 4. Verify if the hotkey was succesfully registered,if not,show message in the console
if (F9Registered)
{
Console.WriteLine("Global Hotkey F9 was succesfully registered");
}
else
{
Console.WriteLine("Global Hotkey F9 couldn't be registered !");
}
}
private void Form1_Load(object sender,EventArgs e)
{
}
protected override void WndProc(ref Message m)
{
// 5. Catch when a HotKey is pressed !
if (m.Msg == 0x0312)
{
int id = m.WParam.ToInt32();
// MessageBox.Show(string.Format("Hotkey #{0} pressed",id));
if (id == 1)
{
MessageBox.Show("F9 Was pressed !");
}
}
base.WndProc(ref m);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。