为什么要使用的MessageBox.show一个所有者窗口?所有者、要使、窗口、show

2023-09-03 01:52:04 作者:下个夏天,我们也该散了

的MessageBox.show具有形式类似的MessageBox.show(ownerWindow,....)。

MessageBox.Show has forms like MessageBox.Show( ownerWindow, .... ).

我该怎么获得通过指定所有者窗口?

What do I gain by assigning a owner window?

推荐答案

一个消息框是一个模式的形式,这意味着它的父窗口被禁止,直到消息框,被罚下场。

A message box is a modal form, which means that its parent window is disabled until the message box is dismissed.

如果一个Show()重载被称为不采取业主标识符,那么父窗体通常是自动选择。我找不到描述这种形式是如何选择的文档中任何东西,但是我的经验是,如果消息框显示GUI线程内(即主线程,或者消息泵线程),则活动窗口该线程被选为父

If a Show() overload is called that does not take an owner identifier, then a parent form is usually chosen automatically. I can't find anything in the documentation that describes how that form is chosen, but my experience is that if the message box is displayed inside the GUI thread (i.e., the main thread, or message pump thread), then the active window for that thread is chosen as the parent.

其他线程可能会产生的消息框没有父窗体。这可能是一个糟糕的情况,因为它可以坐在活动窗口的后面,而用户甚至不知道它的存在,直到他们关闭程序。为了避免这种情况,你可以通过应用程序的主窗口的句柄Show方法,这将禁用窗口消息框的持续时间。

Other threads may create message boxes with no parent form. This could be a bad situation, because it could sit behind the active window, and the user will not even know it is there until they close the program. To avoid this, you could pass the handle of the application's main window to the Show method, which will disable that window for the duration of the message box.

增加:我一直在想这件事,现在我不那么肯定。从反射器,你都给下面的代码片段让我觉得,也许是线程无所谓。 (我没有说,我找不到文档中的任何东西!)

ADDED: I've been thinking about this and now I'm not so sure. The snippet from reflector that you gave below makes me think that maybe the thread doesn't matter. (I did say that I couldn't find anything in the documentation!)

我不得不回去,并期待在code,以确保,但我想,我曾经失去的主要形式背后的消息框可能实际上已经自定义消息框的形式,在这种情况下,我经验是错误的,你永远需要提供父表单参数。

I'd have to go back and look at the code to make sure, but I think the message boxes that I used to lose behind the main form may have actually been custom message box forms, in which case my experience is faulty, and you never ever have to supply a parent form parameter.

抱歉的混乱。

我的建议,现在是,除非你需要的东西以外的活动窗口是主要的形式从来没有提供此参数。

My recommendation now is to never supply this parameter unless you need something other than the active window to be the main form.