C#消息框最小化问题

发布于 2024-12-03 07:31:08 字数 594 浏览 1 评论 0原文

我的消息框遇到了一个小问题。如果我直接运行该程序(例如,双击它并从那里使用它),它们就可以正常工作。

我有一些 .ext 文件,双击后运行该程序(它们与我的程序相关联)。现在,当消息框经过那里时,它们会被显示,但它们被最小化,我必须在任务栏上手动单击它们才能看到它们。

有谁知道这是为什么?我在我的 Program.cs 中运行此代码:

static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    var MyForm = new Form1();
    if (args.Length != 0)
    {
        MyForm.RunMsg(); // this is a function I am calling
        Application.Exit();
    }
    else
        Application.Run(MyForm);
}

任何帮助将不胜感激。是的,我可以确认消息框仅在运行 .ext 文件时最小化。

I'm facing a small problem with my message boxes. If I run the program directly (as in, double clicking it and using it from there), they work fine.

I have some .ext files than when double clicked, run through the program (they are associated with my program). Now when message boxes run through there, they are shown, but they are minimized and I have to manually click them on the Task Bar to see them.

Does anyone know why this is? I have this code running in my Program.cs:

static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    var MyForm = new Form1();
    if (args.Length != 0)
    {
        MyForm.RunMsg(); // this is a function I am calling
        Application.Exit();
    }
    else
        Application.Run(MyForm);
}

Any help would be appreciated. And yes I can confirm the message boxes are only minimized when running the .ext files.

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

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

发布评论

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

评论(2

柠栀 2024-12-10 07:31:08

我非常严重怀疑它们实际上被最小化了。更有可能的是它们隐藏在另一个应用程序的窗口后面。是的,在这种情况下可能会出现这种情况,因为您没有立即创建窗口。您可能会处理传递的文件一段时间,然后尝试告诉用户您已经完成了。为时已晚,仍然无法获得焦点,Windows对此有非常严格的规则,以避免“在用户面前扔一个窗口”综合症。不仅令人烦恼,它还可能失败,因为用户可能在使用鼠标或键盘时意外关闭窗口,甚至没有注意到有一个窗口。如果您曾经不小心启动了 Windows 更新安装,那么您就会明白我的意思(现已修复)。

不要使用消息框。创建进度表单以便您可以获得焦点并显示进度,或者使用 NotifyIcon。

I very seriously doubt they are actually minimized. Much more likely is that they are hidden behind the window of another application. Yes, that's likely in this scenario because you don't create a window right away. You probably crunch on the passed file for a while, then try to tell the user that you are done. Too late to still be able to acquire the focus, Windows has very strict rules about this to avoid the "throw a window in the user's face" syndrome. Not just annoying, it is also likely to fail because the user might accidentally close the window while mousing or keyboarding without even noticing that there was a window. If you ever accidentally started a Windows Update install then you know what I mean (now fixed).

Don't use a message box. Either create a progress form so that you can acquire the focus and show progress or use a NotifyIcon.

哑剧 2024-12-10 07:31:08

更好的方法可能是向表单添加一个新的构造函数,该构造函数接受一个或多个表示命令行中传递的值的参数,如下所示:-

static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    var parsedArgs = Parse(args);
    Application.Run(new Form1(parsedArgs));
}

出于示例的目的,Parse()是一个静态方法,给定命令行参数,它返回某种适当的类型。如果未提供命令行参数,则返回类型可以为 null,也可以为非 null 且其属性具有默认值。

使用此方法,您的表单可以根据构造函数中传递的参数决定如何进行适当的操作,并且您仍然可以获得 Application.Run() 的设置、生命周期和拆卸的好处为你做。

A better approach may be to add a new constructor to your form which accepts an argument or arguments which represent the values passed in the command line, as follows:-

static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    var parsedArgs = Parse(args);
    Application.Run(new Form1(parsedArgs));
}

For the purpose of the example, Parse() is a static method which, given the command line arguments, returns some kind of appropriate type. If there are no command line arguments supplied, the return type can either be null, or can be non-null with default values for it's properties.

Using this method, your form can decide how to act appropriately according to the argument(s) passed in the constructor, and you still get the benefit of the setup, lifetime and teardown which Application.Run() does for you.

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