duilib 的DuiMessageBox

直接发源码!!

.h

[cpp]  view plain copy
  1. #ifndef DUI_MESSAGEBOX_HPP  
  2. #define DUI_MESSAGEBOX_HPP  
  3. #include "StdAfx.h"  
  4. #include "WndShadow.h"  
  5.   
  6. enum Dui_MessageBox_Type  
  7. {  
  8.     DUI_DEFAULT=0,  
  9.     DUI_YESNO  
  10. };  
  11.   
  12. enum Dui_MessageBox_Return  
  13. {  
  14.     DUI_OK=0,  
  15.     DUI_CANCEL,  
  16.     DUI_YES,  
  17.     DUI_NO  
  18. };  
  19.   
  20. class CDuiMessageBox : public CWindowWnd, public INotifyUI, public CNotifyPump  
  21. {  
  22. public:  
  23.     CDuiMessageBox(LPCTSTR lpTitle,  
  24.         LPCTSTR lpMsg,  
  25.         Dui_MessageBox_Type nType,  
  26.         Dui_MessageBox_Return* nReturn);  
  27.       
  28.     LPCTSTR GetWindowClassName() const ;  
  29.     virtual UINT GetClassStyle() const;  
  30.     virtual void OnFinalMessage( HWND hWnd );  
  31.     virtual void Notify(TNotifyUI& msg);  
  32.   
  33.     LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);  
  34.     LRESULT OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);  
  35.     LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);  
  36.     LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);  
  37.     LRESULT ResponseDefaultKeyEvent(WPARAM wParam);  
  38.   
  39.     DUI_DECLARE_MESSAGE_MAP()  
  40.           
  41. public:  
  42.     void OnClick(TNotifyUI& msg);  
  43.     void OnPrepare(TNotifyUI& msg);  
  44.   
  45.   
  46.       
  47.   
  48.   
  49.       
  50. private:  
  51.     CPaintManagerUI m_PaintManager;  
  52.     CWndShadow m_WndShadow;//窗口阴影  
  53.   
  54.     CString m_szTitle;  
  55.     CString m_szMsg;  
  56.     Dui_MessageBox_Type m_nType;  
  57.     Dui_MessageBox_Return* m_nReturn ;  
  58. };  
  59.   
  60. #endif // DUI_MESSAGEBOX_HPP  
  61.   
  62. Dui_MessageBox_Return DuiMessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpTitle=NULL, Dui_MessageBox_Type nType=DUI_DEFAULT);  
[cpp]  view plain copy
  1. </pre><pre name="code" class="cpp">.cpp  
