当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

WindowsTemplateLibrary(WTL)-创建简单的窗口以及对话框

发表于: 2015-11-13   作者:互联网   来源:转载   浏览:
摘要: Windows Template Library (WTL) is a C++ library for developing Windows applications and UI components. It extends ATL (Active Template Library) and provides a set of classes for controls, dialogs, fra

Windows Template Library (WTL) is a C++ library for developing Windows applications and UI components. It extends ATL (Active Template Library) and provides a set of classes for controls, dialogs, frame windows, GDI objects, and more.
以下代码包含了如下内容:
定义以及创建窗口和对话框
消息映射
WTL程序结构
以及一些其他方面的东西
参考资料:WTL for MFC Programmers
#include <atlbase.h>
#include <atlwin.h>
#include "resource.h"
extern CComModule _Module;
class CAboutDlg : public CDialogImpl<CAboutDlg>
{
public:
enum { IDD = IDD_ABOUT };
     BEGIN_MSG_MAP(CAboutDlg)
         MESSAGE_HANDLER(WM_INITDIALOG,OnInitDialog)
         MESSAGE_HANDLER(WM_CLOSE,OnClose)
         COMMAND_ID_HANDLER(IDOK,OnOKCancel)
         COMMAND_ID_HANDLER(IDCANCEL,OnOKCancel)
     END_MSG_MAP()
public:
LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
     {
         CenterWindow();
return TRUE;    
     }
LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
     {
         EndDialog(IDCANCEL);
return 0;
     }
LRESULT OnOKCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
     {
         EndDialog(wID);
return 0;
     }
};
template <class T,COLORREF t_crBrushColor>
class CPaintBkgnd : public CMessageMap
{
public:
     CPaintBkgnd() { m_hbrBkgnd = CreateSolidBrush(t_crBrushColor); }
     ~CPaintBkgnd() { DeleteObject(m_hbrBkgnd); }
     BEGIN_MSG_MAP(CPaintBkgnd)
         MESSAGE_HANDLER(WM_ERASEBKGND,OnEraseBkgnd)
     END_MSG_MAP()
public:
LRESULT OnEraseBkgnd(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
     {
         T*    pT = static_cast<T*>(this);
HDC   dc = (HDC) wParam;
         RECT rcClient;
         pT->GetClientRect ( &rcClient );
         FillRect ( dc, &rcClient, m_hbrBkgnd );
return TRUE;
     }
protected:
HBRUSH m_hbrBkgnd;
};
typedef CWinTraits<WS_OVERLAPPEDWINDOW> ChypWinTraits;
class CFirstWindow : public CWindowImpl<CFirstWindow,CWindow,ChypWinTraits>,
public CPaintBkgnd<CFirstWindow,RGB(0,0,255)>
{
public:
     DECLARE_WND_CLASS("hyp First WTL Window Class")
typedef CPaintBkgnd<CFirstWindow, RGB(0,0,255)> CPaintBkgndBase;
     BEGIN_MSG_MAP(CFirstWindow)
         MESSAGE_HANDLER(WM_CLOSE,OnClose)
         MESSAGE_HANDLER(WM_DESTROY,OnDestroy)
         MESSAGE_HANDLER(WM_CREATE,OnCreate)
         COMMAND_ID_HANDLER(IDC_ABOUT,OnAbout)
         CHAIN_MSG_MAP(CPaintBkgndBase)
     END_MSG_MAP()
public:
LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
     {
         DestroyWindow();
return 0;
     }
LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
     {
         PostQuitMessage(0);
return 0;
     }
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
     {
HMENU hmenu = LoadMenu ( _Module.GetResourceInstance(),MAKEINTRESOURCE(IDR_MENU) );
         SetMenu ( hmenu );
return 0;
     }
LRESULT OnAbout(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
     {
         CAboutDlg dlg;
         dlg.DoModal();
return 0;
     }
};
CComModule _Module;
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
                    )
{
     _Module.Init(NULL,hInstance);
     CFirstWindow wndMain;
     MSG msg;
if ( wndMain.Create(NULL,CWindow::rcDefault,"hyp First WTL Window") == NULL)
     {
return FALSE;
     }
     wndMain.ShowWindow(nCmdShow);
     wndMain.UpdateWindow();
while ( GetMessage(&msg,wndMain.m_hWnd,0,0) )
     {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
     }
     _Module.Term();
return (int)msg.wParam;
}

 

 

原文:http://hi.baidu.com/hypkb/blog/item/37655d58c26483232834f0c0.html

WindowsTemplateLibrary(WTL)-创建简单的窗口以及对话框

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号