我有一个在后台运行的应用程序,就像我可以将我的应用程序保留在系统托盘中一样。如果它保留在系统托盘上,我的应用程序将完成它的工作。每当用户按 F10 或 F9 时,都会完成一些工作。我试过这个:
public partial class Form1 : Form
{
public int a = 1;
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
[DllImport("User32.dll")]
private static extern short GetAsyncKeyState(System.Int32 vKey);
const int MYACTION_HOTKEY_ID = 1;
public Form1()
{
InitializeComponent();
RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 0, (int) Keys.F9);
RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 0, (int)Keys.F10);
this.ShowInTaskbar = false;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID && (GetAsyncKeyState(Keys.F9) == -32767))
{
if ((a % 2) != 0)
{
a++;
MessageBox.Show(a.ToString()+"not equal F9");
label1.Text = "not equal F9";
}
if ((a % 2) == 0)
{
a++;
MessageBox.Show(a.ToString()+"equal F9");
label1.Text = " equal F9";
}
}
else if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID && (GetAsyncKeyState(Keys.F10) == -32767))
{
if ((a % 2) != 0)
{
a++;
MessageBox.Show(a.ToString() + "not equal F10");
label1.Text = "not equal F10";
}
if ((a % 2) == 0)
{
a++;
MessageBox.Show(a.ToString() + "equal F10");
label1.Text = " equal F10";
}
}
base.WndProc(ref m);
}
}
当我使用 set "this.ShowInTaskbar = false" 这一行时,它不起作用。但如果我不设置它,它工作正常。对于我的应用程序,我必须使用这一行。我该如何解决这个问题????