|
2 | 2 | title: "Creating and Managing Modal Dialog Boxes | Microsoft Docs"
|
3 | 3 | ms.date: "11/04/2016"
|
4 | 4 | ms.topic: "conceptual"
|
5 |
| -helpviewer_keywords: |
| 5 | +helpviewer_keywords: |
6 | 6 | - "dialog boxes, managing in Visual Studio"
|
7 | 7 | ms.assetid: 491bc0de-7dba-478c-a76b-923440e090f3
|
8 | 8 | author: "gregvanl"
|
9 | 9 | ms.author: "gregvanl"
|
10 | 10 | manager: jillfra
|
11 |
| -ms.workload: |
| 11 | +ms.workload: |
12 | 12 | - "vssdk"
|
13 | 13 | ---
|
14 | 14 | # Create and manage modal dialog boxes
|
15 |
| -When you create a modal dialog box inside Visual Studio, you must make sure that the parent window of the dialog box is disabled while the dialog box is displayed, then re-enable the parent window after the dialog box is closed. If you do not do so, you may receive the error: *Microsoft Visual Studio cannot shut down because a modal dialog is active. Close the active dialog and try again.* |
16 |
| - |
17 |
| - There are two ways of doing this. The recommended way, if you have a WPF dialog box, is to derive it from <xref:Microsoft.VisualStudio.PlatformUI.DialogWindow>, and then call <xref:Microsoft.VisualStudio.PlatformUI.DialogWindow.ShowModal%2A> to display the dialog box. If you do this, you do not need to manage the modal state of the parent window. |
18 |
| - |
19 |
| - If your dialog box is not WPF, or for some other reason you cannot derive your dialog box class from <xref:Microsoft.VisualStudio.PlatformUI.DialogWindow>, then you must get the parent of the dialog box by calling <xref:Microsoft.VisualStudio.Shell.Interop.IVsUIShell.GetDialogOwnerHwnd%2A> and manage the modal state yourself, by calling the <xref:Microsoft.VisualStudio.Shell.Interop.IVsUIShell.EnableModeless%2A> method with a parameter of 0 (false) before displaying the dialog box and calling the method again with a parameter of 1 (true) after closing the dialog box. |
20 |
| - |
21 |
| -## Create a dialog box derived from DialogWindow |
22 |
| - |
23 |
| -1. Create a VSIX project named **OpenDialogTest** and add a menu command named **OpenDialog**. For more information about how to do this, see [Create an extension with a menu command](../extensibility/creating-an-extension-with-a-menu-command.md). |
24 |
| - |
25 |
| -2. To use the <xref:Microsoft.VisualStudio.PlatformUI.DialogWindow> class, you must add references to the following assemblies (in the Framework tab of the **Add Reference** dialog box): |
26 |
| - |
27 |
| - - *PresentationCore* |
28 |
| - |
29 |
| - - *PresentationFramework* |
30 |
| - |
31 |
| - - *WindowsBase* |
32 |
| - |
33 |
| - - *System.Xaml* |
34 |
| - |
35 |
| -3. In *OpenDialog.cs*, add the following `using` statement: |
36 |
| - |
37 |
| - ```csharp |
38 |
| - using Microsoft.VisualStudio.PlatformUI; |
39 |
| - ``` |
40 |
| - |
41 |
| -4. Declare a class named `TestDialogWindow` that derives from <xref:Microsoft.VisualStudio.PlatformUI.DialogWindow>: |
42 |
| - |
43 |
| - ```csharp |
44 |
| - class TestDialogWindow : DialogWindow |
45 |
| - {. . .} |
46 |
| - ``` |
47 |
| - |
48 |
| -5. To be able to minimize and maximize the dialog box, set <xref:Microsoft.VisualStudio.PlatformUI.DialogWindowBase.HasMaximizeButton%2A> and <xref:Microsoft.VisualStudio.PlatformUI.DialogWindowBase.HasMinimizeButton%2A> to true: |
49 |
| - |
50 |
| - ```csharp |
51 |
| - internal TestDialogWindow() |
52 |
| - { |
53 |
| - this.HasMaximizeButton = true; |
54 |
| - this.HasMinimizeButton = true; |
55 |
| - } |
56 |
| - ``` |
57 |
| - |
58 |
| -6. In the `OpenDialog.ShowMessageBox` method, replace the existing code with the following: |
59 |
| - |
60 |
| - ```csharp |
61 |
| - TestDialogWindow testDialog = new TestDialogWindow(); |
62 |
| - testDialog.ShowModal(); |
63 |
| - ``` |
64 |
| - |
65 |
| -7. Build and run the application. The experimental instance of Visual Studio should appear. On the **Tools** menu of the experimental instance you should see a command named **Invoke OpenDialog**. When you click this command, you should see the dialog window. You should be able to minimize and maximize the window. |
66 |
| - |
67 |
| -## Create and manage a dialog box not derived from DialogWindow |
68 |
| - |
69 |
| -1. For this procedure, you can use the **OpenDialogTest** solution you created in the previous procedure, with the same assembly references. |
70 |
| - |
71 |
| -2. Add the following `using` declarations: |
72 |
| - |
73 |
| - ```csharp |
74 |
| - using System.Windows; |
75 |
| - using Microsoft.Internal.VisualStudio.PlatformUI; |
76 |
| - ``` |
77 |
| - |
78 |
| -3. Create a class named `TestDialogWindow2` that derives from <xref:System.Windows.Window>: |
79 |
| - |
80 |
| - ```csharp |
81 |
| - class TestDialogWindow2 : Window |
82 |
| - {. . .} |
83 |
| - ``` |
84 |
| - |
85 |
| -4. Add a private reference to <xref:Microsoft.VisualStudio.Shell.Interop.IVsUIShell>: |
86 |
| - |
87 |
| - ``` |
88 |
| - private IVsUIShell shell; |
89 |
| - ``` |
90 |
| - |
91 |
| -5. Add a constructor that sets the reference to <xref:Microsoft.VisualStudio.Shell.Interop.IVsUIShell>: |
92 |
| - |
93 |
| - ```csharp |
94 |
| - public TestDialogWindow2(IVsUIShell uiShell) |
95 |
| - { |
96 |
| - shell = uiShell; |
97 |
| - } |
98 |
| - ``` |
99 |
| - |
100 |
| -6. In the `OpenDialog.ShowMessageBox` method, replace the existing code with the following: |
101 |
| - |
102 |
| - ```csharp |
103 |
| - IVsUIShell uiShell = (IVsUIShell)ServiceProvider.GetService(typeof(SVsUIShell)); |
104 |
| - |
105 |
| - TestDialogWindow2 testDialog2 = new TestDialogWindow2(uiShell); |
106 |
| - //get the owner of this dialog |
107 |
| - IntPtr hwnd; |
108 |
| - uiShell.GetDialogOwnerHwnd(out hwnd); |
109 |
| - testDialog2.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner; |
110 |
| - uiShell.EnableModeless(0); |
111 |
| - try |
112 |
| - { |
113 |
| - WindowHelper.ShowModal(testDialog2, hwnd); |
114 |
| - } |
115 |
| - finally |
116 |
| - { |
117 |
| - // This will take place after the window is closed. |
118 |
| - uiShell.EnableModeless(1); |
119 |
| - } |
120 |
| - ``` |
121 |
| - |
122 |
| -7. Build and run the application. On the **Tools** menu you should see a command named **Invoke OpenDialog**. When you click this command, you should see the dialog window. |
| 15 | +When you create a modal dialog box inside Visual Studio, you must make sure that the parent window of the dialog box is disabled while the dialog box is displayed, then re-enable the parent window after the dialog box is closed. If you do not do so, you may receive the error: *Microsoft Visual Studio cannot shut down because a modal dialog is active. Close the active dialog and try again.* |
| 16 | + |
| 17 | +There are two ways of doing this. The recommended way, if you have a WPF dialog box, is to derive it from <xref:Microsoft.VisualStudio.PlatformUI.DialogWindow>, and then call <xref:Microsoft.VisualStudio.PlatformUI.DialogWindow.ShowModal%2A> to display the dialog box. If you do this, you do not need to manage the modal state of the parent window. |
| 18 | + |
| 19 | +If your dialog box is not WPF, or for some other reason you cannot derive your dialog box class from <xref:Microsoft.VisualStudio.PlatformUI.DialogWindow>, then you must get the parent of the dialog box by calling <xref:Microsoft.VisualStudio.Shell.Interop.IVsUIShell.GetDialogOwnerHwnd%2A> and manage the modal state yourself, by calling the <xref:Microsoft.VisualStudio.Shell.Interop.IVsUIShell.EnableModeless%2A> method with a parameter of 0 (false) before displaying the dialog box and calling the method again with a parameter of 1 (true) after closing the dialog box. |
| 20 | + |
| 21 | +## Create a dialog box derived from DialogWindow |
| 22 | + |
| 23 | +1. Create a VSIX project named **OpenDialogTest** and add a menu command named **OpenDialog**. For more information about how to do this, see [Create an extension with a menu command](../extensibility/creating-an-extension-with-a-menu-command.md). |
| 24 | + |
| 25 | +2. To use the <xref:Microsoft.VisualStudio.PlatformUI.DialogWindow> class, you must add references to the following assemblies (in the Framework tab of the **Add Reference** dialog box): |
| 26 | + |
| 27 | + - *PresentationCore* |
| 28 | + |
| 29 | + - *PresentationFramework* |
| 30 | + |
| 31 | + - *WindowsBase* |
| 32 | + |
| 33 | + - *System.Xaml* |
| 34 | + |
| 35 | +3. In *OpenDialog.cs*, add the following `using` statement: |
| 36 | + |
| 37 | + ```csharp |
| 38 | + using Microsoft.VisualStudio.PlatformUI; |
| 39 | + ``` |
| 40 | + |
| 41 | +4. Declare a class named `TestDialogWindow` that derives from <xref:Microsoft.VisualStudio.PlatformUI.DialogWindow>: |
| 42 | + |
| 43 | + ```csharp |
| 44 | + class TestDialogWindow : DialogWindow |
| 45 | + {. . .} |
| 46 | + ``` |
| 47 | + |
| 48 | +5. To be able to minimize and maximize the dialog box, set <xref:Microsoft.VisualStudio.PlatformUI.DialogWindowBase.HasMaximizeButton%2A> and <xref:Microsoft.VisualStudio.PlatformUI.DialogWindowBase.HasMinimizeButton%2A> to true: |
| 49 | + |
| 50 | + ```csharp |
| 51 | + internal TestDialogWindow() |
| 52 | + { |
| 53 | + this.HasMaximizeButton = true; |
| 54 | + this.HasMinimizeButton = true; |
| 55 | + } |
| 56 | + ``` |
| 57 | + |
| 58 | +6. In the `OpenDialog.ShowMessageBox` method, replace the existing code with the following: |
| 59 | + |
| 60 | + ```csharp |
| 61 | + TestDialogWindow testDialog = new TestDialogWindow(); |
| 62 | + testDialog.ShowModal(); |
| 63 | + ``` |
| 64 | + |
| 65 | +7. Build and run the application. The experimental instance of Visual Studio should appear. On the **Tools** menu of the experimental instance you should see a command named **Invoke OpenDialog**. When you click this command, you should see the dialog window. You should be able to minimize and maximize the window. |
| 66 | + |
| 67 | +## Create and manage a dialog box not derived from DialogWindow |
| 68 | + |
| 69 | +1. For this procedure, you can use the **OpenDialogTest** solution you created in the previous procedure, with the same assembly references. |
| 70 | + |
| 71 | +2. Add the following `using` declarations: |
| 72 | + |
| 73 | + ```csharp |
| 74 | + using System.Windows; |
| 75 | + using Microsoft.Internal.VisualStudio.PlatformUI; |
| 76 | + ``` |
| 77 | + |
| 78 | +3. Create a class named `TestDialogWindow2` that derives from <xref:System.Windows.Window>: |
| 79 | + |
| 80 | + ```csharp |
| 81 | + class TestDialogWindow2 : Window |
| 82 | + {. . .} |
| 83 | + ``` |
| 84 | + |
| 85 | +4. Add a private reference to <xref:Microsoft.VisualStudio.Shell.Interop.IVsUIShell>: |
| 86 | + |
| 87 | + ``` |
| 88 | + private IVsUIShell shell; |
| 89 | + ``` |
| 90 | + |
| 91 | +5. Add a constructor that sets the reference to <xref:Microsoft.VisualStudio.Shell.Interop.IVsUIShell>: |
| 92 | + |
| 93 | + ```csharp |
| 94 | + public TestDialogWindow2(IVsUIShell uiShell) |
| 95 | + { |
| 96 | + shell = uiShell; |
| 97 | + } |
| 98 | + ``` |
| 99 | + |
| 100 | +6. In the `OpenDialog.ShowMessageBox` method, replace the existing code with the following: |
| 101 | + |
| 102 | + ```csharp |
| 103 | + IVsUIShell uiShell = (IVsUIShell)ServiceProvider.GetService(typeof(SVsUIShell)); |
| 104 | + |
| 105 | + TestDialogWindow2 testDialog2 = new TestDialogWindow2(uiShell); |
| 106 | + //get the owner of this dialog |
| 107 | + IntPtr hwnd; |
| 108 | + uiShell.GetDialogOwnerHwnd(out hwnd); |
| 109 | + testDialog2.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner; |
| 110 | + uiShell.EnableModeless(0); |
| 111 | + try |
| 112 | + { |
| 113 | + WindowHelper.ShowModal(testDialog2, hwnd); |
| 114 | + } |
| 115 | + finally |
| 116 | + { |
| 117 | + // This will take place after the window is closed. |
| 118 | + uiShell.EnableModeless(1); |
| 119 | + } |
| 120 | + ``` |
| 121 | + |
| 122 | +7. Build and run the application. On the **Tools** menu you should see a command named **Invoke OpenDialog**. When you click this command, you should see the dialog window. |
0 commit comments