windows 暂时隐藏模态对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/696201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:14:08  来源:igfitidea点击:

Temporarily Hide a Modal Dialog

windowswinformswinapimodal-dialog

提问by Agnel Kurian

I have a modal dialog displayed with the main application window set as owner (as in dialog.ShowDialog(mainAppWindow)) and I would like to temporarily allow the user to interact with the main application window before returning to the modal dialog again.

我有一个模态对话框,主应用程序窗口设置为所有者(如dialog.ShowDialog(mainAppWindow)),我想暂时允许用户在再次返回模态对话框之前与主应用程序窗口进行交互。

How can I do this? I am using Windows Forms.

我怎样才能做到这一点?我正在使用 Windows 窗体。

Edit:AutoCAD does this well.

编辑:AutoCAD 在这方面做得很好。

回答by Ed S.

Then I do not think that you want a modal dialog...

那么我不认为你想要一个模态对话框......

The whole purpose of a modal dialog is that the user cannot do anythinguntil they have gotten rid of it in some way. I think that you should just create your own form class to act the way that you would like.

模态对话框的全部目的是用户在以某种方式摆脱它之前不能做任何事情。我认为您应该创建自己的表单类来按照您想要的方式行事。

回答by Hans Passant

Just close the modal dialog. It doesn't get disposed like normal Form instances do so you simply bring it back alive by setting its DialogResult property back to None and calling ShowDialog() again.

只需关闭模态对话框。它不会像普通的 Form 实例那样被处理,因此您只需通过将其 DialogResult 属性设置回 None 并再次调用 ShowDialog() 来将其恢复。

Note that calling Hide() on a modal dialog also closes it, necessarily so since all of your app's windows are disabled. No different from Close().

请注意,在模态对话框上调用 Hide() 也会关闭它,这是必然的,因为您的应用程序的所有窗口都已禁用。与 Close() 没有区别。

回答by Igor Brejc

Take a look at http://en.wikipedia.org/wiki/Modal_window#Criticisms... There's a school of thought that you shouldn't use modal windows in the first place.

看看http://en.wikipedia.org/wiki/Modal_window#Criticisms... 有一种观点认为你不应该首先使用模态窗口。

回答by grover

You need to enable the parent window again. For modal dialogs, Windows automatically disables the parent window and reenables it if the modal dialog was closed.

您需要再次启用父窗口。对于模态对话框,如果模态对话框关闭,Windows 会自动禁用父窗口并重新启用它。

I haven't tried, but it should be sufficient to set the Enabled property of your parent form to true. If that doesn't work using the EnableWindow Win32 API does work.

我还没有尝试过,但是将父表单的 Enabled 属性设置为 true 应该就足够了。如果这不起作用,则使用 E​​nableWindow Win32 API 确实有效。

回答by Agnel Kurian

The modal/modeless paradigm is that if you want the user to be able to interact with the main application, use a modeless window and if you don't, use a modal. If you want to stop him using the main application - but then use it - but then not use it - your user interface design does not work with the modal/modeless paradigm.

模态/非模态范式是,如果您希望用户能够与主应用程序交互,请使用非模态窗口,否则,请使用模态窗口。如果您想阻止他使用主应用程序 - 但随后使用它 - 但随后不使用它 - 您的用户界面设计不适用于模态/非模态范式。

回答by Luká? Rube?

For some reason I have to face same problem in .NET. I have (main) form showing modal dialog, which I need to hide, interact with main window, and return to the modal dialog again.

出于某种原因,我不得不在 .NET 中面临同样的问题。我有(主)表单显示模态对话框,我需要隐藏它,与主窗口交互,然后再次返回模态对话框。

I personally do not understand consequences from the Windows (API) point of view, but following solution works for me.

我个人不明白从 Windows (API) 的角度来看的后果,但以下解决方案对我有用。

Whole trick lies in setting main form to disabled before showing modal dialog (when main form is not set to Enabled = false explicitly, then after hiding modal dialog no interaction can be done with it even when Enabled = true is called).

整个技巧在于在显示模态对话框之前将主窗体设置为禁用(当主窗体未显式设置为 Enabled = false 时,隐藏模态对话框后即使调用 Enabled = true 也无法与它进行交互)。

As response to modal dialog event (called NeedInteraction), I just hide modal dialog, enable main form, in some loop do interaction with user, disable main dialog and show modal dialog again.

作为对模态对话框事件(称为NeedInteraction)的响应,我只是隐藏模态对话框,启用主窗体,在某些循环中与用户进行交互,禁用主对话框并再次显示模态对话框。

void ShowDialog()
{
  var dialog = new MyModalForm();
  dialog.NeedInteraction += (sender, eventArgs) =>
  {
    dialog.Hide();
    Enabled = true;

    //wait till user finishes working with main window

    Enabled = false;
    dialog.Show();
  }

  Enabled = false;
  dialog.ShowDialog();
  Enabled = true; //don't forget to make it enabled afterwards
}

It might not be clean solution (as is not need for hiding modal dialog), but it works at least for my situation.

它可能不是干净的解决方案(因为不需要隐藏模式对话框),但它至少适用于我的情况。