网上找到Duilib入门教程中,第一个给的时基于SDK的例子,在这里,自己写了个MFC的,与入门教程中的例子一样。
新建一个窗口类(CTestDlg)
TestDlg.h内容如下:
[cpp] view
plaincopy
plaincopy
- #pragma once
- class CTestDlg:public CWindowWnd, INotifyUI
- {
- public:
- CTestDlg(void);
- ~CTestDlg(void);
- public:
- LPCTSTR GetWindowClassName() const;
- UINT GetClassStyle() const;
- void OnFinalMessage(HWND hWnd);
- void Notify(TNotifyUI& msg);
- LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
- private:
- CPaintManagerUI m_pm;
- };
TestDlg.cpp内容如下:
[cpp] view
plaincopy
plaincopy
- #include "StdAfx.h"
- #include "TestDlg.h"
- CTestDlg::CTestDlg(void)
- {
- }
- CTestDlg::~CTestDlg(void)
- {
- }
- LPCTSTR CTestDlg::GetWindowClassName() const
- {
- return L"CTestDlg";
- }
- UINT CTestDlg::GetClassStyle() const
- {
- return UI_CLASSSTYLE_FRAME | CS_DBLCLKS;
- }
- void CTestDlg::OnFinalMessage(HWND hWnd)
- {
- }
- void CTestDlg::Notify(TNotifyUI& msg)
- {
- if( msg.sType == _T("click") ) {
- if( msg.pSender->GetName() == _T("closebtn") ) {
- Close();
- }
- }
- }
- LRESULT CTestDlg::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- if( uMsg == WM_CREATE ) {
- m_pm.Init(m_hWnd);
- CControlUI *pButton = new CButtonUI;
- pButton->SetName(_T("closebtn"));
- pButton->SetBkColor(0xFFFF0000);
- m_pm.AttachDialog(pButton);
- m_pm.AddNotifier(this);
- return 0;
- }
- else if( uMsg == WM_DESTROY ) {
- ::PostQuitMessage(0);
- }
- LRESULT lRes = 0;
- if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) ) return lRes;
- return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
- }
然后,在主对话框的类中,增加一个成员变量
[cpp] view
plaincopy
plaincopy
- CTestDlg m_testDlg;
在OnInitDialog函数中,增加如下两行代码:
[cpp] view
plaincopy
plaincopy
- m_testDlg.Create(*this, NULL, UI_WNDSTYLE_CHILD, 0, 0, 0, 642, 520);
- m_testDlg.ShowWindow(TRUE);
编译运行,即可。