当前位置: 代码网 > it编程>编程语言>Asp.net > C#实现Winform序在系统托盘显示图标和开机自启动

C#实现Winform序在系统托盘显示图标和开机自启动

2025年01月20日 Asp.net 我要评论
实现步骤创建 notifyicon 控件并设置属性;编写 notifyicon 响应控制事件;在主窗体的load事件中将 notifyicon 添加到系统托盘;程序退出时,移除系统托盘的 notify

实现步骤

  • 创建 notifyicon 控件并设置属性;
  • 编写 notifyicon 响应控制事件;
  • 在主窗体的load事件中将 notifyicon 添加到系统托盘;
  • 程序退出时,移除系统托盘的 notifyicon;

notifyicon 控件,通常用于在系统托盘中显示图标,通过使用它就可以我们想要的效果。

属性描述
icon在系统托盘中显示的图标
text鼠标悬停在图标时显示的文本
visible指定是否可见

常用方法

方法描述
showcontextmenu在系统托盘上下文菜单中显示指定的菜单

添加控件(拖拽方式)

将notifyicon和一个contextmenustrip控件。拖到主窗体中可以修改控件名称

notifyicon 托盘图标

contextmenustrip 托盘图标右击弹出的菜单

设置控件

点击 contextmenustrip 右上方的三角图标 -> 编辑项,弹出项信合编辑器

添加右健菜单信息

添加主窗体事件

在最小化或关闭主窗体时,显示在任务栏托盘区域,实现了单击关闭时,不真正关闭程序,而是将主界面隐藏hide掉,同时开始显示托盘菜单。

//  只有form_closing事件中 e.cancel可以用。
//  你的是form_closed事件。 form_closed事件时窗口已关了 ,cancel没用了;
//  form_closing是窗口即将关闭时询问你是不是真的关闭才有cancel事件

private void mainwindow_formclosing(object sender, formclosingeventargs e)
{

    // 注意判断关闭事件reason来源于窗体按钮,否则用菜单退出时无法退出!
    if (e.closereason == closereason.userclosing)
    {
        //取消"关闭窗口"事件
        e.cancel = true; // 取消关闭窗体 

        //使关闭时窗口向右下角缩小的效果
        this.windowstate = formwindowstate.minimized;
        this.mainnotifyicon.visible = true;
        //this.m_cartoonform.cartoonclose();
        this.hide();
        return;
    }
}

实现双击托盘打开主程序

//  添加托盘程序
//  版本更新自1.0.1
private void mainnotifyicon_mousedoubleclick(object sender, mouseeventargs e)
{
    if (this.visible)
    {
        this.windowstate = formwindowstate.minimized;
        this.mainnotifyicon.visible = true;
        this.hide();
    }
    else
    {
        this.visible = true;
        this.windowstate = formwindowstate.normal;
        this.activate();
    }
}
//  添加托盘程序右键菜单项
//  版本更新自1.0.1
//  退出
//  添加日期 --  2015-07-29 21:44
private async void toolstripmenuitemquit_click(object sender, eventargs e)
{
    if (messagebox.show("你确定要退出?", "系统提示", messageboxbuttons.yesno, messageboxicon.information, messageboxdefaultbutton.button1) == dialogresult.yes)
    {

        this.mainnotifyicon.visible = false;
        this.close();
        this.dispose();
        system.environment.exit(system.environment.exitcode);   

    }
}

代码方式添加

using system;
using system.windows.forms;


