C#程序在托盘后台运行:
源代码如下:
请在工具栏把notifyIcon控件拖入窗体再使用:
//隐藏窗体
private bool windowCreate = true;
private void toolStripMenuItem1_Click(object sender, EventArgs e) //这是菜单选项的一个item点击事件
{
if (windowCreate)
{
base.Visible = false;
windowCreate = false;
}
this.Hide();
base.OnActivated(e);
}
//显示回窗体(notifyIcon控件双击事件,注:请选择一个ico图标,这样隐藏后在右下角显示有相关图标)
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (this.Visible == true)
{
this.Hide();
this.ShowInTaskbar = false;
}
else
{
this.Visible = true;
this.ShowInTaskbar = true;
this.WindowState = FormWindowState.Normal;
//this.Show();
this.BringToFront();
windowCreate = true;
}
}
文章来自学IT网:http://www.xueit.com/html/2008-05/117_125_00.html
快捷键显示:
最近找了一些资料,是讲在C#中设置快捷键运行方法或程序的,要设置快捷键必须使用user32.dll下面的两个方法。
HWND hWnd,
int id,
UINT fsModifiers,
UINT vk
);
HWND hWnd,
int id
);
public static extern bool RegisterHotKey(
IntPtr hWnd, // handle to window
int id, // hot key identifier
KeyModifiers fsModifiers, // key-modifier options
Keys vk // virtual-key code
);
public static extern bool UnregisterHotKey(
IntPtr hWnd, // handle to window
int id // hot key identifier
);
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
{
label2.AutoSize = true;
RegisterHotKey(Handle, 100, 0, Keys.F10);
}
{
UnregisterHotKey(Handle, 100);//卸载快捷键
}
{
const int WM_HOTKEY = 0x0312;//按快捷键
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotkey();//调用主处理程序
break;
}
base.WndProc(ref m);
}