minimal ATL window

#include <atlbase.h>
#include <atlwin.h>
class MainWindow : public CWindowImpl<MainWindow, CWindow, CFrameWinTraits> {
public:
BEGIN_MSG_MAP(MainWindow)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
END_MSG_MAP()
LRESULT OnPaint(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
PAINTSTRUCT ps;
HDC hDC = GetDC();
BeginPaint( &ps );
SYSTEMTIME st; GetLocalTime(&st);
TCHAR text[MAX_PATH];
wsprintf(text, _T(“Onega’s minimal ATL window paints at %d:%02d:%02d”),
st.wHour, st.wMinute, st.wSecond);
TextOut( hDC, 0, 0, text, _tcslen(text) );
EndPaint( &ps );ReleaseDC(hDC);
return 0;
}
LRESULT OnDestroy(UINT msg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
PostQuitMessage(0);
return 0;
}
};

class ExeModule : public CAtlExeModuleT<ExeModule> {
public:
HRESULT PreMessageLoop(int nCmdShow) throw() {
CAtlExeModuleT<ExeModule>::PreMessageLoop(nCmdShow);
if(!window.Create(0, CWindow::rcDefault, _T(“Onega’s ATL Application!”)))
return E_FAIL;
window.ShowWindow(SW_SHOW);
window.UpdateWindow();
return S_OK;
}
void RunMessageLoop() throw() {
MSG msg;
while(true) {
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
private:
MainWindow window;
};

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR CmdLine, int nCmdShow) {
ExeModule module;
module.Run();
return 0;
}

//build by VC++ 2010 (cl miniatl.cpp), file size: 60.5KB.

Advertisements
This entry was posted in CPP, GUI. Bookmark the permalink.

One Response to minimal ATL window

  1. Drew Canitz says:

    You can make it a little shorter.

    Instead of module.Run(); return 0; you can simple return module.WinMain(nCmdShow);

    Futher, you don’t need to implement RunMessageLoop. The code works fine if you completely remove the method.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s