简体   繁体   English

c#中ShowDialog()和ShowDialog(IWin32Window)有什么区别?

[英]What's difference between ShowDialog() and ShowDialog(IWin32Window) in c#?

Both ShowDialog(); 两个ShowDialog(); and ShowDialog(IWin32Window); ShowDialog(IWin32Window); seem to do the exact same thing to me. 似乎对我做同样的事情。 The documentation isn't very clear either. 文档也不是很清楚。

I've been told that ShowDialog(IWin32Window); 我被告知ShowDialog(IWin32Window); will ensure that the dialog window is on top of the whatever is passed in as the owner window. 将确保对话框窗口位于作为所有者窗口传入的内容之上。

The MSDN documenation makes no mention of this, so it feels like a bit of black magic. MSDN文档没有提到这一点,所以感觉有点像黑魔法。

http://msdn.microsoft.com/en-us/library/w61zzfwe.aspx makes it pretty clear that the parameter represents the owner window. http://msdn.microsoft.com/en-us/library/w61zzfwe.aspx非常清楚该参数代表了所有者窗口。 And that is the behavior of an owner window - it stays behind its children and doesn't close leaving its children still displayed. 这就是所有者窗口的行为 - 它保留在其子项之后,并且不会关闭而使其子项仍然显示。

When ShowDialog() is called, the currently active window is made the owner of the dialog box. 调用ShowDialog()时,当前活动窗口成为对话框的所有者。 If you want to specify a specific owner, use the other version of this method (ShowDialog(IWin32Window)). 如果要指定特定所有者,请使用此方法的其他版本(ShowDialog(IWin32Window))。

This is stated in the MSDN documenation. 这在MSDN文档中说明。

http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx

Internally ShowDialog() calls ShowDialog(IWin32Window) with a null argument. 在内部, ShowDialog()使用null参数调用ShowDialog(IWin32Window) If the argument is not null, it is set as the form's owner. 如果参数不为null,则将其设置为表单的所有者。 If owner is null, it will use GetActiveWindow() to get a IntPtr to the active window and use this as the owner. 如果owner为null,它将使用GetActiveWindow()IntPtr获取到活动窗口并将其用作所有者。 You can check it out yourself using ILSpy . 您可以使用ILSpy自行查看

.NET allows a form to “own” other forms. .NET允许表单“拥有”其他表单。 Owned forms are useful for floating toolbox and command windows. 拥有的表单对浮动工具箱和命令窗口很有用。 One example of an owned form is the Find and Replace window in Microsoft Word. 拥有的表单的一个示例是Microsoft Word中的“查找和替换”窗口。 When an owner window is minimized, the owned forms are also minimized automatically. 当所有者窗口最小化时,所拥有的表单也会自动最小化。 When an owned form overlaps its owner, it is always displayed on top. 当拥有的表单与其所有者重叠时,它始终显示在顶部。

(c) "Pro .NET 2.0 Windows Forms and Custom Controls" by Matthew MacDonald. (c)Matthew MacDonald的“Pro .NET 2.0 Windows窗体和自定义控件”。


As ShowDialog shows the new form, an implicit relationship is established between the currently active form, known as the owner form, and the new form, known as the owned form. ShowDialog显示新表单时,在当前活动表单(称为所有者表单)和新表单(称为拥有表单)之间建立隐式关系 This relationship ensures that the owned form is the active form and is always shown on top of the owner form. 此关系可确保拥有的表单是活动表单,并始终显示在所有者表单的顶部。

One feature of this relationship is that the owned form affects the behavior of its owner form (when using ShowDialog ): 这种关系的一个特征是拥有的表单会影响其所有者表单的行为(使用ShowDialog时 ):

  • The owner form cannot be minimized, maximized, or even moved. 所有者表单无法最小化,最大化甚至移动。
  • The owned form blocks mouse and keyboard input to the owner form. 拥有的表单阻止鼠标和键盘输入到所有者表单。
  • The owner form is minimized when the owned form is. 拥有的表单最小化所有者表单。
  • Only the owned form can be closed. 只能关闭所拥有的表格。
  • If both owner and owned forms are minimized and if the user presses Alt+Tab to switch to the owned form, the owned form is activated. 如果所有者和拥有的表单都被最小化,并且如果用户按Alt + Tab切换到拥有的表单,则激活拥有的表单。

Unlike the ShowDialog method, however, a call to the Show method does not establish an implicit owner-owned relationship . 但是,与ShowDialog方法不同,对Show方法的调用不会建立隐式所有者拥有的关系 This means that either form can be the currently active form. 这意味着任何一种形式都可以是当前活动的形式。

