You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/dotnet/hosting-a-windows-form-user-control-as-an-mfc-dialog-box.md
+21-22Lines changed: 21 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,23 +1,22 @@
1
1
---
2
-
description: "Learn more about: Hosting a Windows Form User Control as an MFC Dialog Box"
3
2
title: "Hosting a Windows Form User Control as an MFC Dialog Box"
4
-
ms.date: "11/04/2016"
3
+
description: "Learn more about: Hosting a Windows Form User Control as an MFC Dialog Box"
4
+
ms.date: 11/04/2016
5
5
helpviewer_keywords: ["MFC [C++], Windows Forms support", "Windows Forms [C++], hosting as MFC Dialog", "hosting Windows Forms control [C++]"]
6
-
ms.assetid: 0434a9d7-8b14-48e6-ad69-9ba9a684677a
7
6
---
8
7
# Hosting a Windows Form User Control as an MFC Dialog Box
9
8
10
-
MFC provides the template class [CWinFormsDialog](../mfc/reference/cwinformsdialog-class.md) so that you can host a Windows Forms user control (<xref:System.Windows.Forms.UserControl>) in a modal or modeless MFC dialog box. `CWinFormsDialog` is derived from the MFC class [CDialog](../mfc/reference/cdialog-class.md), so the dialog box can be launched as modal or modeless.
9
+
MFC provides the template class [`CWinFormsDialog`](../mfc/reference/cwinformsdialog-class.md) so that you can host a Windows Forms user control (<xref:System.Windows.Forms.UserControl>) in a modal or modeless MFC dialog box. `CWinFormsDialog` is derived from the MFC class [`CDialog`](../mfc/reference/cdialog-class.md), so the dialog box can be launched as modal or modeless.
11
10
12
-
The process that `CWinFormsDialog` uses to host the user control is the similar to that described in [Hosting a Windows Form User Control in an MFC Dialog Box](../dotnet/hosting-a-windows-form-user-control-in-an-mfc-dialog-box.md). However, `CWinFormsDialog` manages the initialization and hosting of the user control so that it does not have to be programmed manually.
11
+
The process that `CWinFormsDialog` uses to host the user control is similar to that described in [Hosting a Windows Form User Control in an MFC Dialog Box](hosting-a-windows-form-user-control-in-an-mfc-dialog-box.md). However, `CWinFormsDialog` manages the initialization and hosting of the user control so that it does not have to be programmed manually.
13
12
14
13
### To create the MFC host application
15
14
16
15
1. Create an MFC Application project.
17
16
18
17
On the **File** menu, select **New**, and then click **Project**. In the **Visual C++** folder, select **MFC Application**.
19
18
20
-
In the **Name** box, enter `MFC03` and change the Solution setting to **Add to Solution**.Click **OK**.
19
+
In the **Name** box, enter `MFC03` and change the Solution setting to **Add to Solution**.Click **OK**.
21
20
22
21
In the **MFC Application Wizard**, accept all the defaults, and then click **Finish**. This creates an MFC application with a Multiple Document Interface.
23
22
@@ -29,21 +28,21 @@ The process that `CWinFormsDialog` uses to host the user control is the similar
29
28
30
29
1. Add a reference to the .NET control.
31
30
32
-
In **Solution Explorer**, right-click the **MFC03** project node and choose **Add**, **References**. In the **Property Page**, click **Add New Reference**, select WindowsControlLibrary1 (under the **Projects** tab), and click **OK**. This adds a reference in the form of a [/FU](../build/reference/fu-name-forced-hash-using-file.md) compiler option so that the program will compile; it also copies WindowsControlLibrary1.dll into the `MFC03` project directory so that the program will run.
31
+
In **Solution Explorer**, right-click the **MFC03** project node and choose **Add**, **References**. In the **Property Page**, click **Add New Reference**, select WindowsControlLibrary1 (under the **Projects** tab), and click **OK**. This adds a reference in the form of a [`/FU`](../build/reference/fu-name-forced-hash-using-file.md) compiler option so that the program will compile; it also copies `WindowsControlLibrary1.dll` into the `MFC03` project directory so that the program will run.
33
32
34
33
1. Add `#include <afxwinforms.h>` to *pch.h* (*stdafx.h* in Visual Studio 2017 and earlier), at the end of the existing `#include` statements.
35
34
36
35
1. Add a new class that subclasses `CDialog`.
37
36
38
-
Right click on project name and add an MFC class (called CHostForWinForm) that subclasses `CDialog`. Since you do not need the dialog box resource, you can delete the resource ID (select **Resource View**, expand the **Dialog** folder and delete `IDD_HOSTFORWINFORM` resource. Then, remove any references to the ID in code.).
37
+
Right click on project name and add an MFC class (called `CHostForWinForm`) that subclasses `CDialog`. Since you do not need the dialog box resource, you can delete the resource ID (select **Resource View**, expand the **Dialog** folder and delete `IDD_HOSTFORWINFORM` resource. Then, remove any references to the ID in code.).
39
38
40
-
1. Replace `CDialog` in CHostForWinForm.h and CHostForWinForm.cpp files with `CWinFormsDialog<WindowsControlLibrary1::UserControl1>`.
39
+
1. Replace `CDialog` in `CHostForWinForm.h` and `CHostForWinForm.cpp` files with `CWinFormsDialog<WindowsControlLibrary1::UserControl1>`.
41
40
42
-
1. Call DoModal on the CHostForWinForm class.
41
+
1. Call `DoModal` on the `CHostForWinForm` class.
43
42
44
-
In MFC03.cpp, add `#include "HostForWinForm.h"`.
43
+
In `MFC03.cpp`, add `#include "HostForWinForm.h"`.
45
44
46
-
Before the return statement in the definition of CMFC03App::InitInstance, add:
45
+
Before the return statement in the definition of `CMFC03App::InitInstance`, add:
47
46
48
47
```cpp
49
48
CHostForWinForm m_HostForWinForm;
@@ -58,15 +57,15 @@ The process that `CWinFormsDialog` uses to host the user control is the similar
58
57
59
58
Next you will add code to monitor the state of a control on the Windows Forms from the MFC application.
60
59
61
-
1. Add a handler for OnInitDialog.
60
+
1. Add a handler for`OnInitDialog`.
62
61
63
-
Display the **Properties** window (F4). In **Class View**, select CHostForWinForm. In the **Properties** window, select overrides and in the row for OnInitDialog, click in the left hand column and select \< Add >. This adds the following line to CHostForWinForm.h:
62
+
Display the **Properties** window (F4). In **Class View**, select `CHostForWinForm`. In the **Properties** window, select overrides and in the row for `OnInitDialog`, click in the left hand column and select \< Add >. This adds the following line to `CHostForWinForm.h`:
64
63
65
64
```cpp
66
65
virtual BOOL OnInitDialog();
67
66
```
68
67
69
-
1. Define OnInitDialog (in CHostForWinForm.cpp) as follows:
68
+
1. Define `OnInitDialog` (in `CHostForWinForm.cpp`) as follows:
70
69
71
70
```cpp
72
71
BOOL CHostForWinForm::OnInitDialog() {
@@ -76,7 +75,7 @@ The process that `CWinFormsDialog` uses to host the user control is the similar
76
75
}
77
76
```
78
77
79
-
1. Next add the OnButton1 handler. Add the following lines to the public section of the CHostForWinForm class in CHostForWinForm.h:
78
+
1. Next add the `OnButton1` handler. Add the following lines to the public section of the `CHostForWinForm` class in `CHostForWinForm.h`:
80
79
81
80
```cpp
82
81
virtualvoidOnButton1( System::Object^ sender, System::EventArgs^ e );
@@ -86,7 +85,7 @@ The process that `CWinFormsDialog` uses to host the user control is the similar
86
85
END_DELEGATE_MAP()
87
86
```
88
87
89
-
In CHostForWinForm.cpp, add this definition:
88
+
In `CHostForWinForm.cpp`, add this definition:
90
89
91
90
```cpp
92
91
void CHostForWinForm::OnButton1( System::Object^ sender, System::EventArgs^ e )
@@ -99,13 +98,13 @@ The process that `CWinFormsDialog` uses to host the user control is the similar
99
98
100
99
Next you will add code to display from the MFC code the value in the text box on the Windows Form.
101
100
102
-
1. In the public section of the CHostForWinForm class in CHostForWinForm.h, add the following declaration:
101
+
1. In the public section of the `CHostForWinForm` class in `CHostForWinForm.h`, add the following declaration:
103
102
104
103
```cpp
105
104
CString m_sEditBoxOnWinForm;
106
105
```
107
106
108
-
1. In the definition of DoDataExchange in CHostForWinForm.cpp, add the following three lines to the end of the function:
107
+
1. In the definition of `DoDataExchange` in `CHostForWinForm.cpp`, add the following four lines to the end of the function:
109
108
110
109
```cpp
111
110
if (pDX->m_bSaveAndValidate)
@@ -114,7 +113,7 @@ The process that `CWinFormsDialog` uses to host the user control is the similar
0 commit comments