如何隐藏主窗体并仍然运行程序?
我想运行我的程序,图标显示在系统托盘中,但不从一开始就显示主窗体。
编辑:
lMainForm := new MainForm;
lMainForm.ShowInTaskbar := true;
Application.Run(lMainForm);
没有用。一旦执行Application.Run,主窗体就会与系统托盘中的图标一起显示。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过重写 SetVisibleCore() 方法来完成此操作。像这样:
注意 Load 事件不会触发。请务必将其中的任何代码移至构造函数(首选)或此覆盖。
此代码仅抑制窗口一次。您可以稍后调用 Show() 或设置 Visible = true 以使窗口正常显示。您通常会在 NotifyIcon 的上下文菜单项的 Click 事件处理程序中执行此操作。
You do it by overriding the SetVisibleCore() method. Like this:
Beware that the Load event won't fire. Be sure to move any code you have there to either the constructor (preferred) or this override.
This code suppresses the window only once. You can call Show() or setting Visible = true later to make the window show up as normal. You'd typically do so in the Click event handler of a context menu item for the NotifyIcon.
您目前遇到的问题是您正在调用
Application.Run
重载,将主窗体作为参数。这将显示您不想要的主窗体。相反,您应该调用其他之一
Application.Run
重载。例如,您可以调用 无参数重载
Application.Run
。在此之前,请确保您已创建并安装了通知图标。并且还创建但不显示您的主窗体。当您准备好显示主窗体时,为了响应通知图标上的操作,请调用
lMainForm.Show
。您还希望安排单击表单上的关闭按钮仅隐藏表单而不是关闭它。我假设您希望主表单实例始终隐藏在后台。因此,程序的顶层将如下所示:
您需要将一个项目添加到关闭应用程序的通知图标菜单中。实现此方法:
如果您需要对应用程序生命周期进行更细粒度的控制,那么您最好使用
Application.Run
重载 接收ApplicationContext
。由于我手头没有 Prism,我已经使用 C#/WinForms 检查了这一点,我希望它能顺利转移到 Prism!
The problem you have at the moment is that you are calling the
Application.Run
overload that takes the main form as a parameter. This will show the main form which you do not want.Instead you should call one of the other
Application.Run
overloads.For example you can call the no parameter overload of
Application.Run
. Make sure that you have created and installed your notify icon before you do so. And also create, but do not show your main form.When you are ready to show your main form, in response to an action on the notify icon, call
lMainForm.Show
. You will also wish to arrange that clicking the close button on the form merely hides the form rather than closing it. I'm assuming that you want your main form instance to persist hidden in the background.So the top-level of your program would look like this:
You'll need to add an item to the notify icon menu that closes the application. Implement this with:
If you need more fine grained control over the application lifetime then you may be better off using the
Application.Run
overload that receives anApplicationContext
.Since I don't have Prism at hand I have checked this out with C#/WinForms and I hope it transfers alright to Prism!
您是否尝试过:
或
Have you tried:
or