Create a Window using CWindowImpl Class

In this sample example, I am using CWindowImpl class to create a simple window.

Step 1: Start new Win32 Application project. 

Step 2: Include these lines in your stdafx.h file.      

#include 

extern CComModule _Module;

#include 

The atlbase.h class is basic ATL support. ATL Windowing classes are defined in atlwin.h. CComModule is a global class similar to CWinApp in MFC. The _comModule is a global variable represents the application.

Step 3: Now write this code into your WinMain function. You WinMain should look like this:      

int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE, LPSTR, int )

{

_Module.Init( NULL, hInstance );

// Create an instance of CMcbWindow

CMcbWindow atlWnd;

atlWnd.Create( NULL, CWindow::rcDefault, _T("Mahesh's ATL Window"),

WS_OVERLAPPEDWINDOW|WS_VISIBLE );

MSG msg;

while( GetMessage( &msg, NULL, 0, 0 ) )

{

TranslateMessage( &msg );

DispatchMessage( &msg );

}

_Module.Term();

return msg.wParam;

}

Step 4: Derive CMcbWindow class from CWindowImpl and write OnPaint and OnDestroy functions.

// NOTE: See the template parameter. Its CMcbWindow

class CMcbWindow : public CWindowImpl 

{

// START MESSAGE_MAP

BEGIN_MSG_MAP( CMcbWindow )

MESSAGE_HANDLER( WM_PAINT, OnPaint )

MESSAGE_HANDLER( WM_DESTROY, OnDestroy )

END_MSG_MAP()

// END MESSAGE_MAP

// This function will paint a Hello mindcracker string 

LRESULT OnPaint( UINT, WPARAM, LPARAM, BOOL& )

{

PAINTSTRUCT ps;

HDC hDC = GetDC();

BeginPaint( &ps );

TextOut( hDC, 0, 0, _T("Hello mindcracker"), 17 );

EndPaint( &ps );

return 0;

}

LRESULT OnDestroy( UINT, WPARAM, LPARAM, BOOL& ){

PostQuitMessage( 0 );

return 0;

}

};

Step 5: Define CComModule _Module extern variable. 

// ATLWnd.cpp : Defines the entry point for the application.

#include "stdafx.h"

CComModule _Module;

Step 5: Compile and Run your program.