WinForm窗体程序 实现系统托盘后台运行功能

核心代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows;
namespace winform窗体托盘后台运行
{
   public partial class Form1 : Form   
   {  

    //这里在窗体上没有拖拽一个NotifyIcon控件,而是在这里定义了一个变量  
    private NotifyIcon notifyIcon = null;  

    public Form1()  
    {  

        InitializeComponent();  

        //调用初始化托盘显示函数  
        InitialTray();  

    }  


    /// <summary>  

    /// 窗体关闭的单击事件  

    /// </summary>  

    /// <param name="sender"></param>  

    /// <param name="e"></param>  

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
    {  
        e.Cancel = true;  
    //通过这里可以看出,这里的关闭其实不是真正意义上的“关闭”,而是将窗体隐藏,实现一个“伪关闭”  
        this.Hide();  

    }  


    private void InitialTray()  
    {  

        //隐藏主窗体  

        this.Hide();  

        //实例化一个NotifyIcon对象  

        notifyIcon = new NotifyIcon();  

        //托盘图标气泡显示的内容  

        notifyIcon.BalloonTipText = "正在后台运行";     

        //托盘图标显示的内容  

        notifyIcon.Text = "窗体托盘后台运行测试程序";  

        //注意:下面的路径可以是绝对路径、相对路径。但是需要注意的是:文件必须是一个.ico格式  
        
         //相对路径
        //这个是程序根目录下面的一张图片demo.ico
        string path = System.IO.Path.Combine((Application.StartupPath + @"\"), "demo.ico");
  notifyIcon.Icon = new System.Drawing.Icon(path); 

        //绝对路径
       // notifyIcon.Icon = new System.Drawing.Icon("E:/ASP项目/images/3.5 inch Floppy.ico");  


        //true表示在托盘区可见,false表示在托盘区不可见  

        notifyIcon.Visible = true;  

        //气泡显示的时间(单位是毫秒)  

        notifyIcon.ShowBalloonTip(2000);              

        notifyIcon.MouseClick += new System.Windows.Forms.MouseEventHandler(notifyIcon_MouseClick);  


        设置二级菜单  

        //MenuItem setting1 = new MenuItem("二级菜单1");  

        //MenuItem setting2 = new MenuItem("二级菜单2");  

        //MenuItem setting = new MenuItem("一级菜单", new MenuItem[]{setting1,setting2});  



        //帮助选项,这里只是“有名无实”在菜单上只是显示,单击没有效果,可以参照下面的“退出菜单”实现单击事件  

        MenuItem help = new MenuItem("帮助");  

        //关于选项  

        MenuItem about = new MenuItem("关于");  

        //退出菜单项  

        MenuItem exit = new MenuItem("退出");  

        exit.Click += new EventHandler(exit_Click);  


        关联托盘控件  

        //注释的这一行与下一行的区别就是参数不同,setting这个参数是为了实现二级菜单  

        //MenuItem[] childen = new MenuItem[] { setting, help, about, exit };  

        MenuItem[] childen = new MenuItem[] {help,about,exit};  

        notifyIcon.ContextMenu = new ContextMenu(childen);  


        //窗体关闭时触发  

        this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);  

    }  



    /// <summary>  

    /// 鼠标单击  

    /// </summary>  

    /// <param name="sender"></param>  

    /// <param name="e"></param>  

    private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)  
    {  

        //鼠标左键单击  

        if (e.Button == MouseButtons.Left)  

        {  

            //如果窗体是可见的,那么鼠标左击托盘区图标后,窗体为不可见  

            if (this.Visible == true )  

            {  

                this.Visible = false;  

            }  

            else  

            {  

                this.Visible = true;  

                this.Activate();  

            }  

        }  

    }  



    /// <summary>  

    /// 退出选项  

    /// </summary>  

    /// <param name="sender"></param>  

    /// <param name="e"></param>  

    private void exit_Click(object sender, EventArgs e)  
    {  
        //退出程序  
       System.Environment.Exit(0);    
    }  
  }  
}

 

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是一个简单的在 C# Winform 窗体实现顺序表增删改查的实例: 1. 首先,在你的 Winform 窗体中添加四个 TextBox 控件和四个 Button 控件,分别用于输入元素、输入要删除的元素位置、输入要修改的元素位置和输入新值,以及按钮事件。 2. 接下来,在窗体的 Load 事件中,声明一个 List<int> 类型的变量来存储顺序表中的元素: ``` List<int> array = new List<int>(); ``` 3. 实现插入元素的方法。在 button1_Click 事件中,通过 TextBox 控件获取要插入的元素值,并调用 List 类型的 Add 方法将元素插入到顺序表中: ``` private void button1_Click(object sender, EventArgs e) { int value = int.Parse(textBox1.Text); array.Add(value); // 更新 DataGridView 控件中的数据 dataGridView1.DataSource = null; dataGridView1.DataSource = array; } ``` 4. 实现删除元素的方法。在 button2_Click 事件中,通过 TextBox 控件获取要删除的元素位置,然后调用 List 类型的 RemoveAt 方法删除指定位置的元素: ``` private void button2_Click(object sender, EventArgs e) { int index = int.Parse(textBox2.Text); array.RemoveAt(index); // 更新 DataGridView 控件中的数据 dataGridView1.DataSource = null; dataGridView1.DataSource = array; } ``` 5. 实现修改元素的方法。在 button3_Click 事件中,通过 TextBox 控件获取要修改的元素位置和新值,然后调用 List 类型的 indexer 语法修改指定位置的元素: ``` private void button3_Click(object sender, EventArgs e) { int index = int.Parse(textBox3.Text); int value = int.Parse(textBox4.Text); array[index] = value; // 更新 DataGridView 控件中的数据 dataGridView1.DataSource = null; dataGridView1.DataSource = array; } ``` 6. 实现查找元素的方法。在 button4_Click 事件中,通过 TextBox 控件获取要查找的元素值,然后调用 List 类型的 FindIndex 方法查找元素的位置: ``` private void button4_Click(object sender, EventArgs e) { int value = int.Parse(textBox5.Text); int index = array.FindIndex(x => x == value); if(index >= 0) { MessageBox.Show($"元素 {value} 的位置是:{index}"); } else { MessageBox.Show($"元素 {value} 不存在!"); } } ``` 以上就是一个简单的在 C# Winform 窗体实现顺序表增删改查的实例。你可以将这些方法放在一个类中,并在 Winform 中调用这个类来操作顺序表。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值