一、实现的WinForm窗体应用程序工具
1.1、实现功能:
①实现生成5种标识符(N、D、B、P、X)对应的GUID;
②实现生成16位、22位唯一数字的字符串;
③将生成的GUID内容保存到本地文本文件中;
④能够生成单个和批量生成对应的GUID;
⑤关闭程序会在右下角的托盘显示图标,点击该图标右键退出在真正关闭程序;
⑥实现该程序只能运行一个实例。
1.2、工具效果图如下:
二、该工具的GUID实现基础参考C#中Guid.ToString (String)五种格式,以及将32位的GUID转为16位及其他格式
三、实现点击右上角后在系统右下角程序托盘图标效果
①打开左侧的"工具箱",在项目中添加NotifyIcon控件
②打开左侧的"工具箱",在项目中添加ContextMenuStrip控件
③具体的实现脚本如下:(注意:notifyIcon1是NotifyIcon的名称,myMenu是ContextMenuStrip的名称)
//双击托盘图片重新显示
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
}
//关闭窗体是最小化到系统托盘
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//当用户点击窗体右上角X按钮或(Alt + F4)时 发生
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.ShowInTaskbar = false;
this.notifyIcon1.Icon = this.Icon;
this.Hide();
}
}
//单击托盘图片
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = true;
}
if (e.Button == MouseButtons.Right)
{
myMenu.Show();
}
}
//关闭程序
private void Exit_Click_1(object sender, EventArgs e)
{
Application.Exit();
}
基本的设置如下:
四、实现只允许窗体程序只运行一个实例的方法如下:
static class Program
{
private static System.Threading.Mutex mutex;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
mutex = new System.Threading.Mutex(true,"OnlyRun");
if (mutex.WaitOne(0, false))
{
Application.Run(new Form1());
}
else
{
MessageBox.Show("程序已经在运行,请勿启动多个程序!!!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
Application.Exit();
}
}
}
来源:CSDN
作者:xiaochenXIHUA
链接:https://blog.csdn.net/xiaochenXIHUA/article/details/104043638