C# 窗體最小化的托盤/系統通知區域(轉)
WPF中怎么將窗口最小化到托盤
來源 http://www.bbniu.com/forum/viewthread.php?tid=67
需要先添加兩個引用:using System.Windows.Forms using System.Drawing;
namespace minwindows
{
using System.Windows.Forms; // NotifyIcon control
using System.Drawing; // Icon
/// <summary>
/// Window1.xaml 的交互邏輯
/// </summary>
public partial class Window1 : Window
{
WindowState ws;
WindowState wsl;
NotifyIcon notifyIcon;
public Window1()
{
InitializeComponent();
//顯示托盤。
icon();
//保證窗體顯示在上方。
wsl = WindowState;
}
private void OnNotifyIconDoubleClick(object sender, EventArgs e)
{
this.Show();
WindowState = wsl;
}
private void Window_StateChanged(object sender, EventArgs e)
{
ws = WindowState;
if(ws==WindowState.Minimized)
{
Hide();
}
}
private void icon()
{
this.notifyIcon = new NotifyIcon();
this.notifyIcon.BalloonTipText = "Hello, NotifyIcon!";
this.notifyIcon.Text = "Hello, NotifyIcon!";
this.notifyIcon.Icon = new System.Drawing.Icon("NotifyIcon.ico");
this.notifyIcon.Visible = true;
notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick;
this.notifyIcon.ShowBalloonTip(1000); }
}
C# 最小化到系統托盤的實現(二)
來源 http://www.rzrgm.cn/linglizeng/archive/2009/06/22/CSharpSystemTray02.html
下面要實現的就是這個對話框:
要求:點擊主面板關閉按鈕時,彈出這個對話框. 其中有個選項"Do not show this dialog again".
本來想用系統對話框作的,可是沒有這個選項,所以就定義一個Form來做.
大家一起來討論一下大家要設計這樣一個對話框的思路吧。語言要求:C#。

1. 新建一個WindowsForm, 在主面板里生成一個對象
QPanCloseDialog NewCloseDialog = new QPanCloseDialog();
2. 定義一個值來確定是否顯示這個對話框.
public static bool boolShowCloseDialog = true;
3.定義退出策略.
//退出策略
private bool DecideToExit()
{
if (boolShowCloseDialog == true)
{
this.NewCloseDialog.ShowDialog();//顯示模態窗口
if (boolCancelCloseQPan == true)
{
return true;
}
else
{
if (boolCloseQPan == true)
{
return false;
}
if (boolCloseQPan == false)
{
this.QPan_MiniMizedToTuoPan();
return true;
}
}
}
else if (boolShowCloseDialog == false)
{
if (boolCancelCloseQPan == true)
{
return true;
}
else
{
if (boolCloseQPan == true)
{
return false;
}
if (boolCloseQPan == false)
{
this.QPan_MiniMizedToTuoPan();
return true;
}
}
}
return true;
}
4.關閉窗口
//所有關閉Form的出口
private void MinimizedToTuoPan(object sender, System.Windows.Forms.FormClosingEventArgs e)
{
if (boolCloseFromExitMenu == true)
{
e.Cancel = false;
}
else
{
e.Cancel = DecideToExit();
}
}
5.設定關閉對話框
//加載時取消“Do not show this dialog againt“.
private void QPanCloseDialog_Load(object sender, EventArgs e)
{
RememberSelection.Checked = false;
}
public bool ExitFromButtom = false;
//點擊“Exit”按鈕
private void ExitDialogExit_Click(object sender, EventArgs e)
{
this.ExitFromButtom = true;
QPan.boolCancelCloseQPan = false;
QPan.boolCloseQPan = true;
this.Close();
}
//點擊“Minimize”按鈕
private void ExitDialogMinimize_Click(object sender, EventArgs e)
{
this.ExitFromButtom = true;
QPan.boolCancelCloseQPan = false;
QPan.boolCloseQPan = false;
this.Close();
}
//點擊“Cancel”按鈕
private void ExitDialogCancel_Click(object sender, EventArgs e)
{
this.ExitFromButtom = true;
QPan.boolShowCloseDialog = true;
QPan.boolCancelCloseQPan = true;
QPan.boolCloseQPan = false;
this.Close();
}
//點擊"X"關閉按鈕
private void QPanCloseDialog_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
{
if (this.ExitFromButtom == false)
{
QPan.boolShowCloseDialog = true;
QPan.boolCancelCloseQPan = true;
QPan.boolCloseQPan = false;
RememberSelection.Checked = false;
}
}
//選擇"Do not show this dialog againt"
private void RememberSelection_CheckedChanged(object sender, EventArgs e)
{
if (RememberSelection.Checked == true)
{
QPan.boolShowCloseDialog = false;
}
else if(RememberSelection.Checked == false)
{
QPan.boolShowCloseDialog = true;
}
}
。。。。。。
具體還有一些細節就不詳述了
浙公網安備 33010602011771號