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

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - smalldust

NTT大规模网络故障 亲手焙制一个极其简单但却极其实用的Reflector插件 关于园子里讨论的软件的追求的杂谈 Reflector保护方法初探 .Net 2.0 原汁原味读取注册表 为什么我不用IE7和FireFox 给.Net程序员的PInvoke Tips [2]: Are Strings Immutable? 除了Exception,你还能throw什么? 给.Net程序员的PInvoke Tips [1]: String is Sometimes an Integer CLR Team程序员出的难题,有兴趣的朋友不妨挑战一下 - smalldust - 博客园 Google Trends发布 也谈用反射实现Enum→String映射:一种重视性能的方法 针对个例的、社区性的维基系统设想(草稿) 数学的思考方式 VS 程序的思考方式 C#程序模拟鼠标操作 [Simulate Mouse Movement and Click Programmatically] 如何在C#中获取“当前目录” Windows Vista将推迟到2007年1月发布 .Net 2.0实例学习:WebBrowser页面与WinForm交互技巧 使用关键字作为自定义标识符
WinForm程序启动时不显示主窗体的实现方法
smalldust · 2006-03-29 · via 博客园 - smalldust

曾经在一个月前看过一篇文章(http://gamix.cnblogs.com/archive/2006/03/10/347354.html),其中作者希望程序启动时不显示主窗体,而只是在SystemTray显示一个图标;当用户点击该图标时,才第一次显示出主窗体来。

作者在文章中已经说得很清楚,将Form的Visible属性设置为false是不行的,因为在Application.Run方法的内部,主窗体的Visible属性将被强制设置为true。
此外,有朋友在回复中提供了“不启动任何窗体”的办法,即:

            using (new MainForm())
            {
                Application.Run();
            }

然后在NotifyIcon的Click事件中显示MainForm就可以了。
可以参考他的文章(http://ly4cn.cnblogs.com/archive/2006/03/16/351205.html)。

这个方法的确是解决了这个问题;但是由于切断了主窗体和Application之间的联系,因此必须使用Application.Exit来关闭程序。这样做仍然有些不自然,且有一些安全隐患;那么到底有没有更好的方法呢?

一个方法就是使用ApplicationContext。

ApplicationContext实质上就是一个Application与主窗体之间的连接器,掌管着二者之间的互动关系。
其中最主要的,就是负责在主窗体关闭时结束线程。既然如此,我们只要根据需要自定义一个ApplicationContext就可以了:

    internal class HideOnStartupApplicationContext : ApplicationContext
    {
        
private Form mainFormInternal;// 构造函数,主窗体被存储在mainFormInternal
        public HideOnStartupApplicationContext(Form mainForm)
        {
            
this.mainFormInternal = mainForm;
            
            
this.mainFormInternal.Closed += new EventHandler(mainFormInternal_Closed);
        }
// 当主窗体被关闭时,退出应用程序
        void mainFormInternal_Closed(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }

随后,我们只需要在Main当中像下面这样调用既可:

        static void Main()
        {
            
//...

            HideOnStartupApplicationContext context 
= new HideOnStartupApplicationContext(new MainForm());
            Application.Run(context);
        }

这样,MainForm根本不用去考虑窗口与应用程序的关系,安安心心做它的“主窗口”就可以了。

此外,既然明白了ApplicationContext掌管着窗口结束与程序结束的关系,我们就可以用它来做出一些有趣的东西:
比如MSDN上就给出了一个“双主窗口”——同时显示2个Form,关闭其中任何一个都不会导致程序结束,只有全部关闭了才会结束程序。有兴趣的朋友可以参看MSDN关于ApplicationContext的资料,那里给出了详细的说明和范例代码。
再如,要设计一个“向导(Wizard)”类的程序,使用ApplicationContext也是最佳选择——它可以轻松控制Form到Form的流程,以及Form的启动与关闭,使得你的每个Form更加模块化,减少Form之间的依赖。