C#编写最小化时隐藏为任务栏图标的Window appllication.
1.设置WinForm窗体属性showinTask=false

2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标。

3.添加窗体最小化事件(首先需要添加事件引用):

this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);

//上面一行是主窗体InitializeComponent()方法中需要添加的引用

private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState==FormWindowState.Minimized)
{
this.Hide();
this.notifyIcon1.Visible=true;
}

}
4.添加点击图标事件(首先需要添加事件引用):

private void notifyIcon1_Click(object sender, EventArgs e)
{
this.Visible = true;

this.WindowState = FormWindowState.Normal;

this.notifyIcon1.Visible = false;
}

5.可以给notifyIcon添加右键菜单:

主窗体中拖入一个ContextMenu控件NicontextMenu ,点中控件,在上下文菜单中添加菜单,notifyIcon1的ContextMenu行为中选中NicontextMenu 作为上下文菜单。

this.notifyIcon1= new System.Windows.Forms.NotifyIcon(this.components);
this.NicontextMenu = new System.Windows.Forms.ContextMenu();
this.menuItem_Hide = new System.Windows.Forms.MenuItem();
this.menuItem_Show = new System.Windows.Forms.MenuItem();
this.menuItem_Aubot = new System.Windows.Forms.MenuItem();
this.menuItem_Exit = new System.Windows.Forms.MenuItem();

this.notifyIcon1.ContextMenu = this.NicontextMenu;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("NotifyIcon.Icon")));
this.notifyIcon1.Text = "";
this.notifyIcon1.Visible = true;
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);

this.NicontextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
    this.menuItem_Hide,
    this.menuItem_Show,
    this.menuItem_Aubot,
    this.menuItem_Exit});
//
// menuItem_Hide
//
this.menuItem_Hide.Index = 0;
this.menuItem_Hide.Text = "隐藏";
this.menuItem_Hide.Click += new System.EventHandler(this.menuItem_Hide_Click);
//
// menuItem_Show
//
this.menuItem_Show.Index = 1;
this.menuItem_Show.Text = "显示";
this.menuItem_Show.Click += new System.EventHandler(this.menuItem_Show_Click);
//
// menuItem_Aubot
//
this.menuItem_Aubot.Index = 2;
this.menuItem_Aubot.Text = "关于";
this.menuItem_Aubot.Click += new System.EventHandler(this.menuItem_Aubot_Click);
//
// menuItem_Exit
//
this.menuItem_Exit.Index = 3;
this.menuItem_Exit.Text = "退出";
this.menuItem_Exit.Click += new System.EventHandler(this.menuItem_Exit_Click);
protected override void OnClosing(CancelEventArgs e)
{
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
e.Cancel = true;
}
protected override void OnClosing(CancelEventArgs e)
{
//this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
e.Cancel = true;
}

private void CloseCtiServer()
{
timer.Enabled = false;
DJ160API.DisableCard();
this.NotifyIcon.Visible = false;
this.Close();
this.Dispose();
Application.Exit();
}

private void HideCtiServer()
{
this.Hide();

}

private void ShowCtiServer()
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();

}
private void CtiManiForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.CloseCtiServer();
}

private void menuItem_Show_Click(object sender, System.EventArgs e)
{
this.ShowCtiServer();
}

private void menuItem_Aubot_Click(object sender, System.EventArgs e)
{

}

private void menuItem_Exit_Click(object sender, System.EventArgs e)
{
this.CloseCtiServer();
}

private void menuItem_Hide_Click(object sender, System.EventArgs e)
{
this.HideCtiServer();
}

private void CtiManiForm_SizeChanged(object sender, System.EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.HideCtiServer();
}

}

private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)
{                               
this.ShowCtiServer();
}

看到网上的一些方法是利用重写窗体的Closing过程来完成的,代码如下:
//添加NotifyIcon控件
private System.Windows.Forms.NotifyIcon notifyIcon1;
//设置属性
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.Closing +=new CancelEventHandler(Form1_Closing);
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "网页分析器";
this.notifyIcon1.Visible = false;
this.notifyIcon1.MouseDown += new System.Windows.Forms.MouseEventHandle(this.notifyIcon1_MouseDown);
//重写Closing过程
this.Closing +=new CancelEventHandler(Form1_Closing);
 
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    this.Hide();
    this.notifyIcon1.Visible=true;
    e.Cancel=true;
}
 
private void notifyIcon1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e){
   this.show();
   this.Activate();
   this.notifyIcon1.Visible=false;
}
但是这样就没有最小化的效果了,其实我们可以通过重写Deactivate事件来达到"最小化"的效果
代码:
~~~
this.Deactivate += new System.EventHandler(this.Form1_Mini);
private void Form1_Mini(object sender, System.EventArgs e)
{
   if(this.WindowState==FormWindowState.Minimized)
   {
       this.Visible=false;
       this.notifyIcon1.Visible=true;
   }
}
private void notifyIcon1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
   this.Visible=true;
   this.WindowState=FormWindowState.Normal;
   this.Activate();
   this.notifyIcon1.Visible=false;
}
这样就有最小化的效果了,不过还要解决最小化方向的问题~~~

转载于:https://www.cnblogs.com/freedom831215/archive/2010/01/28/1658373.html

