1、隐藏到托盘
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
// 取消关闭操作,并隐藏窗体
e.Cancel = true;
this.Hide();
// 在系统托盘区域显示 NotifyIcon 控件
m_notifyIcon.Visible = true;
}
}
private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// 如果用户单击了 NotifyIcon 控件,则重新显示窗体
this.Show();
m_notifyIcon.Visible = false;
}
}
2、开机自动启动
// 注册自动启动
private void RegisterAutoStart()
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rk.SetValue("MyApp", Application.ExecutablePath.ToString());
}
// 取消自动启动
private void UnregisterAutoStart()
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rk.DeleteValue("MyApp", false);
}
3、获取粘贴板
string builder =string.Empty;
private void timer1_Tick(object sender, EventArgs e)
{
if (Clipboard.GetText() == builder) return;
textBox1.AppendText("\r\n"+datetime);
textBox1.AppendText("\r\n");
textBox1.AppendText("\r\n" + Clipboard.GetText());
textBox1.AppendText("\r\n");
builder = Clipboard.GetText();
}
4、生成EXE图标和名称
(矢量图 是从阿里矢量图获取 iconfont-阿里巴巴矢量图标库)
5、全部程序
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFrmClipboardTool
{
public partial class Form1 : Form, IDisposable
{
private NotifyIcon m_notifyIcon;
public Form1()
{
InitializeComponent();
// 初始化 NotifyIcon 控件
m_notifyIcon = new NotifyIcon();
m_notifyIcon.Icon = this.Icon;
m_notifyIcon.Text = this.Text;
m_notifyIcon.MouseClick += notifyIcon_MouseClick;
RegisterAutoStart();
textBox1.ScrollBars = ScrollBars.Vertical;
// 添加右键菜单项
ContextMenu menu = new ContextMenu();
menu.MenuItems.Add("退出", (sender, e) =>
{
// 点击退出菜单时关闭应用程序
Application.Exit();
});
m_notifyIcon.ContextMenu = menu;
}
string builder =string.Empty;
private void timer1_Tick(object sender, EventArgs e)
{
if (Clipboard.GetText() == builder) return;
textBox1.AppendText("\r\n******************************************************************************************************************");
textBox1.AppendText("\r\n");
textBox1.AppendText("\r\n" + Clipboard.GetText());
textBox1.AppendText("\r\n");
builder = Clipboard.GetText();
}
// 注册自动启动
private void RegisterAutoStart()
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rk.SetValue("MyApp", Application.ExecutablePath.ToString());
}
// 取消自动启动
private void UnregisterAutoStart()
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rk.DeleteValue("MyApp", false);
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
// 取消关闭操作,并隐藏窗体
e.Cancel = true;
this.Hide();
// 在系统托盘区域显示 NotifyIcon 控件
m_notifyIcon.Visible = true;
}
}
private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// 如果用户单击了 NotifyIcon 控件,则重新显示窗体
this.Show();
m_notifyIcon.Visible = false;
}
}
}
}
标签:EXE,notifyIcon,textBox1,粘贴板,void,System,private,C#,using
From: https://blog.csdn.net/m0_61642614/article/details/136906304