惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
B
Blog RSS Feed
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
Recent Announcements
Recent Announcements
A
About on SuperTechFans
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Google DeepMind News
Google DeepMind News
S
Schneier on Security
S
Secure Thoughts
T
The Exploit Database - CXSecurity.com
Martin Fowler
Martin Fowler
P
Proofpoint News Feed
Security Latest
Security Latest
Jina AI
Jina AI
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Recorded Future
Recorded Future
T
Tor Project blog
有赞技术团队
有赞技术团队
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
News | PayPal Newsroom
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
Forbes - Security
Forbes - Security
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
NISL@THU
NISL@THU
C
Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Google DeepMind News
Google DeepMind News
Project Zero
Project Zero
IT之家
IT之家
T
Threatpost
Cyberwarzone
Cyberwarzone
O
OpenAI News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
J
Java Code Geeks
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
月光博客
月光博客
Latest news
Latest news
MongoDB | Blog
MongoDB | Blog
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 刚子

sql2005中Create 对于 数据库“xxx”失败解决方案 一个简单的C#多线程间同步(事件方法)的例子 俞老师在同济大学的演讲词:度过有意义的生命 [转]谈基于.net平台windows开发中的模式窗体 利用反射原理,灵活配置类名和方法名 System.Windows.Forms.Timer和System.Timers.Timer的区别 [转] 什么是Smart Client 在VS.NET 2005中体验clickonce技术 (智能客户端) C#操作EXCEL文件 如何创建、安装和调试Windows服务 最小化到系统托盘并恢复 C#操作XML的完整例子——XmlDocument篇 C#多线程应用探讨(转载) C# 泛型介绍 vs2005新建网站对话框中三种位置选项(文件系统、http、ftp)的区别? 工厂模式代码的使用解释 创建 类PetShop4.0 架构的项目 .net面试题 C#面试常见问题
[转]在C#隐藏启动窗口的几种方法
刚子 · 2009-03-09 · via 博客园 - 刚子

写过一个程序,要求在程序启动的时候主窗口隐藏,只在系统托盘里显示一个图标。一直以来采用的方法都是设置窗口的ShowInTaskBar=false, WindowState=Minimized。但是偶然发现尽管这样的方法可以使主窗口隐藏不见,但是在用Alt+Tab的时候却可以看见这个程序的图标并把这个窗口显示出来。因此这种方法其实并不能满足要求。

经过研究,又找到两个方法。

方法一: 重写setVisibleCore方法

protected override void SetVisibleCore(bool value)
{
     
base.SetVisibleCore(false);
}

这个方法比较简单,但是使用了这个方法后主窗口就再也不能被显示出来,而且在退出程序的时候也必须调用Application.Exit方法而不是Close方法。这样的话就要考虑一下,要把主窗口的很多功能放到其他的地方去。

方法二: 不创建主窗口,直接创建NotifyIcon和ContextMenu组件
这种方法比较麻烦,很多代码都必须手工写

static void Main()
 {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(
false);

            System.Resources.ResourceManager resources 

= 
                
new System.Resources.ResourceManager("myResource",  System.Reflection.Assembly.GetExecutingAssembly());
            NotifyIcon ni 
= new NotifyIcon();

            ni.BalloonTipIcon 

= System.Windows.Forms.ToolTipIcon.Warning;
            ni.BalloonTipText 
= "test!";
            ni.BalloonTipTitle 
= "test.";
            
//ni.ContextMenuStrip = contextMenu;
            ni.Icon = ((System.Drawing.Icon)(resources.GetObject("ni.Icon")));
            ni.Text 
= "Test";
            ni.Visible 
= true;
            ni.MouseClick 
+= delegate(object sender, MouseEventArgs e)
            {
                ni.ShowBalloonTip(
0);
            };

            Application.Run();
}

 如果需要的组件太多,这个方法就很繁琐,因此只是做为一种可行性研究。

方法三:前面两种方法都有一个问题,主窗口不能再显示出来。现在这种方法就没有这个问题了

private bool windowCreate=true;
...
protected override void OnActivated(EventArgs e) 
        { 
            
if (windowCreate) 
            { 
                
base.Visible = false;
                windowCreate 
= false;
            } 
base.OnActivated(e); 
        }
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            
if (this.Visible == true)
            {
                
this.Hide();
                
this.ShowInTaskbar = false;
            }
            
else
            {
                
this.Visible = true;
                
this.ShowInTaskbar = true;
                
this.WindowState = FormWindowState.Normal;
                
//this.Show();
                this.BringToFront();
            }

        }