、添加资源,设置属性
#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;
}