Duilib在MFC中使用

今天手痒实验了下直接在CDLG里边用DUILIB(我不想直接用duilib了事,后期可能会做做大改动,所以没有直接在duilib::CWindowWnd里边想办法)
发现唯一需要注意的就是需要在OnPaint中屏蔽CDialog的paint,,,duilib还是很牛逼的
同样,把duilib整合成MFC的“插件”之后,那个CWindowWnd::Close会自动delete this的故障也没了,额,应该说是特性,不是故障,,,不过这个特性给我带来的麻烦比较多,,,
 
[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // dui_mfcDlg.h : header file  
  2. //  
  3.   
  4. #pragma once  
  5.   
  6. // Cdui_mfcDlg dialog  
  7. class Cdui_mfcDlg : public CDialog  
  8.     , public DuiLib::INotifyUI, public DuiLib::IMessageFilterUI  
  9. {  
  10. // Construction  
  11. public:  
  12.  Cdui_mfcDlg(CWnd* pParent = NULL);    // standard constructor  
  13.   
  14. // Dialog Data  
  15.    enum { IDD = IDD_DUI_MFC_DIALOG };  
  16.   
  17.     protected:  
  18.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support  
  19.   
  20.   
  21. // Implementation  
  22. protected:  
  23.     HICON m_hIcon;  
  24.   
  25.     // Generated message map functions  
  26.     virtual BOOL OnInitDialog();  
  27.     afx_msg void OnSysCommand(UINT nID, LPARAM lParam);  
  28.     afx_msg void OnPaint();  
  29.     afx_msg HCURSOR OnQueryDragIcon();  
  30.     DECLARE_MESSAGE_MAP()  
  31.   
  32. private:  
  33.     bool OnHChanged(void* param) {  
  34.         DuiLib::TNotifyUI* pMsg = (DuiLib::TNotifyUI*)param;  
  35.         if( pMsg->sType == _T("valuechanged") ) {  
  36.             short H, S, L;  
  37.             DuiLib::CPaintManagerUI::GetHSL(&H, &S, &L);  
  38.             DuiLib::CPaintManagerUI::SetHSL(true, (static_cast<DuiLib::CSliderUI*>(pMsg->pSender))->GetValue(), S, L);  
  39.         }  
  40.         return true;  
  41.     }  
  42.   
  43.     bool OnSChanged(void* param) {  
  44.         DuiLib::TNotifyUI* pMsg = (DuiLib::TNotifyUI*)param;  
  45.         if( pMsg->sType == _T("valuechanged") ) {  
  46.             short H, S, L;  
  47.             DuiLib::CPaintManagerUI::GetHSL(&H, &S, &L);  
  48.             DuiLib::CPaintManagerUI::SetHSL(true, H, (static_cast<DuiLib::CSliderUI*>(pMsg->pSender))->GetValue(), L);  
  49.         }  
  50.         return true;  
  51.     }  
  52.   
  53.     bool OnLChanged(void* param) {  
  54.         DuiLib::TNotifyUI* pMsg = (DuiLib::TNotifyUI*)param;  
  55.         if( pMsg->sType == _T("valuechanged") ) {  
  56.             short H, S, L;  
  57.             DuiLib::CPaintManagerUI::GetHSL(&H, &S, &L);  
  58.             DuiLib::CPaintManagerUI::SetHSL(true, H, S, (static_cast<DuiLib::CSliderUI*>(pMsg->pSender))->GetValue());  
  59.         }  
  60.         return true;  
  61.     }  
  62.   
  63.     bool OnAlphaChanged(void* param) {  
  64.         DuiLib::TNotifyUI* pMsg = (DuiLib::TNotifyUI*)param;  
  65.         if( pMsg->sType == _T("valuechanged") ) {  
  66.             m_pm.SetTransparent((static_cast<DuiLib::CSliderUI*>(pMsg->pSender))->GetValue());  
  67.         }  
  68.         return true;  
  69.     }  
  70.   
  71.     void OnPrepare()   
  72.     {  
  73.         DuiLib::CSliderUI* pSilder = static_cast<DuiLib::CSliderUI*>(m_pm.FindControl(_T("alpha_controlor")));  
  74.         if( pSilder ) pSilder->OnNotify += MakeDelegate(this, &Cdui_mfcDlg::OnAlphaChanged);  
  75.         pSilder = static_cast<DuiLib::CSliderUI*>(m_pm.FindControl(_T("h_controlor")));  
  76.         if( pSilder ) pSilder->OnNotify += MakeDelegate(this, &Cdui_mfcDlg::OnHChanged);  
  77.         pSilder = static_cast<DuiLib::CSliderUI*>(m_pm.FindControl(_T("s_controlor")));  
  78.         if( pSilder ) pSilder->OnNotify += MakeDelegate(this, &Cdui_mfcDlg::OnSChanged);  
  79.         pSilder = static_cast<DuiLib::CSliderUI*>(m_pm.FindControl(_T("l_controlor")));  
  80.         if( pSilder ) pSilder->OnNotify += MakeDelegate(this, &Cdui_mfcDlg::OnLChanged);  
  81.     }  
  82.   
  83. private:  
  84.     DuiLib::CPaintManagerUI m_pm;  
  85.   
  86.     // INotifyUI  
  87. public:  
  88.     virtual void Notify(DuiLib::TNotifyUI& msg);  
  89.   
  90.     // IMessageFilterUI   
  91. public:  
  92.     virtual LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled);  
  93.     virtual BOOL PreTranslateMessage(MSG* pMsg);  
  94.     afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);  
  95. };  
  96.    
  97.   
  98.    
  99.   
  100. // dui_mfcDlg.cpp : implementation file  
  101. //  
  102.   
  103. #include "stdafx.h"  
  104. #include "dui_mfc.h"  
  105. #include "dui_mfcDlg.h"  
  106.   
  107. #ifdef _DEBUG  
  108. #define new DEBUG_NEW  
  109. #endif  
  110.   
  111.   
  112. // CAboutDlg dialog used for App About  
  113.   
  114. class CAboutDlg : public CDialog  
  115. {  
  116. public:  
  117.  CAboutDlg();  
  118.   
  119. // Dialog Data  
  120.  enum { IDD = IDD_ABOUTBOX };  
  121.   
  122.  protected:  
  123.  virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support  
  124.   
  125. // Implementation  
  126. protected:  
  127.  DECLARE_MESSAGE_MAP()  
  128. };  
  129.   
  130. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)  
  131. {  
  132. }  
  133.   
  134. void CAboutDlg::DoDataExchange(CDataExchange* pDX)  
  135. {  
  136.  CDialog::DoDataExchange(pDX);  
  137. }  
  138.   
  139. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)  
  140. END_MESSAGE_MAP()  
  141.   
  142.   
  143. // Cdui_mfcDlg dialog  
  144.   
  145.    
  146.   
  147.   
  148. Cdui_mfcDlg::Cdui_mfcDlg(CWnd* pParent /*=NULL*/)  
  149.  : CDialog(Cdui_mfcDlg::IDD, pParent)  
  150. {  
  151.  m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);  
  152. }  
  153.   
  154. void Cdui_mfcDlg::DoDataExchange(CDataExchange* pDX)  
  155. {  
  156.  CDialog::DoDataExchange(pDX);  
  157. }  
  158.   
  159. BEGIN_MESSAGE_MAP(Cdui_mfcDlg, CDialog)  
  160.  ON_WM_SYSCOMMAND()  
  161.  ON_WM_PAINT()  
  162.  ON_WM_QUERYDRAGICON()  
  163.  //}}AFX_MSG_MAP  
  164.     ON_WM_CREATE()  
  165. END_MESSAGE_MAP()  
  166.   
  167.   
  168. // Cdui_mfcDlg message handlers  
  169.   
  170. BOOL Cdui_mfcDlg::OnInitDialog()  
  171. {  
  172.  CDialog::OnInitDialog();  
  173.   
  174.  // Add "About..." menu item to system menu.  
  175.   
  176.  // IDM_ABOUTBOX must be in the system command range.  
  177.  ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);  
  178.  ASSERT(IDM_ABOUTBOX < 0xF000);  
  179.   
  180.  CMenu* pSysMenu = GetSystemMenu(FALSE);  
  181.  if (pSysMenu != NULL)  
  182.  {  
  183.   BOOL bNameValid;  
  184.   CString strAboutMenu;  
  185.   bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);  
  186.   ASSERT(bNameValid);  
  187.   if (!strAboutMenu.IsEmpty())  
  188.   {  
  189.    pSysMenu->AppendMenu(MF_SEPARATOR);  
  190.    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);  
  191.   }  
  192.  }  
  193.   
  194.  // Set the icon for this dialog.  The framework does this automatically  
  195.  //  when the application's main window is not a dialog  
  196.  SetIcon(m_hIcon, TRUE);   // Set big icon  
  197.  SetIcon(m_hIcon, FALSE);  // Set small icon  
  198.   
  199.  // TODO: Add extra initialization here  
  200.   
  201.  return TRUE;  // return TRUE  unless you set the focus to a control  
  202. }  
  203.   
  204. void Cdui_mfcDlg::OnSysCommand(UINT nID, LPARAM lParam)  
  205. {  
  206.  if ((nID & 0xFFF0) == IDM_ABOUTBOX)  
  207.  {  
  208.   CAboutDlg dlgAbout;  
  209.   dlgAbout.DoModal();  
  210.  }  
  211.  else  
  212.  {  
  213.   CDialog::OnSysCommand(nID, lParam);  
  214.  }  
  215. }  
  216.   
  217. // If you add a minimize button to your dialog, you will need the code below  
  218. //  to draw the icon.  For MFC applications using the document/view model,  
  219. //  this is automatically done for you by the framework.  
  220.   
  221. void Cdui_mfcDlg::OnPaint()  
  222. {  
  223.  if (IsIconic())  
  224.  {  
  225.   CPaintDC dc(this); // device context for painting  
  226.   
  227.   SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);  
  228.   
  229.   // Center icon in client rectangle  
  230.   int cxIcon = GetSystemMetrics(SM_CXICON);  
  231.   int cyIcon = GetSystemMetrics(SM_CYICON);  
  232.   CRect rect;  
  233.   GetClientRect(&rect);  
  234.   int x = (rect.Width() - cxIcon + 1) / 2;  
  235.   int y = (rect.Height() - cyIcon + 1) / 2;  
  236.   
  237.   // Draw the icon  
  238.   dc.DrawIcon(x, y, m_hIcon);  
  239.  }  
  240.  else  
  241.  {  
  242.   //CDialog::OnPaint();  
  243.  }  
  244. }  
  245.   
  246. // The system calls this function to obtain the cursor to display while the user drags  
  247. //  the minimized window.  
  248. HCURSOR Cdui_mfcDlg::OnQueryDragIcon()  
  249. {  
  250.  return static_cast<HCURSOR>(m_hIcon);  
  251. }  
  252.   
  253.   
  254. void Cdui_mfcDlg::Notify(DuiLib::TNotifyUI& msg)  
  255. {  
  256.     if( msg.sType == _T("windowinit") ) OnPrepare();  
  257.     else if( msg.sType == _T("click") ) {  
  258.         if( msg.pSender->GetName() == _T("insertimagebtn") ) {  
  259.             DuiLib::CRichEditUI* pRich = static_cast<DuiLib::CRichEditUI*>(m_pm.FindControl(_T("testrichedit")));  
  260.             if( pRich ) {  
  261.                 pRich->RemoveAll();  
  262.             }  
  263.         }  
  264.         else if( msg.pSender->GetName() == _T("changeskinbtn") ) {  
  265.             if( DuiLib::CPaintManagerUI::GetResourcePath() == DuiLib::CPaintManagerUI::GetInstancePath() )  
  266.                 DuiLib::CPaintManagerUI::SetResourcePath(DuiLib::CPaintManagerUI::GetInstancePath() + _T("skin\\FlashRes"));  
  267.             else  
  268.                 DuiLib::CPaintManagerUI::SetResourcePath(DuiLib::CPaintManagerUI::GetInstancePath());  
  269.             DuiLib::CPaintManagerUI::ReloadSkin();  
  270.         }  
  271.     }  
  272. }  
  273.   
  274. LRESULT Cdui_mfcDlg::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled)  
  275. {  
  276.     if( uMsg == WM_ERASEBKGND )   
  277.     {  
  278.         return true;  
  279.     }  
  280.   
  281.     return false;  
  282. }  
  283. BOOL Cdui_mfcDlg::PreTranslateMessage(MSG* pMsg)  
  284. {  
  285.     // TODO: Add your specialized code here and/or call the base class  
  286.     bool bHandled = false;  
  287.     MessageHandler(pMsg->message, pMsg->wParam, pMsg->lParam, bHandled);  
  288.     if (bHandled)  
  289.     {  
  290.         return true;  
  291.     }  
  292.     else  
  293.     {  
  294.         LRESULT lresult = 0;  
  295.         if (m_pm.MessageHandler(pMsg->message, pMsg->wParam, pMsg->lParam, lresult))  
  296.         {  
  297.             return lresult;  
  298.         }  
  299.     }  
  300.   
  301.     return __super::PreTranslateMessage(pMsg);  
  302. }  
  303.   
  304. int Cdui_mfcDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)  
  305. {  
  306.     if (__super::OnCreate(lpCreateStruct) == -1)  
  307.         return -1;  
  308.   
  309.     // TODO:  Add your specialized creation code here  
  310.     m_pm.Init(m_hWnd);  
  311.     DuiLib::CDialogBuilder builder;  
  312.     DuiLib::CControlUI* pRoot = builder.Create(_T("test1.xml"), (UINT)0, NULL, &m_pm);  
  313.     ASSERT(pRoot && "Failed to parse XML");  
  314.     m_pm.AttachDialog(pRoot);  
  315.     m_pm.AddNotifier(this);  
  316.   
  317.     return 0;  
  318. }  


注:在WindowProc 调用消息处理要比在PreTranslateMessage调用更合理。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值