如何隐藏主窗体并仍然运行程序?

发布于 2024-12-25 05:55:02 字数 245 浏览 1 评论 0 原文

我想运行我的程序,图标显示在系统托盘中,但不从一开始就显示主窗体。

编辑:

  lMainForm := new MainForm; 
  lMainForm.ShowInTaskbar := true;
  Application.Run(lMainForm);

没有用。一旦执行Application.Run,​​主窗体就会与系统托盘中的图标一起显示。

I want to run my program with the icon showing in the system tray but without the mainform showing from the start.

Edit:

  lMainForm := new MainForm; 
  lMainForm.ShowInTaskbar := true;
  Application.Run(lMainForm);

Didn't work. As soon as Application.Run is executed, mainform is displayed along with the icon in the system tray.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

究竟谁懂我的在乎 2025-01-01 05:55:02

您可以通过重写 SetVisibleCore() 方法来完成此操作。像这样:

    protected override void SetVisibleCore(bool value) {
        if (!this.IsHandleCreated) {
            CreateHandle();
            value = false;
        }
        base.SetVisibleCore(value);
    }

注意 Load 事件不会触发。请务必将其中的任何代码移至构造函数(首选)或此覆盖。

此代码仅抑制窗口一次。您可以稍后调用 Show() 或设置 Visible = true 以使窗口正常显示。您通常会在 NotifyIcon 的上下文菜单项的 Click 事件处理程序中执行此操作。

You do it by overriding the SetVisibleCore() method. Like this:

    protected override void SetVisibleCore(bool value) {
        if (!this.IsHandleCreated) {
            CreateHandle();
            value = false;
        }
        base.SetVisibleCore(value);
    }

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.

只等公子 2025-01-01 05:55:02

您目前遇到的问题是您正在调用 Application.Run 重载,将主窗体作为参数。这将显示您不想要的主窗体。

相反,您应该调用其他之一 Application.Run 重载。

例如,您可以调用 无参数重载 Application.Run。在此之前,请确保您已创建并安装了通知图标。并且还创建但不显示您的主窗体。

当您准备好显示主窗体时,为了响应通知图标上的操作,请调用 lMainForm.Show。您还希望安排单击表单上的关闭按钮仅隐藏表单而不是关闭它。我假设您希望主表单实例始终隐藏在后台。

因此,程序的顶层将如下所示:

//create and show the notify icon here
lMainForm := new MainForm; 
lMainForm.ShowInTaskbar := true;
lMainForm.Visible := false;//I believe this is the default in any case
Application.Run;

您需要将一个项目添加到关闭应用程序的通知图标菜单中。实现此方法:

Application.Exit;

如果您需要对应用程序生命周期进行更细粒度的控制,那么您最好使用 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:

//create and show the notify icon here
lMainForm := new MainForm; 
lMainForm.ShowInTaskbar := true;
lMainForm.Visible := false;//I believe this is the default in any case
Application.Run;

You'll need to add an item to the notify icon menu that closes the application. Implement this with:

Application.Exit;

If you need more fine grained control over the application lifetime then you may be better off using the Application.Run overload that receives an ApplicationContext.

Since I don't have Prism at hand I have checked this out with C#/WinForms and I hope it transfers alright to Prism!

屌丝范 2025-01-01 05:55:02

您是否尝试过:

lMainForm.WindowState := System.Windows.Forms.FormWindowState.Minimized;

lMainForm.Hide(); // call on application start

Have you tried:

lMainForm.WindowState := System.Windows.Forms.FormWindowState.Minimized;

or

lMainForm.Hide(); // call on application start
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文