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.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;
}
}
。。。。。。
具体还有一些细节就不详述了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架