C#编写最小化时隐藏为任务栏图标的Window appllication相关推荐

  1. 如何使对话框程序启动以及主窗口最小化时不在任务栏上显示

    时下流行的许多软件,如QQ,迅雷等,启动主程序以及主窗口最小化时,不在任务栏上显示按钮,这是怎么做到的呢? 首先说主窗口启动时不再任务栏上显示按钮的问题 方法1,将主窗口的属性设置为WS_EX_TOO ...

  2. 窗体最小化到隐藏到任务栏 notifyIcon

    在窗体上添加notifylcon控件 //设置控件为可见 notifyIcon1.Visible = true; //弹气泡/通知框提示 notifyIcon1.ShowBalloonTip(3, & ...

  3. Mac的最小化和隐藏的区别

    Mac 中应用程序窗口的最小化和隐藏的快捷键: CMD + H 隐藏应用程序 CMD + M 最小化应用程序 重点在于两点的区别: 最小化会隐藏当前应用程序的窗口,不切换当前的应用程序:隐藏应用程序会 ...

  4. 文件夹的最小化时,上面的名字。

    我们在把文件夹最小化时,常常会带上路径,所以看着很不方便~ [在标题栏显示完整路径]不要选中,那么,你看到的就是这个文件夹的名字,不会在前面加上一堆路径.

  5. QT制作的软件全屏显示后最小化,点击任务栏图标打开时不是全屏

    QT制作的软件全屏显示后最小化,点击任务栏图标打开时不是全屏 前言 用QT做了一个小软件,把它自带的窗口栏给屏蔽了自己搞了关闭和最小化的按钮,且软件打开是全屏显示的.如今遇到的问题是,打开软件全屏显示 ...

  6. MFC子窗口设置,添加最小化按键,显示任务栏图标,点击关闭退出程序

    MFC子窗口默认只有一个关闭按钮,并且没有任务栏图标存在,如何给子窗口添加最小化按钮并且显示任务栏图标 在窗体的属性菜单中,有Minimize Box项设置成true,就会出现最小化按钮 在窗体的On ...

  7. 【Mac 教程系列第 7 篇】最小化时程序统一在程序坞右侧缩小,很占地方怎么办?

    这是[Mac 教程系列第 7 篇],如果觉得有用的话,欢迎关注专栏. 问题描述 当你打开了一个程序后,最小化该程序后,会统一缩小在程序坞底部右侧部分,如下图黄线包着部分所示 打开的越多,底部被占用的部 ...

  8. 【pyqt5学习】——窗口最小化至托盘、取消任务栏图标

    目录 1.最小化至托盘 1)重写系统托盘类,设置托盘图标等属性 2)将具体的窗口放入托盘 2.取消任务栏界面图标 3.问题汇总 1)退出后托盘图标还是存在,没有消失 2)最小化后左下角会出现 窗口 3 ...

  9. 最小化窗口后,在任务栏里找不到最小化后的图标

    用记事本把以下代码存为文件名为xp_taskbar_desktop_fixall_chs.vbs的文件后双击运行既可! =======以下代码========== 'xp_taskbar_deskto ...

最新文章

  1. 深度学习——你需要了解的八大开源框架
  2. android studio 7200u,超惊艳的设计!微软正式将Surface Studio和Surface Laptop带进中国:设计师们都看哭了...
  3. SecureCRT上传和下载文件
  4. leetcode 492. 构造矩形(Java版,三种解法)
  5. swift date 计算差_[Swift 设计模式] 适配器
  6. web.config 指定的默认页失效
  7. 四种变量的区别(局部变量,全局变量,静态局部变量,静态全局变量)
  8. js 设置body背景图片
  9. mfc中CImageList的使用
  10. 如何用卡诺图化简带有约束条件的逻辑函数?
  11. 内卷时代下的前端技术-使用JavaScript在浏览器中生成PDF文档
  12. R语言ggplot2可视化:loess回归曲线可视化、填充两条 loess回归曲线之间的区域实战(Fill region between two loess-smoothed lines)
  13. 怀旧服ouf头像插件_魔兽世界7.0前夕ShadowedUF简洁头像插件
  14. word 2007 无法输入汉字,怎么办?
  15. 公众号头条文章 API 接口
  16. html5 input搜索框样式修改,修改input搜索框默认叉号的样式为自定义图片
  17. 计算机图形输入的原理,【计算机图形学】零 · 计算机图形系统概述
  18. MySQL 密码设置
  19. PyEcharts 基本图表之日历图
  20. 深度学习(二十九)Batch Normalization 学习笔记

热门文章

  1. 浅析网站如何才能最大化获得用户访问量?
  2. docker 查看已安装容器_docker中的容器安装PHP扩展件
  3. c语言课设代写一般多少钱_结婚彩礼一般多少钱 2019彩礼会涨到多少钱
  4. python代码写龙卷风_python面试题
  5. oracle union 最多_用户来稿:我就是那个在优买计划赚钱最多的男人
  6. Parcelable与Serializable接口的用法和区别
  7. 位于/var/log目录下的20个Linux日志文件
  8. 交叉熵代价函数——当我们用sigmoid函数作为神经元的激活函数时,最好使用交叉熵代价函数来替代方差代价函数,以避免训练过程太慢...
  9. 开源DDos 机器学习思路求解的一些源码——TODO 待分析
  10. spark shuffle内在原理说明