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

来源:互联网 发布:ubuntu ibus 设置 编辑:程序博客网 时间:2024/06/07 15:32

 

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.csdn.net/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;
    }
};

 

 

 

 

 

原创粉丝点击