C#编写最小化时隐藏为任务栏图标的 Window appllication.

1.设置WinForm窗体属性showinTask=false

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

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

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

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

时间: 2024-08-10 21:29:26

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

窗体最小化时,不挡住任务栏

/// <summary>        /// 窗体最小化时,隐藏到系统托盘        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void MainFace_SizeChanged(object sender, Even

Mac的最小化和隐藏的区别

Mac 中应用程序窗口的最小化和隐藏的快捷键: CMD + H 隐藏应用程序 CMD + M 最小化应用程序 重点在于两点的区别: 最小化会隐藏当前应用程序的窗口,不切换当前的应用程序:隐藏应用程序会隐藏窗口,并将当前的焦点切换到上一次活跃的应用程序. 在用CMD + TAB手动切换应用程序时,隐藏的窗口会自动展现,最小化的却不会. 最小化会有动画效果,隐藏不会有动画.

delphi模态窗体最小化会隐藏的问题

在使用delphi创建模态窗体的时候最小化窗体会导致最小化的窗体不可见,再次点击主窗体才会显示. 在这个模态窗体中增加以下函数 procedure WmSysCommand(var msg: TMessage); message WM_SYSCOMMAND; procedure TForm1.WmSysCommand(var msg: TMessage); begin inherited;//必须加,否则模态窗关闭最大化还原将不能用 if msg.WParam = SC_MINIMIZE the

浏览器最小化时右下角 消息提醒,点击消息最大化浏览器

//框架页面 function isMinStatus() { var isMin = false; if (window.outerWidth != undefined) { isMin = window.outerWidth <= 160 && window.outerHeight <= 27; } else { isMin = window.screenTop < -30000 && window.screenLeft < -30000; }

线性回归(Linear Regression)均方误差损失函数最小化时关于参数theta的解析解的推导(手写)

第一页纸定义了损失函数的样子, theta, X 和 y 的 shape, 以及最终的损失函数向量表现形式. 第二页纸抄上了几个要用到的矩阵求导公式,以及推导过程和结果. 要说明的是:推导结果与theta, X 和 y 的 shape有直接关系.也就是说可能和某教材,某大牛教学视频的结论外貌上不一致,但实质完全相同.

UIScrollView滚动时隐藏底部导航栏问题

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { NSLog(@"開始滚动"); int currentPostion = scrollView.contentOffset.y; if (currentPostion - _lastPosition > 20  && currentPostion >0) { _lastPosition = currentPostion; NSLog(@"

Delphi如何让程序最小化到任务栏(截取WM_SYSCOMMAND后,调用Shell_NotifyIcon)

现在很多的应用程序都有这样一种功能,当用户选择最小化窗口时,窗口不是象平常那样最小化到任务栏上,而是“最小化”成一个任务栏图标.象FoxMail 3.0 NetVampire 3.0等都提供了这样的功能.实现这样的功能实际上并不复杂,在窗口最小化时,窗口会发出WM_SYSCOMMAND消息,你只要需要截取Windows的WM_SYSCOMMAND消息,在窗口最小化时隐藏窗口并调用WindowsAPI函数Shell_NotifyIcon将定义的图标添加到任务栏上,Shell_NotifyIcon的

Outlook 最小化不关闭图解

  Outlook 最小化不关闭图解 设置Outlook 最小化而不是真正的关闭 (1)      找到右下角通知区域右键 (2)找到Outlook 图标右键,去掉:最小化时隐藏 设置Outlook 最小化而不是关闭,完成

最小化到托盘

这个办法主要是针对Delphi2010或者更高版本, 利用自带的TTrayIcon来实现最小化到托盘的功能, 至于低版本, 比如D7, 使用RZ的Tray控件就好了 主要修改点: 1.2010或者更高版本, 自带了一个TTrayIcon控件来实现托盘图标, 但是这个控件没有实现最小化时隐藏的功能 2.2010或者更高版本增加了MainFormOnTaskbar功能, 众所周知, delphi的任务栏按钮一直都不是标准的windows风格, 有了这个设置后, 任务栏按钮正常了, 但是也为我们的这个