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

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

 

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;
    }
};

 

 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值