场景:点击窗体关闭按钮并隐藏在系统托盘里显示小图标,小图标右键有显示窗体和退出两个按钮以及最后打包成exe
1.在主页form窗体拖入控件notifyIcon和contextMenuStrip控件
设置notifyIcon1控件属性:
contextMenuStrip为contextMenuStrip1
Icon选择一个图标
Text输入你想起托盘的名称
notifyIcon1事件里设置
/// <summary>
/// 点击鼠标"左键"发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
//点击鼠标"左键"发生
if (e.Button == MouseButtons.Left)
{
this.Visible = true; //窗体可见
this.WindowState = FormWindowState.Normal; //窗体默认大小
this.notifyIcon1.Visible = true; //设置图标可见
}
}
设置contextMenuStrip控件新增两个按钮
然后双击编辑“显示”按钮点击事件
this.Show(); //窗体显示
this.WindowState = FormWindowState.Normal; //窗体状态默认大小
this.Activate();
编辑“退出”按钮点击事件
//点击"是(YES)"退出程序
if (MessageBox.Show("确定要退出程序?", "安全提示",
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Warning)
== System.Windows.Forms.DialogResult.Yes)
{
notifyIcon1.Visible = false; //设置图标不可见
this.Close(); //关闭窗体
this.Dispose(); //释放资源
Application.Exit(); //关闭应用程序窗体
}
最后设置主窗体FormClosing事件
//窗体关闭原因为单击"关闭"按钮或Alt+F4
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true; //取消关闭操作 表现为不关闭窗体
this.Hide(); //隐藏窗体
}
打包:
1.选择项目右键==》选择属性==>调试==>配置==》选择活动(Release)
2.选择项目右键==》重新生成==》打开项目文件夹bin\Release文件夹下
3.全选所有文件夹,然后选择添加到压缩文件
5.在压缩文件名输入你要起的名称后缀带.exe,然后选择高级选项==》自解压选项
6.设置提取后运行
7.设置模式 解压到临时文件夹以及全部隐藏,然后点击确定,最后再次点击确定打包成功
封装公共方法:
/// <summary>
/// 公历转为农历的函数
/// </summary>
/// <param name="solarDateTime">公历日期</param>
/// <returns>农历的日期</returns>
public string SolarToChineseLunisolarDate(DateTime solarDateTime)
{
System.Globalization.ChineseLunisolarCalendar cal = new System.Globalization.ChineseLunisolarCalendar();
int year = cal.GetYear(solarDateTime);
int month = cal.GetMonth(solarDateTime);
int day = cal.GetDayOfMonth(solarDateTime);
int leapMonth = cal.GetLeapMonth(year);
return string.Format("农历{0}{1}({2})年{3}{4}月{5}{6}"
, "甲乙丙丁戊己庚辛壬癸"[(year - 4) % 10]
, "子丑寅卯辰巳午未申酉戌亥"[(year - 4) % 12]
, "鼠牛虎兔龙蛇马羊猴鸡狗猪"[(year - 4) % 12]
, month == leapMonth ? "闰" : ""
, "无正二三四五六七八九十冬腊"[leapMonth > 0 && leapMonth <= month ? month - 1 : month]
, "初十廿三"[day / 10]
, "日一二三四五六七八九"[day % 10]
);
}
/// <summary>
/// 根据年月日计算星期几(Label2.Text=CaculateWeekDay(2004,12,9);)
/// </summary>
/// <param name="y">年</param>
/// <param name="m">月</param>
/// <param name="d">日</param>
/// <returns></returns>
public string CaculateWeekDay(int y, int m, int d)
{
if (m == 1) m = 13;
if (m == 2) m = 14;
int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7 + 1;
string weekstr = "";
switch (week)
{
case 1: weekstr = "星期一"; break;
case 2: weekstr = "星期二"; break;
case 3: weekstr = "星期三"; break;
case 4: weekstr = "星期四"; break;
case 5: weekstr = "星期五"; break;
case 6: weekstr = "星期六"; break;
case 7: weekstr = "星期日"; break;
}
return weekstr;
}
/// <summary>
/// 计算两个日期时间差多少天
/// </summary>
/// <param name="dateStart"></param>
/// <param name="dateEnd"></param>
/// <returns></returns>
private int DateDiff(DateTime dateStart, DateTime dateEnd)
{
DateTime start = Convert.ToDateTime(dateStart.ToShortDateString());
DateTime end = Convert.ToDateTime(dateEnd.ToShortDateString());
TimeSpan sp = end.Subtract(start);
return sp.Days;
}
踩坑点:文章来源:https://www.toymoban.com/news/detail-458588.html
1.最后打包的时候压缩包压缩路径需要换成非项目里的路径或起的名称不能与文件夹已有exe重名文章来源地址https://www.toymoban.com/news/detail-458588.html
到了这里,关于【winForm取消窗体关闭操作并隐藏到系统托盘操作以及打包exe】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!