Without an implicit owner-owned relationship, owner and owned forms alike can be minimized, maximized, or moved. 如果没有隐含的所有者拥有的关系,所有者和拥有的表单都可以最小化,最大化或移动。 If the user closes any form other than the main form, the most recently active form is reactivated. 如果用户关闭除主窗体之外的任何窗体,则会重新激活最近激活的窗体。

Although ShowDialog establishes an implicit owner-owned relationship , there is no built-in way for the owned form to call back to or query the form that opened it. 尽管ShowDialog建立了一个隐式的所有者拥有的关系 ,但是拥有的表单没有内置的方式来回调或查询打开它的表单。 In the modeless case, you can set the new form's Owner property to establish the owner-owned relationship. 在无模式的情况下,您可以设置新表单的所有者属性以建立所有者拥有的关系。 As a shortcut, you could pass the owner form as an argument to an overload of the Show method, which also takes an IWin32Window parameter (IWin32Window is implemented by Windows Forms UI objects that expose a Win32 HWND property via the IWin32Window.Handle property). 作为一种快捷方式,您可以将所有者表单作为参数传递给Show方法的重载,该方法也采用IWin32Window参数(IWin32Window由通过IWin32Window.Handle属性公开Win32 HWND属性的Windows窗体UI对象实现)。

The behavior of forms in an explicit modal owner-owned form relationship is the same as its implicit modal counterpart, but the modeless owner-owned relationship provides additional behavior in the non-owner-owned modeless case. 显式模态所有者拥有的表单关系中的表单行为与其隐式模式对应关系相同,但无模式所有者拥有的关系在非所有者拥有的无模式情况下提供了额外的行为。 First, the modeless owned form always appears on top of the owner form, even though either can be active. 首先,无模式拥有的表单始终显示在所有者表单的顶部,即使其中任何一个都可以处于活动状态。 This is useful when you need to keep a form, such as a floating tool window, on top of other forms within an application. 当您需要在应用程序中的其他表单之上保留表单(例如浮动工具窗口)时,这非常有用。 Second, if the user presses Alt+Tab to switch from the owner, the owned forms follow suit. 其次,如果用户按Alt + Tab从所有者切换,则拥有的表单也会跟随。 To ensure that the user knows which form is the main form, minimizing the owner hides the task bar buttons for all owned forms, leaving only the owner's task bar button visible. 为了确保用户知道哪个表单是主表单,最小化所有者会隐藏所有自有表单的任务栏按钮,只显示所有者的任务栏按钮。

(c) "Windows Forms 2.0 Programming" by Chris Sells, Michael Weinhardt. (c)Chris Sells的“Windows Forms 2.0 Programming”,Michael Weinhardt。

frm2.ShowDialog() make frm2 as a model dialog box, we can not click anywhere on form1 unless we close the instance of frm2. frm2.ShowDialog()使frm2成为模型对话框,除非我们关闭frm2的实例,否则我们无法在form1上的任何地方单击。

frm2.show() there is no such restriction. frm2.show()没有这样的限制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .Owner属性和ShowDialog(IWin32Window所有者)之间的区别? - Difference between .Owner property and ShowDialog(IWin32Window owner)? C#WinForms:在另一个线程中具有IWin32Window owner参数的Form.ShowDialog() - C# WinForms: Form.ShowDialog() with IWin32Window owner parameter in a different thread WPF 等效于 Form.ShowDialog(IWin32Window) - WPF equivalent of Form.ShowDialog(IWin32Window) C# 中的 IWin32Window? - IWin32Window in C#? Form.ShowDialog(IWin32Window) 是否应该与任何 window 句柄一起使用? - Should Form.ShowDialog(IWin32Window) work with any window handle? ShowDialog(所有者)隐藏在/不显示,即使我正在提供所有者IWin32Window - ShowDialog(owner) is hiding behind/not showing even though I am supplying owner IWin32Window C#未处理的异常'必须在创建第一个IWin32Window对象之前调用SetCompatibleTextRenderingDefault' - C# Unhandled Exception 'SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created' Application.Run()和Form.ShowDialog()之间有什么区别? - What's the difference between Application.Run() and Form.ShowDialog()? 如何在SetCompatibleTextRenderingDefault抛出InvalidOperationException时找出IWin32Window已创建的内容? - How to find out what IWin32Window has been created when SetCompatibleTextRenderingDefault throws an InvalidOperationException? 获取Window ShowDialog取消和关闭之间的区别 - Get difference between Window ShowDialog cancel and close
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM