C++杂记 05/08/2011 开始一个完整的WTL例子

本文详细记录了使用C++和Windows Template Library (WTL) 创建一个简单Win32项目的步骤,从创建empty win32 project开始,到遇到并解决编译错误,最终成功运行一个显示窗口但未填充内容的应用。过程中提到了类定义问题、消息映射和视图的添加,展示了如何在WTL中处理消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

1. 生成empty win32 project

2. 加入My1stWTL70Win.cpp

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{...}

int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT)
{...}

3. 添加MyMainWnd Class

 

class MyMainWnd : public CFrameWindowImpl<MyMainWnd>
{
public:
    MyMainWnd();
    virtual ~MyMainWnd();

};

 

Error:

MyMainWnd.cpp
d:/study/wtl/hellowtl70/my1stwtl70win/mymainwnd.h(12) : error C2504: 'CFrameWindowImpl' : base class undefined
d:/study/wtl/hellowtl70/my1stwtl70win/mymainwnd.h(12) : error C2143: syntax error : missing ',' before '<'
d:/study/wtl/hellowtl70/my1stwtl70win/mymainwnd.h(12) : error C2059: syntax error : '<'
Error executing cl.exe.

 

4. 去掉MyMainWnd.cpp文件,OK ?

最后的结果,一个空的windows。相关代码如下:

 


#include "stdafx.h"

#include <atlframe.h>
#include <atlctrls.h>
#include <atldlgs.h>
#include <atlctrlw.h>

#include "MyMainWnd.h"

CAppModule _Module;

int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT)
{
    CMessageLoop theLoop;
    _Module.AddMessageLoop(&theLoop);


    //CMainFrame wndMain;
    MyMainWnd wndMain;
   
    if(wndMain.CreateEx() == NULL)
    {
        ATLTRACE(_T("Main window creation failed!/n"));
        return 0;
    }
   
    wndMain.ShowWindow(nCmdShow);
   
    int nRet = theLoop.Run();
   
    _Module.RemoveMessageLoop();

    return nRet;

}

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow)
{
    HRESULT hRes = ::CoInitialize(NULL);
    // If you are running on NT 4.0 or higher you can use the following call instead to
    // make the EXE free threaded. This means that calls come in on a random RPC thread.
    //    HRESULT hRes = ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
    ATLASSERT(SUCCEEDED(hRes));
   
    // this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
    ::DefWindowProc(NULL, 0, 0, 0L);
   
    AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES);    // add flags to support other controls
   
    hRes = _Module.Init(NULL, hInstance);
    ATLASSERT(SUCCEEDED(hRes));
   
    int nRet = Run(lpstrCmdLine, nCmdShow);
   
    _Module.Term();
    ::CoUninitialize();
   
    return nRet;
}

 

注:此程序在关闭窗口后,进程并没有消失,奇怪!!!

 

5. 增加一个view: MyView

 

Error:

 

Compiling...
My1stWTL70Win.cpp
d:/study/wtl/hellowtl70/my1stwtl70win/mymainwnd.h(19) : error C2146: syntax error : missing ';' before identifier 'myView'
d:/study/wtl/hellowtl70/my1stwtl70win/mymainwnd.h(19) : error C2501: 'MyView' : missing storage-class or type specifiers
d:/study/wtl/hellowtl70/my1stwtl70win/mymainwnd.h(19) : error C2501: 'myView' : missing storage-class or type specifiers
Error executing cl.exe.

 

A: 在MyMainWnd.h中加入头文件:

#include "MyView.h"

 

Compile OK !    ( WHY wizard-generated files have no such issue ?!  头文件被引入的顺序有关系?)

 

 

**** 目前状态 ****

窗口显示出来了,但view里面没有东东。

 

这里有一篇文章讲WTL消息处理的:http://blog.youkuaiyun.com/fengrx/archive/2009/05/09/4163169.aspx

 

6. 让View显示东东,终于搞定!

 

主要修改:在Message map中增加 CHAIN_MSG_MAP(CFrameWindowImpl<MyMainWnd>)

 

MyMainWnd.h的完整代码如下:

 

#include "MyView.h"
class MyMainWnd : public CFrameWindowImpl<MyMainWnd>//, public CUpdateUI<MyMainWnd>
{
public:
    DECLARE_FRAME_WND_CLASS(NULL, NULL)
    MyView myView;
    MyMainWnd(){}
    virtual ~MyMainWnd(){}
    BEGIN_MSG_MAP(MyMainWnd)
        MESSAGE_HANDLER(WM_CREATE, OnCreate)
        //CHAIN_MSG_MAP(CUpdateUI<MyMainWnd>)
        CHAIN_MSG_MAP(CFrameWindowImpl<MyMainWnd>)
    END_MSG_MAP()

public:
    LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
    {
        m_hWndClient = myView.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, WS_EX_CLIENTEDGE);   
        CMessageLoop* pLoop = _Module.GetMessageLoop();
        ATLASSERT(pLoop != NULL);
        return 0;
    }
};

 

 

 

 

 

