纯粹在扯 最近老看到这样的帖子 其实在C#中实现起来非常简单 因为MS为我们提供了一个现成的控件 NotifyIcon有了它就像使用Button一样简单ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.chs/CPref17/html/T_System_Windows_Forms_NotifyIcon.htmusing System; using System.Drawing; using System.Windows.Forms;public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.NotifyIcon notifyIcon1; private System.Windows.Forms.ContextMenu contextMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.ComponentModel.IContainer components; [STAThread] static void Main() { Application.Run(new Form1()); } public Form1() { this.components = new System.ComponentModel.Container(); this.contextMenu1 = new System.Windows.Forms.ContextMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); // Initialize contextMenu1 this.contextMenu1.MenuItems.AddRange( new System.Windows.Forms.MenuItem[] {this.menuItem1}); // Initialize menuItem1 this.menuItem1.Index = 0; this.menuItem1.Text = "E&xit"; this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click); // Set up how the form should be displayed. this.ClientSize = new System.Drawing.Size(292, 266); this.Text = "Notify Icon Example"; // Create the NotifyIcon. this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); // The Icon property sets the icon that will appear // in the systray for this application. notifyIcon1.Icon = new Icon("appicon.ico"); // The ContextMenu property sets the menu that will // appear when the systray icon is right clicked. notifyIcon1.ContextMenu = this.contextMenu1; // The Text property sets the text that will be displayed, // in a tooltip, when the mouse hovers over the systray icon. notifyIcon1.Text = "Form1 (NotifyIcon example)"; notifyIcon1.Visible = true; // Handle the DoubleClick event to activate the form. notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick); } protected override void Dispose( bool disposing ) { // Clean up any components being used. if( disposing ) if (components != null) components.Dispose(); base.Dispose( disposing ); } private void notifyIcon1_DoubleClick(object Sender, EventArgs e) { // Show the form when the user double clicks on the notify icon. // Set the WindowState to normal if the form is minimized. if (this.WindowState == FormWindowState.Minimized) this.WindowState = FormWindowState.Normal; // Activate the form. this.Activate(); } private void menuItem1_Click(object Sender, EventArgs e) { // Close the form, which closes the application. this.Close(); } }
经常可以看到这样的程序,点了关闭按扭后程序并没有关闭,而是最小化到了系统拖盘处,就像网易泡泡,MSN之类的程序。实现这个其实很简单,捕捉窗体关闭时的消息就可以了。代码如下:
protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_CLOSE = 0xF060;
if (m.Msg == WM_SYSCOMMAND && (int) m.WParam == SC_CLOSE)
{
MessageBox.Show("用户点了关闭按纽了");
return;
}
base.WndProc(ref m);
}
代码添加进窗体代码就可以
2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标。
3.添加窗体最小化事件
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState==FormWindowState.Minimized)
{
this.Hide();
this.notifyIcon1.Visible=true;
}
}
4.添加点击图标事件 private void notifyIcon1_Click(object sender, EventArgs e)
{
this.Visible = true; this.WindowState = FormWindowState.Normal; this.notifyIcon1.Visible = false;
}
这样就可以了,已经通过程序验证,c#2005。
俺的意思是當運行exe文件時.不顯示主窗口,而是隱藏在右下角.
yaoshuwen() 不好意思,我看不懂你的代碼.
我的意思是..編譯好的程序不有一個exe文件嗎?我雙擊exe文件,即運行程序時,程序啟動後主窗口為關閉狀態.
雙擊托盤圖標時才開啟主窗口.
最近老看到这样的帖子
其实在C#中实现起来非常简单
因为MS为我们提供了一个现成的控件
NotifyIcon有了它就像使用Button一样简单ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.chs/CPref17/html/T_System_Windows_Forms_NotifyIcon.htmusing System;
using System.Drawing;
using System.Windows.Forms;public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.ComponentModel.IContainer components; [STAThread]
static void Main()
{
Application.Run(new Form1());
} public Form1()
{
this.components = new System.ComponentModel.Container();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem(); // Initialize contextMenu1
this.contextMenu1.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] {this.menuItem1}); // Initialize menuItem1
this.menuItem1.Index = 0;
this.menuItem1.Text = "E&xit";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click); // Set up how the form should be displayed.
this.ClientSize = new System.Drawing.Size(292, 266);
this.Text = "Notify Icon Example"; // Create the NotifyIcon.
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); // The Icon property sets the icon that will appear
// in the systray for this application.
notifyIcon1.Icon = new Icon("appicon.ico"); // The ContextMenu property sets the menu that will
// appear when the systray icon is right clicked.
notifyIcon1.ContextMenu = this.contextMenu1; // The Text property sets the text that will be displayed,
// in a tooltip, when the mouse hovers over the systray icon.
notifyIcon1.Text = "Form1 (NotifyIcon example)";
notifyIcon1.Visible = true; // Handle the DoubleClick event to activate the form.
notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick); } protected override void Dispose( bool disposing )
{
// Clean up any components being used.
if( disposing )
if (components != null)
components.Dispose(); base.Dispose( disposing );
} private void notifyIcon1_DoubleClick(object Sender, EventArgs e)
{
// Show the form when the user double clicks on the notify icon. // Set the WindowState to normal if the form is minimized.
if (this.WindowState == FormWindowState.Minimized)
this.WindowState = FormWindowState.Normal; // Activate the form.
this.Activate();
} private void menuItem1_Click(object Sender, EventArgs e) {
// Close the form, which closes the application.
this.Close();
}
}
...
protected override void OnActivated(EventArgs e)
{
if (windowCreate)
{
base.Visible = false;
windowCreate = false;
} base.OnActivated(e);
}private void notifyIcon1_DoubleClick(object sender, EventArgs 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();
}我從網上找到的.好使
m.Msg是消息号
m.WParam是消息字段
我们平时关闭、最大化等等就是这些消息
{
this.Visible = false;
}这个可以实现运行.exe程序时最小化到右下角,自己解决双击显示窗口吧