namespace fountain.winform.notifydemo
{
    public partial class formmain : form
    {
        /// <summary>
        /// 通知控件
        /// </summary>
        private notifyicon notifyicon = new notifyicon();
        /// <summary>
        /// 通知控件显示菜单
        /// </summary>
        private contextmenustrip contextmenustrip = new contextmenustrip();
        /// <summary>
        /// 构造方法
        /// </summary>
        public formmain()
        {
            initializecomponent();
        }
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void formmain_load(object sender, eventargs e)
        {
            this.initializenotifymenu();
            this.notifyicon.text = this.text;
            this.notifyicon.visible = true;
            this.notifyicon.icon = this.icon;
            this.notifyicon.contextmenustrip = this.contextmenustrip;
            this.notifyicon.doubleclick += notifyicon_doubleclick;
        }
        /// <summary>
        /// 托盘菜单
        /// </summary>
        private void initializenotifymenu()
        {
            try
            {
                contextmenustrip.items.clear();
                toolstripmenuitem showmenuitem = new toolstripmenuitem("显示界面");
                showmenuitem.tag = "显示";
                showmenuitem.click += new eventhandler(showmenuitem_click);
                contextmenustrip.items.add(showmenuitem);


                toolstripmenuitem sboutmenuitem = new toolstripmenuitem("关于");
                sboutmenuitem.tag = "关于";
                sboutmenuitem.click += new eventhandler(aboutmenuitem_click);
                contextmenustrip.items.add(sboutmenuitem);


                toolstripmenuitem exitmenuitem = new toolstripmenuitem("退出");
                exitmenuitem.tag = "退出";
                exitmenuitem.click += new eventhandler(existmenuitem_click);
                contextmenustrip.items.add(exitmenuitem);
            }
            catch(exception exception) 
            {
                throw new exception(exception.message);
            }
        }
        /// <summary>
        /// 右击任务栏图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyicon_doubleclick(object sender, eventargs e)
        {
            try
            {
                if (this.windowstate == formwindowstate.normal)
                {
                    this.windowstate = formwindowstate.minimized;
                    this.hide();
                }
                else if (this.windowstate == formwindowstate.minimized)
                {
                    this.show();
                    this.windowstate = formwindowstate.normal;
                    this.activate();
                }
            }
            catch (exception objexception)
            {
                throw new exception(objexception.message);
            }
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void showmenuitem_click(object sender, eventargs e)
        {
            try
            {
                this.show();
                this.windowstate = formwindowstate.normal;
                this.activate();
            }
            catch (exception objexception)
            {
                throw new exception(objexception.message);
            }
        }
        /// <summary>
        /// 关于
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void aboutmenuitem_click(object sender, eventargs e)
        {
            try
            {
            }
            catch (exception objexception)
            {
                messagebox.show(objexception.message);
            }
        }
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void existmenuitem_click(object sender, eventargs e)
        {
            try
            {
                if (messagebox.show("你确定要退出程序吗?","提示", messageboxbuttons.okcancel, messageboxicon.question, messageboxdefaultbutton.button2) == dialogresult.ok)
                {
                    this.notifyicon.visible = false;
                    this.notifyicon.dispose();
                    this.dispose();
                    application.exit();
                }
            }
            catch (exception objexception)
            {
                messagebox.show(objexception.message);
            }
        }
        /// <summary>
        /// 主窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void formmain_formclosing(object sender, formclosingeventargs e)
        {
            try
            {
                e.cancel = true;
                this.hide();
                this.notifyicon.dispose();
            }
            catch (exception objexception)
            {
                messagebox.show(objexception.message);
            }
        }
        /// <summary>
        /// 窗体大小变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void formmain_resize(object sender, eventargs e)
        {
            if (this.windowstate == formwindowstate.minimized)
            {
                this.showintaskbar = false;
                this.hide();
                this.notifyicon.visible = true;
            }
        }
    }
}

系统开机自启动应用程序

using microsoft.win32;
using system;
using system.diagnostics;
using system.windows.forms;


namespace fountain.winform.notifydemo
{
    public partial class formmain : form
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        public formmain()
        {
            initializecomponent();
        }
        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void formmain_load(object sender, eventargs e)
        { 
            //一般不会放这个事件里去设置,而且设置时需要看下注册表要有没有写入成功有的话可以不用调了,偷懒多调一次也没事
            string applictionname = process.getcurrentprocess().mainmodule.modulename;
            string applictionpath = process.getcurrentprocess().mainmodule.filename;
            #region 当前登陆用户的注册表启动项
            registrykey registrykey = registry.currentuser.createsubkey(@"software\microsoft\windows\currentversion\run");
            registrykey.setvalue(applictionname, applictionpath);
            #endregion
            #region 所有用户的注册表启动项
            //registrykey registrykey = registry.localmachine.createsubkey(@"software\microsoft\windows\currentversion\run");
            //registrykey.setvalue(applictionname, applictionpath);
            #endregion
        }
    }
}

到此这篇关于c#实现winform序在系统托盘显示图标和开机自启动的文章就介绍到这了,更多相关c# winform开机自启动内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com