SamuelRobinson.com
A resource for Windows programmers
It is reasonably easy to develop code that can be used in both an ATL and MFC environment. This can be particularly useful if you wish to have utility classes that are re-used in many projects. In this article we will explore the minimum necessary support required to use one of the ATL windowing classes, in this caseCDialogImpl<>, in a MFC class.
The first part of this is to enable the use of ATL windowing classes in your MFC project.
This is a fairly simple proposition.
First, you add the following lines to your stdafx.h file.
#include <atlbase.h> // basic ATL support extern CComModule _Module; // every source file that uses atlcom.h must have access to this #include <atlcom.h> // basic ATL COM Support #include <atlwin.h> // ATL windowing classes
Your will need to make some additions to the implementation file for your application. Just as you have a global pointer to the application in "pure" MFC, ATL requires a pointer to the application. This is a HINSTANCE variable _Module. I usually put it just below the application object
///////////////////////////////////////////////////////////////////////////// // The one and only CMyApp object CMyApp theApp; ///////////////////////////////////////////////////////////////////////////// // Adding a _Module for ATL support CComModule _Module;
This variable needs to be intialized, and we can do this in our CMyApp::InitInstance().
_Module.Init(NULL, AfxGetInstanceHandle());
Additional initialization will be necessary for some of the ATL functionality, but this will be enough to get the windowing classes working. For the sample, I've taken an appwizard application and used the defaults for everything except my view class. I changed the view class to a CFormView derived class so that we can explore using ATL controls on that form.
I'll start out by adding a menu item to show a CDialogImpl<> derived dialog. There are a couple of ways to do this implementation, but I'm going to right click on the workspace and just add the class.

When the class is added, there will be a warning regarding the header files for the parent class. You can safely ignore this warning as we have included the header required in the stdafx.h file. In this class I've a message map, an event handler, and the initialization handler. This particular dialog doesn't do much, but it is perfectly happy in either an ATL environment, or a MFC one.
class CATLDlg : public CDialogImpl<CATLDlg>
{
public:
CATLDlg(){};
virtual ~CATLDlg(){};
public:
// We must provide a dialog resource named IDD
enum { IDD = IDD_ATLDLG1 };
BEGIN_MSG_MAP(CATLDlg)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
END_MSG_MAP()
// Handler prototypes:
// LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
// LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
// LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);
LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
return 1; // Let the system set the focus
}
// This function is called when the cancel button is pressed. Depending
// on the way the dialog was instantiated it calls the proper code to
// close the dialog.
LRESULT OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// This code allows us to either call DoModal() or Create()
// for this sample.
if(this->m_bModal)
{
EndDialog(wID);
}
else
{
DestroyWindow();
}
return 0;
}
};
We will need to provide a dialog resource, and add a member of our class type to the application view. In the example, this class is invoked in response to a menu handler, but this is an implementation detail.
void
CAtlCtlsView::OnAtldlgShowdlg()
{ // Uncomment this line if you wish the dialog to be
modal //m_wndDlg.DoModal(this->m_hWnd);
// comment out these three lines if you wish the dialog to
// be modal
HWND hWnd = m_wndDlg.Create(this->m_hWnd);
_ASSERTE(::IsWindow(hWnd));
::ShowWindow(hWnd,SW_SHOW);
}
CATLDlg m_wndDlg;
The class can now be built and run. You will be able to invoke the dialog, either using the modal or nonmodal code. This example has shown how to simply and easily add an ATL object to a simple MFC app.
Please tell me what you think about this content and how I might improve it.