C# 窗体 隐藏到托盘 开机自动启动 粘贴板 生成的EXE图标和名称

本文介绍了如何在C#WindowsForm应用中实现窗体关闭时隐藏到系统托盘、通过NotifyIcon控制台点击重显、设置开机自动启动以及监控粘贴板内容的功能。
摘要由CSDN通过智能技术生成

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;
            }
        }

    }
}

  • 14
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值