目录 WTL for MFC Programmers, Chinese Version Prologue WTL for MFC Programmers, Chinese Version PrologueWTL for MFC Programmers, Chinese Version Prologue WTL for MFC Programmers, Chinese Version Prologue WTL for MFC Programmers, Chinese Version Prologue WTL for MFC Programmers, Chinese Version PrologueWTL for MFC Programmers, Chinese Version PrologueWTL for MFC Programmers, Chinese Version Prologue WTL for MFC Programmers, Chinese Version PrologueWTL for MFC Programmers, Chinese Version Prologue WTL for MFC Programmers, Chinese Version Prologue WTL for MFC Programmers, Chinese Version PrologueWTL for MFC Programmers, Chinese Version PrologueWTL for MFC Programmers, Chinese Version PrologueWTL for MFC Programmers, Chinese Version Prologue WTL for MFC Programmers, Chinese Version PrologueWTL for MFC Programmers, Chinese Version PrologueWTL for MFC Programmers, Chinese Version PrologueWTL for MFC Programmers, Chinese Version Prologue WTL for MFC Programmers, Chinese Version PrologueWTL for MFC Programmers, Chinese Version PrologueWTL for MFC Programmers, Chinese Version PrologueWTL for MFC Programmers, Chinese Version PrologueWTL for MFC Programmers, Chinese Version Prologue WTL for MFC Programmers, Chinese Version Prologue WTL for MFC Programmers, Chinese Version Prologue WTL for MFC Programmers, Chinese Version PrologueWTL for MFC Programmers, Chinese Version Prologue WTL for MFC Programmers, Chinese Version Prologue WTL for MFC Programmers, Chinese Version Prologue ................................ ........................... 6 WTL for MFC Programmers, Part I - ATL GUI Classes ................................ ........................... 8 README.TXTREADME.TXT README.TXTREADME.TXT README.TXTREADME.TXT ................................ ................................ ................................ ......................... 8 对本系列文章的总体介绍 对本系列文章的总体介绍 对本系列文章的总体介绍 ................................ ................................ ................................ ....... 9 对第一章的简单介绍 对第一章的简单介绍 ................................ ................................ ................................ ............. 11 ATL ATL ATL 背景知识 ................................ ................................ ................................ ....................... 11 ATL 和 WTL 的发展历史 ................................ ................................ ......................... 12 ATL-style 模板 ................................ ................................ ................................ ............. 12 ATL ATL ATL 窗口类 ................................ ................................ ................................ ........................... 15 定义一个窗口的实现 定义一个窗口的实现 ................................ ................................ ................................ ............. 17 填写消息映射链 ................................ ................................ ................................ ............. 19 高级消息映射链和嵌 高级消息映射链和嵌 入类 ................................ ................................ ................................ ..... 21 ATLATL 程序的结构 ................................ ................................ ................................ .................... 24 ATLATL 中的对话框 ................................ ................................ ................................ .................... 27 WTL for MFC Programmers, Part II - WTL GUI Base Classes ................................ ............. 32 对第二部分的介绍 对第二部分的介绍 ................................ ................................ ................................ ................. 32 WTL WTL WTL WTL 总体印象 ................................ ................................ ................................ ...................... 32 开始WTLWTLWTL程序 ................................ ................................ ................................ .................. 33 WTL WTL WTL WTL 对消息映射的增强 ................................ ................................ ................................ ...... 36 从 WTLWTLWTL的应用程序生成向导能得到什么 的应用程序生成向导能得到什么 的应用程序生成向导能得到什么 ................................ ................................ .......... 42 使用向导的整个过程 ................................ ................................ ................................ ..... 42 查看生成的代码 ................................ ................................ ................................ ............. 44 CMessageLoop CMessageLoop CMessageLoop CMessageLoop CMessageLoop CMessageLoop CMessageLoop CMessageLoop 的内部实现 ................................ ................................ ................................ .. 47 CFrameWindowImpl CFrameWindowImpl CFrameWindowImpl CFrameWindowImpl CFrameWindowImpl CFrameWindowImpl CFrameWindowImpl CFrameWindowImpl CFrameWindowImpl CFrameWindowImpl CFrameWindowImpl CFrameWindowImpl CFrameWindowImpl 的内部实现 ................................ ................................ ......................... 48 回到前面的时钟程序 回到前面的时钟程序 ................................ ................................ ................................ ............. 50 界面元素的自动更新 界面元素的自动更新 (UI Updating)(UI Updating)(UI Updating)(UI Updating) (UI Updating) (UI Updating)(UI Updating)(UI Updating)(UI Updating) (UI Updating) ................................ ................................ ...................... 51 添加控制时钟的新菜单项 ................................ ................................ ............................. 52 调用 UIEnable()................................ ................................ ................................ ............ 53 消息映射链中最后需要注意的地方 消息映射链中最后需要注意的地方 消息映射链中最后需要注意的地方 消息映射链中最后需要注意的地方 ................................ ................................
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值