关于让程序成为托盘区图标后的问题

来源:百度知道 编辑:UC知道 时间:2024/05/30 22:24:10
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.ToString() == "F9")
{
MessageBox.Show("");
}
}

在上面代码中,按F9后会弹出一个messagebox
可为什么当窗体成为托盘区图标后再按F9就不行了呀?
有什么方法可以使程序成为托盘区图标后按F9同样可以弹出messagebox呀?
会的人请解答!

您好,

出现这种问题是因为窗体在最小化到系统托盘的时候就失去了输入焦点,因此无法接收到按键消息,您可以使用RegisterHotKey API函数注册全局系统热键。

您可以参考以下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplicationTestHotKey
{
public class Form1 : Form
{
[DllImport("user32.dll", SetLastError = true)]
private static extern bool RegisterHotKey(IntPtr HWnd, int id, uint fsModifiers, uint vk);

[DllImport("user32.dll", SetLastError = true)]
private static extern bool UnregisterHotKey(IntPtr HWnd, int id);

private const uint ALT = 0x0001;
private const uint CONTROL = 0x0002;
private const uint SHIFT = 0x0004;
private const uint WINKEY = 0x000;