MFC中使用Duilib--1

网上找到Duilib入门教程中,第一个给的时基于SDK的例子,在这里,自己写了个MFC的,与入门教程中的例子一样。

新建一个窗口类(CTestDlg)

TestDlg.h内容如下:

  1. #pragma once  
  2. class CTestDlg:public CWindowWnd, INotifyUI  
  3. {  
  4. public:  
  5.     CTestDlg(void);  
  6.     ~CTestDlg(void);  
  7.       
  8. public:  
  9.     LPCTSTR GetWindowClassName() const;  
  10.     UINT GetClassStyle() const;  
  11.     void OnFinalMessage(HWND hWnd);  
  12.     void Notify(TNotifyUI& msg);  
  13.     LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);  
  14. private:  
  15.     CPaintManagerUI m_pm;  
  16. };  


TestDlg.cpp内容如下:

  1. #include "StdAfx.h"  
  2. #include "TestDlg.h"  
  3.   
  4.   
  5. CTestDlg::CTestDlg(void)  
  6. {  
  7. }  
  8.   
  9. CTestDlg::~CTestDlg(void)  
  10. {  
  11. }  
  12.   
  13. LPCTSTR CTestDlg::GetWindowClassName() const  
  14. {  
  15.     return L"CTestDlg";  
  16. }  
  17.   
  18. UINT CTestDlg::GetClassStyle() const  
  19. {  
  20.     return UI_CLASSSTYLE_FRAME | CS_DBLCLKS;  
  21. }  
  22.   
  23. void CTestDlg::OnFinalMessage(HWND hWnd)  
  24. {  
  25. }  
  26.   
  27. void CTestDlg::Notify(TNotifyUI& msg)  
  28. {  
  29.     if( msg.sType == _T("click") ) {  
  30.         if( msg.pSender->GetName() == _T("closebtn") ) {  
  31.             Close();  
  32.         }  
  33.     }  
  34. }  
  35.   
  36. LRESULT CTestDlg::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)  
  37. {  
  38.     if( uMsg == WM_CREATE ) {  
  39.         m_pm.Init(m_hWnd);  
  40.         CControlUI *pButton = new CButtonUI;  
  41.         pButton->SetName(_T("closebtn"));  
  42.         pButton->SetBkColor(0xFFFF0000);  
  43.         m_pm.AttachDialog(pButton);  
  44.         m_pm.AddNotifier(this);  
  45.         return 0;  
  46.     }  
  47.     else if( uMsg == WM_DESTROY ) {  
  48.         ::PostQuitMessage(0);  
  49.     }  
  50.     LRESULT lRes = 0;  
  51.     if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) ) return lRes;  
  52.     return CWindowWnd::HandleMessage(uMsg, wParam, lParam);  
  53.   
  54. }  


然后,在主对话框的类中,增加一个成员变量

  1. CTestDlg m_testDlg;  


在OnInitDialog函数中,增加如下两行代码:

  1. m_testDlg.Create(*this, NULL, UI_WNDSTYLE_CHILD, 0, 0, 0, 642, 520);  
  2. m_testDlg.ShowWindow(TRUE);  


编译运行,即可。

原文地址:https://www.cnblogs.com/jinsedemaitian/p/5589090.html