[ATL]对话框

这个博客展示了如何使用C++和ATL库创建一个包含窗口和对话框的应用程序。程序包括了窗口的创建、菜单加载、消息处理以及对话框的显示。通过消息映射,处理了WM_INITDIALOG、WM_CLOSE、WM_DESTROY等关键消息,并实现了窗口的居中显示和关闭操作。
摘要由CSDN通过智能技术生成

、添加资源,设置属性

#include <iostream>
#include<atlbase.h>
#include<atlwin.h>
#include"resource.h"
using namespace std;

CComModule gModule;
//样式,风格
//WS_OVERLAPPEDWINDOW:各种风格的窗体		WS_CLIPCHILDREN:可点击   APP(应用程序)
typedef CWinTraits<WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CAPTION, WS_EX_APPWINDOW> MyTraits;
//对话框
class CAboutDlg :public CDialogImpl<CAboutDlg, CWindow>
{
public:
	enum { IDD = IDD_ABOUT };

	BEGIN_MSG_MAP(CAboutDlg)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)	//初始化窗口
		MESSAGE_HANDLER(WM_CLOSE, OnClose)				//关闭窗口
		COMMAND_ID_HANDLER(IDOK, OnOK)					//点击确认 按钮
		COMMAND_ID_HANDLER(IDCANCEL, OnOK)
	END_MSG_MAP()

	LRESULT OnInitDialog(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		CenterWindow();	//居中显示
		return TRUE;
	}
	LRESULT OnClose(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		EndDialog(IDOK);
		return 0;
	}
	LRESULT OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtrl, BOOL& bHandled)
	{
		EndDialog(wID);
		return 0;
	}

};

//窗口
class MyWindow :public CWindowImpl<MyWindow, CWindow, MyTraits>
{
public:
	DECLARE_WND_CLASS(_T("MyWindow!"));	//注册窗口

	//消息映射表 消息处理
	BEGIN_MSG_MAP(MyWindow)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)		//创建
		MESSAGE_HANDLER(WM_CLOSE, OnClose)		 //关闭窗口
		MESSAGE_HANDLER(WM_DESTROY, OnDestroy)	//销毁窗口
		COMMAND_ID_HANDLER(ID_HELLO_WORLD, OnAbout)	//菜单处理
		COMMAND_ID_HANDLER(ID_HELLO_CLOSE, OnHelloClose)
	END_MSG_MAP()

	//消息对应函数
	//bHandled TRUE:则不会调用WindowProc消息  FASLSE:会调用WindowProc(继续处理)
	LRESULT OnCreate(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		CenterWindow();	//居中显示
		HMENU hMenu = LoadMenu(gModule.GetResourceInstance(), MAKEINTRESOURCE(IDR_ABOUT));//加载Menu
		SetMenu(hMenu);
		return 0;
	}
	LRESULT OnClose(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		DestroyWindow();
		return 0;
	}
	LRESULT OnDestroy(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
	{
		PostQuitMessage(0);
		return 0;
	}
	LRESULT OnAbout(UINT wNotifyCode, WORD wID, HWND hWndCtrl, BOOL& bHandled)
	{
		OutputDebugString(_T("OnAbout called!\r\n"));
		CAboutDlg dlg;
		dlg.DoModal();
		return 0;
	}
	LRESULT OnHelloClose(UINT wNotifyCode, WORD wID, HWND hWndCtrl, BOOL& bHandled)
	{
		DestroyWindow();
		return 0;
	}
};


int main()
{
	//初始化.com
	HRESULT hRes = gModule.Init(NULL, GetModuleHandle(NULL));
	if (hRes != 0)
	{
		cout << hRes << endl;
	}

	MyWindow win;
	//创建窗口
	HWND hWnd = win.Create(NULL, CWindow::rcDefault, _T("MyATLWind!!!"));
	if (hWnd == NULL)
	{
		cout << "create window failed!:" << hWnd << endl;
	}
	win.ShowWindow(SW_SHOW); //显示窗口
	win.UpdateWindow();		//刷新窗口
	//建立消息循环
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);



	}
	gModule.Term();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值