[cpp]  view plain copy
  1. <pre name="code" class="cpp">#include "DuiMessageBox.h"  
  2.   
  3. /  
  4.   
  5. DUI_BEGIN_MESSAGE_MAP(CDuiMessageBox, CNotifyPump)  
  6.     DUI_ON_MSGTYPE(DUI_MSGTYPE_WINDOWINIT, OnPrepare)  
  7.     DUI_ON_MSGTYPE(DUI_MSGTYPE_CLICK, OnClick)  
  8. DUI_END_MESSAGE_MAP()  
  9.   
  10. CDuiMessageBox::CDuiMessageBox( LPCTSTR lpTitle,   
  11.             LPCTSTR lpMsg,  
  12.             Dui_MessageBox_Type nType,  
  13.             Dui_MessageBox_Return* nReturn)  
  14. {  
  15.     m_szTitle = lpTitle;  
  16.     m_szMsg = lpMsg;  
  17.     m_nType = nType;  
  18.     m_nReturn = nReturn;  
  19. }  
  20.   
  21. void CDuiMessageBox::OnFinalMessage( HWND hWnd )  
  22. {  
  23.     m_PaintManager.RemoveNotifier(this);  
  24.     m_PaintManager.ReapObjects(m_PaintManager.GetRoot());  
  25.     delete this;  
  26. }  
  27.   
  28. void CDuiMessageBox::Notify( TNotifyUI& msg )  
  29. {  
  30.     return CNotifyPump::NotifyPump(msg);  
  31. }  
  32.   
  33. void CDuiMessageBox::OnClick( TNotifyUI& msg )  
  34. {  
  35.     CDuiString sCtrlName = msg.pSender->GetName();  
  36.     if( sCtrlName == _T("okbtn") )  
  37.     {  
  38.         *m_nReturn = DUI_OK;          
  39.     }  
  40.     else if( sCtrlName == _T("cancelbtn"))  
  41.     {   
  42.         *m_nReturn = DUI_CANCEL;  
  43.     }  
  44.     else if( sCtrlName == _T("yesbtn"))  
  45.     {   
  46.         *m_nReturn = DUI_YES;  
  47.     }  
  48.     else if( sCtrlName == _T("nobtn"))  
  49.     {   
  50.         *m_nReturn = DUI_NO;          
  51.     }  
  52.     Close();  
  53.     return;  
  54. }  
  55.   
  56.   
  57. LPCTSTR CDuiMessageBox::GetWindowClassName( void ) const  
  58. {  
  59.     return _T("DuiMessageBox");  
  60. }  
  61.   
  62. UINT CDuiMessageBox::GetClassStyle() const  
  63. {  
  64.     return CS_DBLCLKS;  
  65. }  
  66.   
  67. void CDuiMessageBox::OnPrepare( TNotifyUI& msg )  
  68. {  
  69.       
  70. }  
  71.   
  72. LRESULT CDuiMessageBox::OnSize( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )  
  73. {  
  74.     SIZE szRoundCorner = m_PaintManager.GetRoundCorner();  
  75.     if( !::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0) )  
  76.     {  
  77.         CDuiRect rcWnd;  
  78.         ::GetWindowRect(*this, &rcWnd);  
  79.         rcWnd.Offset(-rcWnd.left, -rcWnd.top);  
  80.         rcWnd.right++; rcWnd.bottom++;  
  81.         HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy);  
  82.         ::SetWindowRgn(*this, hRgn, TRUE);  
  83.         ::DeleteObject(hRgn);  
  84.   
  85.         /*CTextUI* pMsgText = static_cast<CTextUI*>(m_PaintManager.FindControl(_T("msg_text"))); 
  86.         if (NULL != pMsgText) 
  87.         { 
  88.             int x = pMsgText->GetX(); 
  89.             int y = pMsgText->GetY(); 
  90.              
  91.             int ynum = y + 70; 
  92.         }*/  
  93.     }  
  94.   
  95.       
  96.   
  97.     bHandled = FALSE;  
  98.     return 0;  
  99. }  
  100.   
  101. LRESULT CDuiMessageBox::OnNcHitTest( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )  
  102. {  
  103.     POINT pt; pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam);  
  104.     ::ScreenToClient(*this, &pt);  
  105.   
  106.     RECT rcClient;  
  107.     ::GetClientRect(*this, &rcClient);  
  108.   
  109.     RECT rcCaption = m_PaintManager.GetCaptionRect();  
  110.     if( pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \  
  111.         && pt.y >= rcCaption.top && pt.y < rcCaption.bottom )  
  112.     {  
  113.         CControlUI* pControl = static_cast<CControlUI*>(m_PaintManager.FindControl(pt));  
  114.         if( pControl && _tcscmp(pControl->GetClass(), _T("ButtonUI")) != 0 &&   
  115.             _tcscmp(pControl->GetClass(), _T("OptionUI")) != 0  )  
  116.             return HTCAPTION;  
  117.     }  
  118.   
  119.     return HTCLIENT;  
  120. }  
  121.   
  122. LRESULT CDuiMessageBox::HandleMessage( UINT uMsg, WPARAM wParam, LPARAM lParam )  
  123. {  
  124.     LRESULT lRes = 0;  
  125.     BOOL bHandled = TRUE;  
  126.   
  127.     switch (uMsg)  
  128.     {  
  129.     case WM_CREATE:         lRes = OnCreate(uMsg, wParam, lParam, bHandled); break;  
  130.       
  131.     //case WM_NCACTIVATE:       lRes = OnNcActivate(uMsg, wParam, lParam, bHandled); break;  
  132.     //case WM_NCCALCSIZE:       lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); break;  
  133.     case WM_NCPAINT:        lRes = 0; break;  
  134.     case WM_NCHITTEST:      lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled); break;  
  135.     case WM_GETMINMAXINFO:  lRes = 0;bHandled = FALSE; break;  
  136.     case WM_SIZE:           lRes = OnSize(uMsg, wParam, lParam, bHandled); break;  
  137.           
  138.     default:                bHandled = FALSE; break;  
  139.     }  
  140.     if (bHandled) return lRes;  
  141.       
  142.     if (m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes))  
  143.         return lRes;  
  144.     return CWindowWnd::HandleMessage(uMsg, wParam, lParam);  
  145. }  
  146.   
  147. #include <math.h>  
  148. LRESULT CDuiMessageBox::OnCreate( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )  
  149. {  
  150.     LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);  
  151.     styleValue &= ~WS_CAPTION;  
  152.     ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);  
  153.       
  154.     m_PaintManager.Init(m_hWnd);  
  155.     /* 
  156.     SIZE size = {0,0}; 
  157.     HDC hDC = m_PaintManager.GetPaintDC(); 
  158.     ::SetBkMode(hDC, TRANSPARENT); 
  159.     HFONT hOldFont = (HFONT)::SelectObject(hDC, m_PaintManager.GetFont(12)); 
  160.     ::GetTextExtentPoint32(hDC, m_szMsg, m_szMsg.GetLength(), &size); 
  161.     ::SelectObject(hDC, hOldFont); 
  162.  
  163.     double nRow = sqrt(double(size.cx / size.cy / 10));*/  
  164.     int nLinks = 0;  
  165.     RECT rcText ={ 0, 0, 9999, 9999 };  
  166.   
  167.     CRenderEngine::DrawHtmlText(m_PaintManager.GetPaintDC(), &m_PaintManager, rcText, m_szMsg, 0, NULL, NULL, nLinks, DT_CALCRECT | DT_WORDBREAK);  
  168.   
  169.     int nClientX = rcText.right - rcText.left + 50;  
  170.     int nClientY = rcText.bottom - rcText.top + 100 ;  
  171.   
  172.     if(nClientX < 240 ) nClientX = 240;  
  173.     if(nClientY < 130 ) nClientY = 130;  
  174.     m_PaintManager.SetInitSize(nClientX, nClientY);  
  175.     RECT rcCaption = {0, 0, 0, nClientY};  
  176.     m_PaintManager.SetCaptionRect(rcCaption);  
  177.     m_PaintManager.SetRoundCorner(2, 2);  
  178.       
  179.     CVerticalLayoutUI* pRoot = new CVerticalLayoutUI;  
  180.     pRoot->SetBkColor(0xFFCFDDD0);  
  181.     RECT rcInset = {10, 2, 10, 2};  
  182.     pRoot->SetInset(rcInset);  
  183.     pRoot->SetBorderSize(2);  
  184.     pRoot->SetBorderColor(0xFF314E34);  
  185.     //提示标题  
  186.     CLabelUI* pTitleLabel = new CLabelUI;  
  187.     pTitleLabel->SetFixedHeight(30);  
  188.     pTitleLabel->SetAttribute(_T("align"), _T("center"));  
  189.     pTitleLabel->SetTextColor(0xFFA31515);  
  190.     pTitleLabel->SetText(m_szTitle);  
  191.     pRoot->Add(pTitleLabel);  
  192.   
  193.     pRoot->Add(new CControlUI);  
  194.     //msg 正文  
  195.     CTextUI* pMsgLabel = new CTextUI;  
  196.     pMsgLabel->SetName(_T("msg_text"));  
  197.     pMsgLabel->SetText(m_szMsg);  
  198.     pMsgLabel->SetShowHtml(true);  
  199.     pMsgLabel->SetAttribute(_T("align"), _T("center"));  
  200.     //pMsgLabel->EstimateSize();  
  201.     pRoot->Add(pMsgLabel);  
  202.       
  203.   
  204.     pRoot->Add(new CControlUI);  
  205.   
  206.     //类型 按钮个数和功能  
  207.     CHorizontalLayoutUI* pBtnHor = new CHorizontalLayoutUI;  
  208.     pBtnHor->SetFixedHeight(40);  
  209.     pBtnHor->Add(new CControlUI);  
  210.     //pTitleHor->SetBkColor(0xFF1777EF);  
  211.     //CControlUI* pControl = new CControlUI;  
  212.     //pTitleHor->Add(pControl);  
  213.     switch (m_nType)  
  214.     {  
  215.     case DUI_DEFAULT:  
  216.         {  
  217.             CButtonUI* pOKbtn = new CButtonUI;  
  218.             pOKbtn->SetName(_T("okbtn"));  
  219.             pOKbtn->SetText(_T("确定"));  
  220.             pOKbtn->SetNormalImage(_T("file='button_normal.png' corner='5,5,5,5'"));  
  221.             pOKbtn->SetHotImage(_T("file='button_hover.png' corner='5,5,5,5'"));  
  222.             pOKbtn->SetPushedImage(_T("file='button_pushed.png' corner='5,5,5,5'"));  
  223.             pOKbtn->SetFixedWidth(60);  
  224.             pOKbtn->SetFixedHeight(30);  
  225.             pBtnHor->Add(pOKbtn);  
  226.         }  
  227.         break;  
  228.     case DUI_YESNO:  
  229.         {  
  230.             CButtonUI* pYesbtn = new CButtonUI;  
  231.             pYesbtn->SetName(_T("yesbtn"));  
  232.             pYesbtn->SetText(_T("是(Y)"));  
  233.             pYesbtn->SetNormalImage(_T("file='button_normal.png' corner='5,5,5,5'"));  
  234.             pYesbtn->SetHotImage(_T("file='button_hover.png' corner='5,5,5,5'"));  
  235.             pYesbtn->SetPushedImage(_T("file='button_pushed.png' corner='5,5,5,5'"));  
  236.             pYesbtn->SetFixedWidth(60);  
  237.             pYesbtn->SetFixedHeight(30);  
  238.             pBtnHor->Add(pYesbtn);  
  239.   
  240.             CButtonUI* pNobtn = new CButtonUI;  
  241.             pNobtn->SetName(_T("nobtn"));  
  242.             pNobtn->SetText(_T("否(N)"));  
  243.             pNobtn->SetNormalImage(_T("file='button_normal.png' corner='5,5,5,5'"));  
  244.             pNobtn->SetHotImage(_T("file='button_hover.png' corner='5,5,5,5'"));  
  245.             pNobtn->SetPushedImage(_T("file='button_pushed.png' corner='5,5,5,5'"));  
  246.             pNobtn->SetFixedWidth(60);  
  247.             pNobtn->SetFixedHeight(30);  
  248.             pBtnHor->Add(pNobtn);  
  249.         }  
  250.         break;  
  251.     default:  
  252.         break;  
  253.     }  
  254.       
  255.       
  256.     pRoot->Add(pBtnHor);  
  257.     pBtnHor->Add(new CControlUI);  
  258.   
  259.   
  260.   
  261.     m_PaintManager.AttachDialog(pRoot);  
  262.     m_PaintManager.AddNotifier(this);  
  263.     //m_PaintManager.SetBackgroundTransparent(TRUE);  
  264.   
  265.     //添加阴影  
  266.     CWndShadow::Initialize(m_PaintManager.GetInstance());  
  267.     m_WndShadow.Create(m_hWnd);  
  268.     m_WndShadow.SetSize(4);  
  269.     m_WndShadow.SetPosition(1, 1);  
  270.       
  271.   
  272.     return 0;  
  273. }  
  274.   
  275.   
  276.   
  277.   
  278. //  
  279.   
  280. Dui_MessageBox_Return DuiMessageBox( HWND hWnd, LPCTSTR lpText, LPCTSTR lpTitle/*=NULL*/, Dui_MessageBox_Type nType/*=0*/ )  
  281. {  
  282.       
  283.     Dui_MessageBox_Return nReturnId = DUI_OK;  
  284.     if(NULL == lpTitle)  
  285.     {  
  286.         lpTitle = _T("提示");  
  287.     }  
  288.     CDuiMessageBox* pMsgBox = new CDuiMessageBox(lpTitle, lpText, nType, &nReturnId);  
  289.     if( pMsgBox == NULL )  
  290.         return DUI_OK;  
  291.     pMsgBox->Create(hWnd, _T("DuiMessageBox"), UI_WNDSTYLE_DIALOG, 0 , 0, 0, 0, 0);  
  292.     pMsgBox->CenterWindow();  
  293.     pMsgBox->ShowModal();  
  294.     return nReturnId;  
  295. }  
[cpp]  view plain copy
  1. </pre><pre name="code" class="cpp">  


 
源码下载: 
点击打开链接
                
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值