Share via

How drive class from ATL::CDialogImpl

drjackool 956 Reputation points
2023-07-31T18:17:41.1366667+00:00

Hi platform Win32/ATL How correctly drive a class from CDialogImpl like below (two or more vertically drived class)? because atl uses an enum IDD on create dialog

to solve this issue instead enum {IDD}; I use int IDD; and set it to desired dialog template before calling Create() thanks. sorry for single line type msdn edit box not come to new line on android mobile device!

class basedlg : public Cdialogimpl<basedlg>class a : public basedlg // base on diffent dialog templateclass b : public basedlg // base on diffent dialog template

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


Answer accepted by question author

RLWA32 52,571 Reputation points
2023-08-01T09:58:51.22+00:00

Take a look at this -

All windowing done with ATL window classes. Classes for modal and modeless dialogs in MyDlg.h header -

#pragma once

#include "resource.h"       // main symbols

using namespace ATL;

template<class T, int DLG>
class CBaseDlg :
	public CDialogImpl<CBaseDlg<T, DLG>>
{
public:
	CBaseDlg()
	{
	}

	~CBaseDlg()
	{
	}

	enum { IDD = DLG };

	BEGIN_MSG_MAP(CBaseDlg)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		COMMAND_HANDLER(IDOK, BN_CLICKED, OnClickedOK)
		COMMAND_HANDLER(IDCANCEL, BN_CLICKED, OnClickedCancel)
	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)
	{
		CString strTitle;
		strTitle.Format(_T("Dialog Caption set by %s"), _T(__FUNCTION__));
		SetWindowText(strTitle);

		return 1;  // Let the system set the focus
	}

	LRESULT OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		EndDialog(wID);
		return 0;
	}

	LRESULT OnClickedCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		EndDialog(wID);
		return 0;
	}
};

class ModalDlg : public CBaseDlg<ModalDlg, IDD_DIALOG1>
{
public:
	BEGIN_MSG_MAP(ModalDlg)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		CHAIN_MSG_MAP(CBaseDlg) // Chain to base for OK/Cancel handlers
	END_MSG_MAP()

	LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		bHandled = TRUE; // Set to FALSE to see the effect of message map chaining
		CString strTitle;
		strTitle.Format(_T("Dialog Caption set by %s"), _T(__FUNCTION__));
		SetWindowText(strTitle);

		return 1;  // Let the system set the focus
	}
};

class ModelessDlg : public CBaseDlg<ModelessDlg, IDD_DIALOG2>
{
public:
	BEGIN_MSG_MAP(ModelessDlg)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		COMMAND_HANDLER(IDOK, BN_CLICKED, OnOK)
		COMMAND_HANDLER(IDCANCEL, BN_CLICKED, OnCancel)
	END_MSG_MAP()

	LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		CString strTitle;
		strTitle.Format(_T("Dialog Caption set by %s"), _T(__FUNCTION__));
		SetWindowText(strTitle);

		return 1;  // Let the system set the focus
	}

	LRESULT OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		DestroyWindow();
		return 0;
	}

	LRESULT OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
	{
		DestroyWindow();
		return 0;
	}

	void OnFinalMessage(HWND hwnd)
	{
		delete this;
	}

};

Functions to create dialogs from main window -

	LRESULT OnModal(WORD, WORD, HWND, BOOL&)
	{
		ModalDlg dlg;
		dlg.DoModal(m_hWnd);
		return 0;
	}

	LRESULT OnModeless(WORD, WORD, HWND, BOOL&)
	{
		ModelessDlg* pModeless = new ModelessDlg;
		pModeless->Create(m_hWnd);
		pModeless->ShowWindow(SW_SHOW);
		return 0;
	}

In action

enter image description here

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.