This blog is about developing Windows applications using Visual Studio. All postings on this weblog are provided "AS IS" with no warranties, and confer no rights. Use of any samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm
Your host Nikola Dudar is a Program Manager in Windows division of Microsoft Corporation. He has been working on Windows Web Services API during Windows 7 and various additions to Visual C++ during VS2005 and VS2008. More details are in LinkedIn profile under Nikola's formal name Mykola Dudar.
If you are interested in program management and project management, check out my other blog at http://www.pmsnack.com/ where I collect best practices and other topics interesting to program and project managers.
To send feedback, comments or requests for new posts, please use the contact form.
One of the primary scenarios around integration of Windows Forms into an existing MFC application is around showing a Windows Form as a dialog, similar to CDialog class in MFC. Developer can create a MFC representation of an existing Windows Form control (System::Windows::Form::Control) created in either in C++ or C#. Then he can use this MFC control in existing MFC applications.
Let's explore this in an example. Assume you have a called HostedWinFormsControl and you want to integrate it with your existing MFC applications. Below are the steps to follow:
And that's it. Now you can use this MFC class that hosts Winform as a regular MFC dialog. Here are the code snippets:
--------------------------------------------------------------------------------------------------------
HostForWinForm.h
#pragma once
// CHostForWinForm dialog
class CHostForWinForm : public CWinFormsDialog<HostedWinForms::HostedWinFormsControl>
// public CDialog
{
DECLARE_DYNAMIC(CHostForWinForm)
public:
CHostForWinForm(CWnd* pParent = NULL); // standard constructor
virtual ~CHostForWinForm();
virtual BOOL OnInitDialog();
// Dialog Data
// enum { IDD = IDD_HOSTFORWINFORM };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
/////////////////////////////////////////////
// CODE YOU ADD TO INTEGRATE WINFORM CONTROL
virtual void OnButton1( System::Object^ sender, System::EventArgs^ e );
virtual void OnButton2( System::Object^ sender, System::EventArgs^ e );
BEGIN_DELEGATE_MAP( CHostForWinForm )
EVENT_DELEGATE_ENTRY( OnButton1, System::Object^, System::EventArgs^ );
EVENT_DELEGATE_ENTRY( OnButton2, System::Object^, System::EventArgs^ );
END_DELEGATE_MAP()
CString m_sEditBoxOnWinForm;
};
HostForWinForm.cpp
#include "stdafx.h"
#include "HostForWinForm.h"
IMPLEMENT_DYNAMIC(CHostForWinForm, CWinFormsDialog<HostedWinFormsControl>)
CHostForWinForm::CHostForWinForm(CWnd* pParent /*=NULL*/)
: CWinFormsDialog<HostedWinFormsControl>(CHostForWinForm::IDD, pParent)
}
CHostForWinForm::~CHostForWinForm()
void CHostForWinForm::DoDataExchange(CDataExchange* pDX)
CDialog::DoDataExchange(pDX);
if (pDX->m_bSaveAndValidate)
m_sEditBoxOnWinForm = CString( m_control->textBox1->Text);
else {
m_pControl->textBox1->Text = gcnew System::String(m_sEditBoxOnWinForm);
BEGIN_MESSAGE_MAP(CHostForWinForm, CWinFormsDialog<HostedWinFormsControl>)
END_MESSAGE_MAP()
BOOL CHostForWinForm::OnInitDialog()
CWinFormsDialog<HostedWinFormsControl>::OnInitDialog();
GetControl()->OkButton->Click += MAKE_DELEGATE( System::EventHandler, OnButton1);
GetControl()->CancelButton->Click += MAKE_DELEGATE( System::EventHandler, OnButton2);
return TRUE;
void CHostForWinForm::OnButton1( System::Object^ , System::EventArgs^ )
CDialog::OnOK(); // This will close the dialog and DoModal will return.
void CHostForWinForm::OnButton2( System::Object^ , System::EventArgs^ )
CDialog::OnCancel(); // This will close the dialog and DoModal will return.
Note this code snippet was fully tested in the final release of VS2005. I have not tested it since because it was not the area of my main focus. Lately I was posting on web services, guidance to developers around developing windows applications and best practices in program management and project management. You may check out other posts on MFC here. Here are some that you may find interesting:
If you have questions and need more help in integrating Windows Forms in your MFC applications, please ask questions on Visual C++ MFC forum.