直接发源码!!
.h
- #ifndef DUI_MESSAGEBOX_HPP
- #define DUI_MESSAGEBOX_HPP
- #include "StdAfx.h"
- #include "WndShadow.h"
- enum Dui_MessageBox_Type
- {
- DUI_DEFAULT=0,
- DUI_YESNO
- };
- enum Dui_MessageBox_Return
- {
- DUI_OK=0,
- DUI_CANCEL,
- DUI_YES,
- DUI_NO
- };
- class CDuiMessageBox : public CWindowWnd, public INotifyUI, public CNotifyPump
- {
- public:
- CDuiMessageBox(LPCTSTR lpTitle,
- LPCTSTR lpMsg,
- Dui_MessageBox_Type nType,
- Dui_MessageBox_Return* nReturn);
- LPCTSTR GetWindowClassName() const ;
- virtual UINT GetClassStyle() const;
- virtual void OnFinalMessage( HWND hWnd );
- virtual void Notify(TNotifyUI& msg);
- LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
- LRESULT OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
- LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
- LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
- LRESULT ResponseDefaultKeyEvent(WPARAM wParam);
- DUI_DECLARE_MESSAGE_MAP()
- public:
- void OnClick(TNotifyUI& msg);
- void OnPrepare(TNotifyUI& msg);
- private:
- CPaintManagerUI m_PaintManager;
- CWndShadow m_WndShadow;//窗口阴影
- CString m_szTitle;
- CString m_szMsg;
- Dui_MessageBox_Type m_nType;
- Dui_MessageBox_Return* m_nReturn ;
- };
- #endif // DUI_MESSAGEBOX_HPP
- Dui_MessageBox_Return DuiMessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpTitle=NULL, Dui_MessageBox_Type nType=DUI_DEFAULT);
- </pre><pre name="code" class="cpp">.cpp
- <pre name="code" class="cpp">#include "DuiMessageBox.h"
- /
- DUI_BEGIN_MESSAGE_MAP(CDuiMessageBox, CNotifyPump)
- DUI_ON_MSGTYPE(DUI_MSGTYPE_WINDOWINIT, OnPrepare)
- DUI_ON_MSGTYPE(DUI_MSGTYPE_CLICK, OnClick)
- DUI_END_MESSAGE_MAP()
- CDuiMessageBox::CDuiMessageBox( LPCTSTR lpTitle,
- LPCTSTR lpMsg,
- Dui_MessageBox_Type nType,
- Dui_MessageBox_Return* nReturn)
- {
- m_szTitle = lpTitle;
- m_szMsg = lpMsg;
- m_nType = nType;
- m_nReturn = nReturn;
- }
- void CDuiMessageBox::OnFinalMessage( HWND hWnd )
- {
- m_PaintManager.RemoveNotifier(this);
- m_PaintManager.ReapObjects(m_PaintManager.GetRoot());
- delete this;
- }
- void CDuiMessageBox::Notify( TNotifyUI& msg )
- {
- return CNotifyPump::NotifyPump(msg);
- }
- void CDuiMessageBox::OnClick( TNotifyUI& msg )
- {
- CDuiString sCtrlName = msg.pSender->GetName();
- if( sCtrlName == _T("okbtn") )
- {
- *m_nReturn = DUI_OK;
- }
- else if( sCtrlName == _T("cancelbtn"))
- {
- *m_nReturn = DUI_CANCEL;
- }
- else if( sCtrlName == _T("yesbtn"))
- {
- *m_nReturn = DUI_YES;
- }
- else if( sCtrlName == _T("nobtn"))
- {
- *m_nReturn = DUI_NO;
- }
- Close();
- return;
- }
- LPCTSTR CDuiMessageBox::GetWindowClassName( void ) const
- {
- return _T("DuiMessageBox");
- }
- UINT CDuiMessageBox::GetClassStyle() const
- {
- return CS_DBLCLKS;
- }
- void CDuiMessageBox::OnPrepare( TNotifyUI& msg )
- {
- }
- LRESULT CDuiMessageBox::OnSize( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
- {
- SIZE szRoundCorner = m_PaintManager.GetRoundCorner();
- if( !::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0) )
- {
- CDuiRect rcWnd;
- ::GetWindowRect(*this, &rcWnd);
- rcWnd.Offset(-rcWnd.left, -rcWnd.top);
- rcWnd.right++; rcWnd.bottom++;
- HRGN hRgn = ::CreateRoundRectRgn(rcWnd.left, rcWnd.top, rcWnd.right, rcWnd.bottom, szRoundCorner.cx, szRoundCorner.cy);
- ::SetWindowRgn(*this, hRgn, TRUE);
- ::DeleteObject(hRgn);
- /*CTextUI* pMsgText = static_cast<CTextUI*>(m_PaintManager.FindControl(_T("msg_text")));
- if (NULL != pMsgText)
- {
- int x = pMsgText->GetX();
- int y = pMsgText->GetY();
- int ynum = y + 70;
- }*/
- }
- bHandled = FALSE;
- return 0;
- }
- LRESULT CDuiMessageBox::OnNcHitTest( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
- {
- POINT pt; pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam);
- ::ScreenToClient(*this, &pt);
- RECT rcClient;
- ::GetClientRect(*this, &rcClient);
- RECT rcCaption = m_PaintManager.GetCaptionRect();
- if( pt.x >= rcClient.left + rcCaption.left && pt.x < rcClient.right - rcCaption.right \
- && pt.y >= rcCaption.top && pt.y < rcCaption.bottom )
- {
- CControlUI* pControl = static_cast<CControlUI*>(m_PaintManager.FindControl(pt));
- if( pControl && _tcscmp(pControl->GetClass(), _T("ButtonUI")) != 0 &&
- _tcscmp(pControl->GetClass(), _T("OptionUI")) != 0 )
- return HTCAPTION;
- }
- return HTCLIENT;
- }
- LRESULT CDuiMessageBox::HandleMessage( UINT uMsg, WPARAM wParam, LPARAM lParam )
- {
- LRESULT lRes = 0;
- BOOL bHandled = TRUE;
- switch (uMsg)
- {
- case WM_CREATE: lRes = OnCreate(uMsg, wParam, lParam, bHandled); break;
- //case WM_NCACTIVATE: lRes = OnNcActivate(uMsg, wParam, lParam, bHandled); break;
- //case WM_NCCALCSIZE: lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); break;
- case WM_NCPAINT: lRes = 0; break;
- case WM_NCHITTEST: lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled); break;
- case WM_GETMINMAXINFO: lRes = 0;bHandled = FALSE; break;
- case WM_SIZE: lRes = OnSize(uMsg, wParam, lParam, bHandled); break;
- default: bHandled = FALSE; break;
- }
- if (bHandled) return lRes;
- if (m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes))
- return lRes;
- return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
- }
- #include <math.h>
- LRESULT CDuiMessageBox::OnCreate( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
- {
- LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
- styleValue &= ~WS_CAPTION;
- ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
- m_PaintManager.Init(m_hWnd);
- /*
- SIZE size = {0,0};
- HDC hDC = m_PaintManager.GetPaintDC();
- ::SetBkMode(hDC, TRANSPARENT);
- HFONT hOldFont = (HFONT)::SelectObject(hDC, m_PaintManager.GetFont(12));
- ::GetTextExtentPoint32(hDC, m_szMsg, m_szMsg.GetLength(), &size);
- ::SelectObject(hDC, hOldFont);
- double nRow = sqrt(double(size.cx / size.cy / 10));*/
- int nLinks = 0;
- RECT rcText ={ 0, 0, 9999, 9999 };
- CRenderEngine::DrawHtmlText(m_PaintManager.GetPaintDC(), &m_PaintManager, rcText, m_szMsg, 0, NULL, NULL, nLinks, DT_CALCRECT | DT_WORDBREAK);
- int nClientX = rcText.right - rcText.left + 50;
- int nClientY = rcText.bottom - rcText.top + 100 ;
- if(nClientX < 240 ) nClientX = 240;
- if(nClientY < 130 ) nClientY = 130;
- m_PaintManager.SetInitSize(nClientX, nClientY);
- RECT rcCaption = {0, 0, 0, nClientY};
- m_PaintManager.SetCaptionRect(rcCaption);
- m_PaintManager.SetRoundCorner(2, 2);
- CVerticalLayoutUI* pRoot = new CVerticalLayoutUI;
- pRoot->SetBkColor(0xFFCFDDD0);
- RECT rcInset = {10, 2, 10, 2};
- pRoot->SetInset(rcInset);
- pRoot->SetBorderSize(2);
- pRoot->SetBorderColor(0xFF314E34);
- //提示标题
- CLabelUI* pTitleLabel = new CLabelUI;
- pTitleLabel->SetFixedHeight(30);
- pTitleLabel->SetAttribute(_T("align"), _T("center"));
- pTitleLabel->SetTextColor(0xFFA31515);
- pTitleLabel->SetText(m_szTitle);
- pRoot->Add(pTitleLabel);
- pRoot->Add(new CControlUI);
- //msg 正文
- CTextUI* pMsgLabel = new CTextUI;
- pMsgLabel->SetName(_T("msg_text"));
- pMsgLabel->SetText(m_szMsg);
- pMsgLabel->SetShowHtml(true);
- pMsgLabel->SetAttribute(_T("align"), _T("center"));
- //pMsgLabel->EstimateSize();
- pRoot->Add(pMsgLabel);
- pRoot->Add(new CControlUI);
- //类型 按钮个数和功能
- CHorizontalLayoutUI* pBtnHor = new CHorizontalLayoutUI;
- pBtnHor->SetFixedHeight(40);
- pBtnHor->Add(new CControlUI);
- //pTitleHor->SetBkColor(0xFF1777EF);
- //CControlUI* pControl = new CControlUI;
- //pTitleHor->Add(pControl);
- switch (m_nType)
- {
- case DUI_DEFAULT:
- {
- CButtonUI* pOKbtn = new CButtonUI;
- pOKbtn->SetName(_T("okbtn"));
- pOKbtn->SetText(_T("确定"));
- pOKbtn->SetNormalImage(_T("file='button_normal.png' corner='5,5,5,5'"));
- pOKbtn->SetHotImage(_T("file='button_hover.png' corner='5,5,5,5'"));
- pOKbtn->SetPushedImage(_T("file='button_pushed.png' corner='5,5,5,5'"));
- pOKbtn->SetFixedWidth(60);
- pOKbtn->SetFixedHeight(30);
- pBtnHor->Add(pOKbtn);
- }
- break;
- case DUI_YESNO:
- {
- CButtonUI* pYesbtn = new CButtonUI;
- pYesbtn->SetName(_T("yesbtn"));
- pYesbtn->SetText(_T("是(Y)"));
- pYesbtn->SetNormalImage(_T("file='button_normal.png' corner='5,5,5,5'"));
- pYesbtn->SetHotImage(_T("file='button_hover.png' corner='5,5,5,5'"));
- pYesbtn->SetPushedImage(_T("file='button_pushed.png' corner='5,5,5,5'"));
- pYesbtn->SetFixedWidth(60);
- pYesbtn->SetFixedHeight(30);
- pBtnHor->Add(pYesbtn);
- CButtonUI* pNobtn = new CButtonUI;
- pNobtn->SetName(_T("nobtn"));
- pNobtn->SetText(_T("否(N)"));
- pNobtn->SetNormalImage(_T("file='button_normal.png' corner='5,5,5,5'"));
- pNobtn->SetHotImage(_T("file='button_hover.png' corner='5,5,5,5'"));
- pNobtn->SetPushedImage(_T("file='button_pushed.png' corner='5,5,5,5'"));
- pNobtn->SetFixedWidth(60);
- pNobtn->SetFixedHeight(30);
- pBtnHor->Add(pNobtn);
- }
- break;
- default:
- break;
- }
- pRoot->Add(pBtnHor);
- pBtnHor->Add(new CControlUI);
- m_PaintManager.AttachDialog(pRoot);
- m_PaintManager.AddNotifier(this);
- //m_PaintManager.SetBackgroundTransparent(TRUE);
- //添加阴影
- CWndShadow::Initialize(m_PaintManager.GetInstance());
- m_WndShadow.Create(m_hWnd);
- m_WndShadow.SetSize(4);
- m_WndShadow.SetPosition(1, 1);
- return 0;
- }
- //
- Dui_MessageBox_Return DuiMessageBox( HWND hWnd, LPCTSTR lpText, LPCTSTR lpTitle/*=NULL*/, Dui_MessageBox_Type nType/*=0*/ )
- {
- Dui_MessageBox_Return nReturnId = DUI_OK;
- if(NULL == lpTitle)
- {
- lpTitle = _T("提示");
- }
- CDuiMessageBox* pMsgBox = new CDuiMessageBox(lpTitle, lpText, nType, &nReturnId);
- if( pMsgBox == NULL )
- return DUI_OK;
- pMsgBox->Create(hWnd, _T("DuiMessageBox"), UI_WNDSTYLE_DIALOG, 0 , 0, 0, 0, 0);
- pMsgBox->CenterWindow();
- pMsgBox->ShowModal();
- return nReturnId;
- }
- </pre><pre name="code" class="cpp">
源码下载: 点击打开链接