没有主窗体的 Winforms 应用程序 - 对话框未显示在顶部

发布于 2024-09-06 05:27:34 字数 434 浏览 4 评论 0原文

我有一个 winforms 应用程序,它实际上没有主窗体 - 它只是一个简单的文件处理工具,因此它实际上不需要一个主窗体。

当应用程序启动时,首先使用 OpenFileDialog 来选择文件。然后,使用 Showdialog() 显示一个对话框,其中包含一些有关如何处理所选文件的选项。最后,在操作文件的同时会显示一个进度条形式,然后应用程序退出。

问题是,当我从 Visual Studio 进行调试时,中间窗体不会显示在其他打开的窗口之上,例如 Visual Studio 本身。然而,当我运行编译后的可执行文件时,它似乎工作正常。

通常,当 ShowDialog 导致后面出现一个对话框时,解决方案是使用 Showdialog(owner) 调用,除非我没有主窗体可言,我没有要设置的所有者。

那么,如何才能使该对话框像 OpenFileDialog 自动那样显示在其他窗口之上呢?

I have a winforms application that doesn't really have a main form - it's just a simple file handling tool so it doesn't really need one.

When the application starts, first an OpenFileDialog is used to select a file. Then, a dialog box is shown using Showdialog() with some options for how the selected file should be processed. Finally, a progress bar form is displayed whilst the file is operated on, and the application exits.

The problem is that when I'm debugging from Visual Studio, the middle form doesn't get shown on top of other open windows, such as Visual Studio itself. When I run the compiled executable however it seems to work fine.

Usually, when ShowDialog results in a dialog that comes up behind, the solution is to use the Showdialog(owner) call, except as I don't have a main form to speak of I don't have an owner to set.

So, how can I make the dialog box come up on top of other windows in the same way that the OpenFileDialog automatically does?

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

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

发布评论

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

评论(1

萤火眠眠 2024-09-13 05:27:34

我确切地知道你在说什么,问题实际上是你的应用程序的设计与 Windows 处理焦点的方式不一致。 Windows从win31开始就做了这种事情。这就像抢地盘;根据比尔的说法,如果你在应用程序中显示一个窗口,然后隐藏/禁用它,你打瞌睡,你就输了。解决办法:你需要一个主窗口!或者切换操作系统。仅仅因为它在 EXE 中运行并不意味着当您的用户之一从批处理文件或资源管理器之外的其他文件启动它时它也会运行。

你可以想办法摆脱这个困境——运行 3 个独立的 exe;尝试在子 Main 中的 3 个 Application.Run 线程上运行;自己使用SetTopMost、AppActivate。

或者查找如何使您的应用程序使用 SDK 调用来获取输入焦点 - Windows 的连续版本已经变得困难,以减轻使用它的令人讨厌的应用程序(因此,可以在 w98 中执行此操作的应用程序将在 xp 中使任务栏闪烁橙色) - 然后程序员当然就解决了这个问题。你可以这么做。

但你这样做是错误的。重构是为了未来应用程序的策略和改变。这不是我做的决定; Windows 的工作方式是一成不变的,加上用户期望它工作的方式是在数以百万计的大脑中确定的,这比石头更难 - 随波逐流!

轻松重构,没有实际影响:

  • 备份您的项目(或在 TFS 中搁置副本)
  • 在选项对话框中,剪切所有控件。放入一个名为 pnlOpts 的表单大小的面板。将它们粘贴回来。
  • 将该表单作为您的主要表单。
  • 在 form_shown 中,设置 pnlOpts.Visible=false,或者只是默认这种方式(如果你想变得有点垃圾,你可以显示另一个带有应用程序名称或背景图像的面板;-)
  • 调用也显示的设置代码该区域的文件 dlg 也
  • 将文件 dlg 设置为将该表单作为父级,启动位置作为中心父级(我的意思是所有者不是父级)。请记住从此取消 = 关闭设置表单
  • 完成此操作后,显示 pnlOpts。
  • 在“确定”按钮中,调用执行进度条的代码。您可以在此处再次隐藏或禁用 pnlOpts。
  • 您在任何时候都不能在整个窗口上隐藏、设置 Visible=false 或enabled=false,否则您将引发 Windows 的聚焦愤怒(这就是您在这里痛苦的原因)。

这里真正的关键是一行答案:让主窗口始终可见并启用,如果您想禁用其功能,只需灰色或隐藏其所有控件即可。这是最容易做到的与面板。如果您愿意,您可以向用户显示其他面板或图像来娱乐他们(特别是如果将内容变灰 [.enabled=false] 没有意义)。

您不会喜欢这样的外观,或者您一开始就会像这样构建它,但是数百万行 Windows 代码和数百万 Windows 用户的大脑现在正在被动地、积极地不同意您的观点!

I know exactly what you're talking about and the problem is actually with the design of your app disagreeing with the way windows handles the focus. Windows has done this type of thing since win31. It's like a land grab; if you show a window from your app, then hide/disable it, You snooze, you lose according to Bill. The fix: you need a main window! Or switch OS. Just because it works from an EXE doesn't mean it will work when one of your users kicks it off from a batchfile or other than explorer.

You CAN kludge your way out of this - run 3 separate exes; try running on 3 Application.Run threads in your sub Main; use to SetTopMost, AppActivate yourself.

Or look up how to cause your app to grab the input focus using SDK calls - which successive versions of windows have made difficult in order to mitigate obnoxious apps using it (hence an app that could do this in w98 will flash the taskbar orange in xp) - and then programmers have worked around that of course. You could do that.

But you'd be doing it wrong. A refactor is in order and change in stragegy for future apps. This is not a decision I made; the way windows works is set in stone, plus the way users expect it to work is set in millions of brains, which is harder than stone - swim with the flow!

Easy refactor with no real impact:

  • Back up your project (or shelve a copy in TFS)
  • In your options dialog, cut all your controls. Put in a form-sized panel named pnlOpts. Paste them back.
  • Make that form your main form.
  • In the form_shown, set pnlOpts.Visible=false, or just default it that way (you can show another panel w/ the app name or a background image if you want to be a little trashy ;-)
  • Call the setup code that also shows the file dlg from this area as well
  • Set your file dlg to have that form as parent, startup position as center-parent (I mean owner not parent). Remember cancel from this = close the settings form
  • When this is done, show pnlOpts.
  • In the OK button, call the code that does the progress bar. You can hide or disable pnlOpts here again.
  • At no point can you hide, set Visible=false, or enabled=false on the ENTIRE window or you will invoke windows' focusing wrath (which is the cause of your misery here).

The real key here, the one-line answer: leave a main window visible and enabled at all times, if you want to disable its functionality, just gray or hide all its controls. this is easiest to do with the panel. You can show the user other panels or images to entertain them if you want (especially if graying [.enabled=false] the content doesn't make sense).

You won't like that way that looks, or you'd have built it like that to begin with, but millions of lines of windows code and millions of brains of windows users are passive-agressively disagreeing with you right now!

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