The article shows complete samples on how to create ATL window and ATL dialog application.
Introduction
I don't intend to share all that I know. Just providing starting point. I find short samples very useful for starting and for better understanding of the online technical documentation when needed. Maybe I will extend the article with more short complete samples in the future.
Background
The reader should be familiar with Win API to understand the code and to be able to use ATL to write his own code. The code is written under VisualStudio, and I strongly suggest to use it. But any programming environment supporting C++ WinAPI is ok.
1. Writing windows application
Copy/paste the code and use
#include<atlbase.h>
#include<atlwin.h>
class MyMainWnd : public CWindowImpl<MyMainWnd>
{
public:
BEGIN_MSG_MAP(MyMainWnd)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
END_MSG_MAP()
LRESULT OnDestroy(UINT, WPARAM, LPARAM, BOOL&)
{
PostQuitMessage(0);
return 0;
}
};
int main()
{
MyMainWnd wnd;
wnd.Create(NULL, CWindow::rcDefault, L"My Main Window", WS_OVERLAPPEDWINDOW);
wnd.ShowWindow(SW_SHOW);
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
2. Writing dialog application
To create a dialog from resource, suppose resource id is IDD_DIALOG_COM
. The dialog is a simple one, containing the two buttons, Ok and Cancel.
#include "resource.h"
#include <iostream>
#include <atlbase.h>
#include <atlwin.h>
class CMyDialog : public CAxDialogImpl<CMyDialog>
{
public:
enum { IDD = IDD_DIALOG_COM};
BEGIN_MSG_MAP(CMyDialog)
COMMAND_ID_HANDLER(IDCANCEL, OnBnCancel)
COMMAND_ID_HANDLER(IDOK, OnBnOk)
END_MSG_MAP()
public:
LRESULT OnBnCancel(WORD, WORD, HWND, BOOL&)
{
EndDialog(IDCANCEL);
return 0;
}
LRESULT OnBnOk(WORD, WORD, HWND, BOOL&)
{
EndDialog(IDOK);
return 0;
}
};
using namespace std;
int main()
{
CMyDialog dlg;
auto result = dlg.DoModal();
if (result == IDOK) cout<< "agree"<< endl;
return 0;
}
Points of Interest
If you like to read short samples of code rather than bybles of knowledge base, here you are.
I'm C/C++ developer since 1999.
My favorite language is C++ and my favorite programming environment is Microsoft Visual Studio.