For VisualStudio 2005 or above versions, refer to below link
http://msdn.microsoft.com/en-us/library/ahdd1h97.aspx
http://msdn.microsoft.com/en-us/library/b1kyh79x(v=VS.80).aspx
The basic idea of this implementation is to use the class CWinFormsControl<NameSpce::WinformClass/UserControlClass> to host .Net Winform or UserControl.
This article presents how to implement this in VS2002 environment.
- Add a UserControl and Winform to the project
- Compile and run the C# project.
- Insert a new Visual C++ project, select “MFC Application” as the project type
- Create a dialog based application in the MFC wizard.
Add a static control and two buttons in the dialog.
Set the MFC application as the startup project, and set the dependency to make sure MFC application depends on the CSharp library.
Compile and run the application, and the result should be as below.
- In the MFC project setting, change the “Use Managed Extensions” from “No” to Yes.
- type “#using <mscorlib.dll>” in the dialog.cpp file, compile and run. You will find such errors:
This is due to the conflicting compiling directives. Perform below actions:
Go to the Code Generation Tab, change “Basic Runtime Checks” from “Both (/RTC1, equiv. to /RTCsu)” to “Default”
Go to the Code Generation Tab, change “Enable Minimal rebuild” from “Yes” to “No”
Go to the General Tab,Change “Debug Information Format” from “/ZI” to “/Zi”
- Rebuild and run, this should be fine now.
- Add other necessary directives and references as below in the dialog.cpp file
- An compiling error pops up:
This is due to failure in finding the requested dll, in this case, our CSharp library project’s output.
- To troubleshoot this, in the CSharp project setting, set the output directory to the MFC output directory. Note that the debug and release version should be separately configured.
- Rebuild and run, this should work fine now.
- In the MFC dialog.cpp file, create the two event handlers for these two buttons.
void CMFCMainDlg::OnBnClickedTestSharpCtrl() { SharpForm* sc=new SharpForm(); sc->Visible=true; }
Now the problem is not that the class SharpForm is not recognized inside MFC, but due to the “new” operator in MFC, specifically in MFC debug mode. The workaround is to temporarily override the “new” definition
See also http://www.codeguru.com/forum/archive/index.php/t-184491.html for details.
- Rebuild and run, wow, it works!
- There might be another minor problem: the SharpForm is not a child window of the dialog! What is expected is to embed the dialog into the static control! And to troubleshoot this, add below code:
Now, the CSharp form can no longer be moved outside the MFC dialog window!
- To fine tune the location of the SharpForm, call Windows API ::MoveWindow:
- To get rid of the title bar of the SharpForm, add the below code in CSharpLibrary project:
Now, the title is removed!
- Now, when you drag the dialog to other locations or simply click other client area of the dialog, you will see that the location of the SharpForm remains (or even disappeared), but the parent dialog has already changed. This will result in overlapped windows, which is to be avoided.
- To make the embedded SharpForm to change accordingly with the move/size changes, define a function:
This function is then called in the CXXXDlg::OnMove() and CXXXDlg::OnSize()to update the child CSharp form’s location.
Note that it is impossible to keep a “SharpForm*” object, as it is not feasible to keep a managed object pointer (reference) in a unmanaged class. However, keeping a handle, (i.e. HWND hSharpWnd) is plausible.
This concludes the tutorial.
Filed under: MFC
