• C# 托盘程序编写


    我使用的环境:Visual Studio 2005 Professoinal Edition
    注意:Windows窗体生成器生成的代码中不能加注释,即使加了注释也会被自动去掉!
    使用NotifyIcon控件,该控件的作用是程序运行时在Windows任务栏右侧的通知区域中(任务栏)显示图标。 使用contextMenuStrip控件,该控件可以关联到其它控件,作用是当右击关联的控件时显示菜单。 在NotifyIcon1的属性列表中的contextMenuStrip的下拉列表中选择你刚才创建的contextMenuStrip1菜单。你的托盘程序就拥有一个菜单了。 接下来的与通常图形界面程序的编写相同,首先在[设计]窗口中设计你的界面,然后双击做好的菜单或按钮进入代码窗口写对应的代码。 接下来是一些托盘程序所应具有的一些特别功能的代码
    为托盘程序设置双击托盘图标显示/隐藏窗口
    首先是在Windows 窗体设计器生成的代码中为托盘图标增加双击事件,具体做法是在具有托盘图标的Form的designer.cs中找到notifyIcon的部分,加入语句

    1this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);

    其作用是重载notifyIcon1的双击事件,为其添加方法notifyIcon1_DoubleClick(注意,这里的notifyIcon1和notifyIcon1_DoubleClick可以由你自己设定,可能与这里不同)。
    接着我们来实现notifyIcon1_DoubleClick方法,在Form的Class中写方法:

     1private void notifyIcon1_DoubleClick(object sender, EventArgs e) 
     2
     3this.Show(); 
     4if (this.WindowState == FormWindowState.Minimized) 
     5this.WindowState = FormWindowState.Normal; 
     6else if (this.WindowState == FormWindowState.Normal) 
     7this.WindowState = FormWindowState.Minimized; 
     8this.Activate(); //激活窗体并为其赋予焦点 
     9}
     
    10

    最小化时隐藏窗体(隐藏任务栏上的项目)
    首先同样是修改窗体设计器自动生成的代码,在Form1(假设,可能不同)中增加语句

    1 this.Resize += new System.EventHandler(this.Form1_Resize);

    然后实现Form1_Resize方法:

    1private void Form1_Resize(object sender, System.EventArgs e) 
    2
    3if (this.WindowState == FormWindowState.Minimized) 
    4
    5this.Hide(); 
    6}
     
    7}

    将关闭按钮重载为最小化

     1protected override void OnFormClosing(FormClosingEventArgs e) 
     2
     3if (!CloseTag) 
     4
     5this.WindowState = FormWindowState.Minimized; 
     6e.Cancel = true
     7}
     
     8else 
     9e.Cancel = false
    10base.OnFormClosing(e); 
    11}
     
    12

    这里需要说明的是,我的Form1有一个Bool型私有变量CloseTag,另外我的程序有一个关闭按钮,该按钮才是真正的关闭程序。我的做法是当使用我的关闭按钮时将CloseTag设为True,否则CloseTag为false。这样就做到了完整的关闭重载。

  • 相关阅读:
    AI:IPPR的数学表示-CNN稀疏结构进化(Mobile、xception、Shuffle、SE、Dilated、Deformable)
    基于视觉的机械手控制
    远程图形界面:VncServer与KDE桌面远程连接
    远程图形界面:使用putty+xmin远程登录ubuntu-kde
    CUDA 显存操作:CUDA支持的C++11
    C++11:using 的各种作用
    C++ 模板template和template
    Detectron:Pytorch-Caffe2-Detectron的一些跟进
    TF实战:(Mask R-CNN原理介绍与代码实现)-Chapter-8
    The type javax.servlet.http.HttpServletRequest cannot be resolved.
  • 原文地址:https://www.cnblogs.com/xxaxx/p/1603954.html
Copyright © 2020-2023  润新知