Skip to content

Commit 35e3cff

Browse files
authored
Merge pull request #5383 from Rageking8/update-hosting-a-windows-form-user-control-as-an-mfc-dialog-box
Update "Hosting a Windows Form User Control as an MFC Dialog Box"
2 parents eee99b3 + 14cfda2 commit 35e3cff

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

docs/dotnet/hosting-a-windows-form-user-control-as-an-mfc-dialog-box.md

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
---
2-
description: "Learn more about: Hosting a Windows Form User Control as an MFC Dialog Box"
32
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
55
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
76
---
87
# Hosting a Windows Form User Control as an MFC Dialog Box
98

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.
1110

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.
1312

1413
### To create the MFC host application
1514

1615
1. Create an MFC Application project.
1716

1817
On the **File** menu, select **New**, and then click **Project**. In the **Visual C++** folder, select **MFC Application**.
1918

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**.
2120

2221
In the **MFC Application Wizard**, accept all the defaults, and then click **Finish**. This creates an MFC application with a Multiple Document Interface.
2322

@@ -29,21 +28,21 @@ The process that `CWinFormsDialog` uses to host the user control is the similar
2928

3029
1. Add a reference to the .NET control.
3130

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.
3332

3433
1. Add `#include <afxwinforms.h>` to *pch.h* (*stdafx.h* in Visual Studio 2017 and earlier), at the end of the existing `#include` statements.
3534

3635
1. Add a new class that subclasses `CDialog`.
3736

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.).
3938

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>`.
4140

42-
1. Call DoModal on the CHostForWinForm class.
41+
1. Call `DoModal` on the `CHostForWinForm` class.
4342

44-
In MFC03.cpp, add `#include "HostForWinForm.h"`.
43+
In `MFC03.cpp`, add `#include "HostForWinForm.h"`.
4544

46-
Before the return statement in the definition of CMFC03App::InitInstance, add:
45+
Before the return statement in the definition of `CMFC03App::InitInstance`, add:
4746

4847
```cpp
4948
CHostForWinForm m_HostForWinForm;
@@ -58,15 +57,15 @@ The process that `CWinFormsDialog` uses to host the user control is the similar
5857

5958
Next you will add code to monitor the state of a control on the Windows Forms from the MFC application.
6059

61-
1. Add a handler for OnInitDialog.
60+
1. Add a handler for `OnInitDialog`.
6261

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`:
6463

6564
```cpp
6665
virtual BOOL OnInitDialog();
6766
```
6867

69-
1. Define OnInitDialog (in CHostForWinForm.cpp) as follows:
68+
1. Define `OnInitDialog` (in `CHostForWinForm.cpp`) as follows:
7069

7170
```cpp
7271
BOOL CHostForWinForm::OnInitDialog() {
@@ -76,7 +75,7 @@ The process that `CWinFormsDialog` uses to host the user control is the similar
7675
}
7776
```
7877

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`:
8079

8180
```cpp
8281
virtual void OnButton1( System::Object^ sender, System::EventArgs^ e );
@@ -86,7 +85,7 @@ The process that `CWinFormsDialog` uses to host the user control is the similar
8685
END_DELEGATE_MAP()
8786
```
8887

89-
In CHostForWinForm.cpp, add this definition:
88+
In `CHostForWinForm.cpp`, add this definition:
9089

9190
```cpp
9291
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
9998

10099
Next you will add code to display from the MFC code the value in the text box on the Windows Form.
101100

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:
103102

104103
```cpp
105104
CString m_sEditBoxOnWinForm;
106105
```
107106

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:
109108

110109
```cpp
111110
if (pDX->m_bSaveAndValidate)
@@ -114,7 +113,7 @@ The process that `CWinFormsDialog` uses to host the user control is the similar
114113
GetControl()->textBox1->Text = gcnew System::String(m_sEditBoxOnWinForm);
115114
```
116115

117-
1. In the definition of OnButton1 in CHostForWinForm.cpp, add the following three lines to the end of the function:
116+
1. In the definition of `OnButton1` in `CHostForWinForm.cpp`, add the following three lines to the end of the function:
118117

119118
```cpp
120119
this->UpdateData(TRUE);
@@ -126,5 +125,5 @@ The process that `CWinFormsDialog` uses to host the user control is the similar
126125

127126
## See also
128127

129-
<xref:System.Windows.Forms.UserControl?displayProperty=fullName>
130-
[Using a Windows Form User Control in MFC](../dotnet/using-a-windows-form-user-control-in-mfc.md)
128+
<xref:System.Windows.Forms.UserControl?displayProperty=fullName>\
129+
[Using a Windows Form User Control in MFC](using-a-windows-form-user-control-in-mfc.md)

0 commit comments

Comments
 (0)