Click here to Skip to main content
15,920,603 members
Articles / Desktop Programming / WTL
Article

ATL window program, minimal

7 Jun 2022CPOL 1.3K   2
Here are minimal and pieces of ATL for WinAPI programming.
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

C++
#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.

C++
#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.

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Thomson Reuters
Moldova (Republic of) Moldova (Republic of)
I'm C/C++ developer since 1999.
My favorite language is C++ and my favorite programming environment is Microsoft Visual Studio.

Comments and Discussions

 
QuestionHow many times do we have to reject this? Pin
OriginalGriff6-Jun-22 10:27
mveOriginalGriff6-Jun-22 10:27 
AnswerRe: How many times do we have to reject this? Pin
Nelek6-Jun-22 10:35
protectorNelek6-Jun-22 10:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.