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.cnblogs.com/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;
}
}
。。。。。。
具体还有一些细节就不详述了