引入duilib时请确保引入头文件开始时先引入COMUTIL.H头文件
duilib基本程序结构
在stdafx.h文件中加入
<span style="font-size:18px;">#include "COMUTIL.H" #include "UIlib.h" using namespace DuiLib;</span>
<span style="font-size:18px;">#ifndef LoginView_h__ #define LoginView_h__ class LoginView : public WindowImplBase// 只能放在最后,否则其消息路由出问题 { public: DUI_DECLARE_MESSAGE_MAP() public: LoginView(void); ~LoginView(void); void on_btn_click( DuiLib::TNotifyUI &msg ); virtual CDuiString GetSkinFolder() { return _T ("skin\\"); } virtual CDuiString GetSkinFile() { return _T("login.xml"); } virtual LPCTSTR GetWindowClassName( void ) const { return _T("LoginView"); } }; #endif // LoginView_h__</span>
<span style="font-size:18px;">#include "StdAfx.h" #include "LoginView.h" DUI_BEGIN_MESSAGE_MAP(LoginView, WindowImplBase) DUI_ON_MSGTYPE(DUI_MSGTYPE_CLICK,on_btn_click) DUI_END_MESSAGE_MAP() LoginView::LoginView(void) { } LoginView::~LoginView(void) { } void LoginView::on_btn_click( DuiLib::TNotifyUI &msg ) { }</span>
<span style="font-size:18px;">// chatme.cpp : 定义应用程序的入口点。 // #include "stdafx.h" #include "chatme.h" #include "LoginView.h" int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { CPaintManagerUI::SetInstance(hInstance); CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath()); HRESULT Hr = ::CoInitialize(NULL); if( FAILED(Hr) ) return 0; LoginView login_view; login_view.Create(NULL, _T("LoginView"), UI_WNDSTYLE_DIALOG, WS_EX_STATICEDGE | WS_EX_APPWINDOW); login_view.CenterWindow(); CPaintManagerUI::MessageLoop(); ::CoUninitialize(); return 0; }</span>
答:设置nativebkcolor属性。
问:如何设置按钮的鼠标悬浮时的字体颜色?
答:设置按钮的hottextcolor属性,相对的还有pushedtextcolor和focustextcolor.
问:如何设置按钮按下时字体的颜色?
答:设置按钮的pushedtextcolor属性,相对的还有hottextcolor和focustextcolor.
#pragma once #define DUI_MFCCTRL_COMMAND_MSG _T("MFC_CTRL_NOTIFY_MSG") #define DUI_MFCCTRL_NOTIFY_MSG _T("MFC_CTRL_COMMAND_MSG") // 封装MFC控件到DUI控件中,实现duilib中嵌入MFC控件 // 通过维护一个HWND实现 class CDUIMFCCtrlWrapper : public DuiLib::CControlUI, public DuiLib::IMessageFilterUI { public: CDUIMFCCtrlWrapper(void) : m_hWnd(NULL), m_bAddedMessageFilter(FALSE){} ~CDUIMFCCtrlWrapper(void){} // 绑定 BOOL Attach(HWND hWndNew); HWND Detach(); protected: // 控制显示 virtual void SetInternVisible(bool bVisible = true); // 控制位置 virtual void SetPos(RECT rc); // 对控件消息的分派,例如对发送给如对命令消息和通知消息进行分派 // 通过SendNotify实现,可以在OnNotify中进行响应。 virtual LRESULT MessageHandler( UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled ); virtual void SetManager( DuiLib::CPaintManagerUI* pManager, DuiLib::CControlUI* pParent, bool bInit = true ); protected: HWND m_hWnd; BOOL m_bAddedMessageFilter; // 防止重复设置消息监听 };
#include "StdAfx.h" #include "DUIMFCCtrlWrapper.h" using namespace DuiLib; void CDUIMFCCtrlWrapper::SetManager( CPaintManagerUI* pManager, CControlUI* pParent, bool bInit /*= true */ ) { if (pManager && !m_bAddedMessageFilter) { m_bAddedMessageFilter = TRUE; // 设置本控件封装能接收到消息,从而进行分派 pManager->AddMessageFilter(this); } CControlUI::SetManager(pManager, pParent, false); } LRESULT CDUIMFCCtrlWrapper::MessageHandler( UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled ) { bHandled = TRUE; // 如果是命令消息和通知消息等MFC控件消息则不用再传递下去 if (uMsg == WM_NOTIFY) { NMHDR* pNMHDR = (NMHDR*)lParam; HWND hWndCtrl = pNMHDR->hwndFrom; UINT nId = LOWORD(wParam); int nCode = pNMHDR->code; ASSERT(NULL != hWndCtrl); ASSERT(::IsWindow(hWndCtrl)); typedef struct _CtrlNotifyStruct { NMHDR* pNMHDR; int nCode; }CtrlNotifyStruct; CtrlNotifyStruct ns; ns.pNMHDR = pNMHDR; ns.nCode = nCode; m_pManager->SendNotify(this, DUI_MFCCTRL_COMMAND_MSG, WPARAM(nId), LPARAM(&ns)); } else if (WM_COMMAND == uMsg) { UINT nID = LOWORD(wParam); HWND hWndCtrl = (HWND)lParam; int nCode = HIWORD(wParam); m_pManager->SendNotify(this, DUI_MFCCTRL_NOTIFY_MSG, nID, nCode); } //else if(XTPWM_PROPERTYGRID_NOTIFY == uMsg) //{ // m_pManager->SendNotify(this, DUI_MFCCTRL_NOTIFY_MSG, wParam, lParam); //} else { // 否则该消息需要继续传递下去 bHandled = FALSE; return 1; } return 0; } void CDUIMFCCtrlWrapper::SetPos( RECT rc ) { __super::SetPos(rc); ::SetWindowPos(m_hWnd, NULL, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER | SWP_NOACTIVATE); } void CDUIMFCCtrlWrapper::SetInternVisible( bool bVisible /*= true*/ ) { __super::SetInternVisible(bVisible); ::ShowWindow(m_hWnd, bVisible); } HWND CDUIMFCCtrlWrapper::Detach() { HWND hWnd = m_hWnd; m_hWnd = NULL; return hWnd; } BOOL CDUIMFCCtrlWrapper::Attach( HWND hWndNew ) { if (! ::IsWindow(hWndNew)) { return FALSE; } m_hWnd = hWndNew; return TRUE; }
#include "stdafx.h" #include "DUIMFCCtrlWrapper.h" #include "Mycug.h" using namespace DuiLib; class CFrameWindowWnd : public WindowImplBase { DUI_DECLARE_MESSAGE_MAP() public: protected: virtual CDuiString GetSkinFolder() { return CPaintManagerUI::GetInstancePath(); } virtual CDuiString GetSkinFile() { return _T("test.xml"); } virtual LPCTSTR GetWindowClassName( void ) const { return _T("CFrameWindowWnd"); } virtual CControlUI* CreateControl( LPCTSTR pstrClass ) { if (_tcscmp(pstrClass, _T("MfcCtrl")) == 0) { return new CDUIMFCCtrlWrapper(); } return NULL; } void OnClick(TNotifyUI& msg) { CDuiString sCtrlName = msg.pSender->GetName(); if( sCtrlName == _T("closebtn") ) { PostMessage(WM_QUIT, 0, 0); return; } else if( sCtrlName == _T("minbtn")) { SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0); return; } else if( sCtrlName == _T("maxbtn")) { SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0); return; } else if( sCtrlName == _T("restorebtn")) { SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0); return; } } virtual void InitWindow() { CDUIMFCCtrlWrapper* pCtrl = (CDUIMFCCtrlWrapper*)m_PaintManager.FindControl(_T("grid")); if (pCtrl) { m_GridCtrl.CreateGrid(WS_CHILD|WS_VISIBLE,CRect(0,0,0,0),CWnd::FromHandle(GetHWND()),1234); pCtrl->Attach(m_GridCtrl.GetSafeHwnd()); } } MyCug m_GridCtrl; };
DUI_BEGIN_MESSAGE_MAP(CFrameWindowWnd, WindowImplBase) DUI_ON_MSGTYPE(DUI_MSGTYPE_CLICK,OnClick) DUI_END_MESSAGE_MAP() int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow) { // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: MFC initialization failed\n")); return 1; } CPaintManagerUI::SetInstance(hInstance); CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath()); HRESULT Hr = ::CoInitialize(NULL); if( FAILED(Hr) ) return 0; CFrameWindowWnd* pFrame = new CFrameWindowWnd(); if( pFrame == NULL ) return 0; pFrame->Create(NULL, _T("测试"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE); pFrame->CenterWindow(); pFrame->ShowWindow(true); CPaintManagerUI::MessageLoop(); ::CoUninitialize(); return 0; }
// hDc HDC句柄 // CPaintManagerUI句柄 // rc 目标画布的大小 // rcPaint 绘制区域 // pStrImage 为源图像的名称,不需提供路径,函数内部会自动加上资源位置的路径,路径为CPaintManagerUI::SetResourcePath设置的路径。 // pStrModify 设置绘制属性,含义为: // 2、file='aaa.jpg' res='' restype='0' dest='0,0,0,0' source='0,0,0,0' corner='0,0,0,0' // mask='#FF0000' fade='255' hole='false' xtiled='false' ytiled='false' // source和dest表示从源图像的source区域贴到目标图像的dest区域,mask表示让某颜色为透明色,例如mask="#FF000000",设置黑色为透明色。 // xtiled,ytiled 设置为true表示横向和纵向的图像不拉伸显示而是平铺显示 // hole 为true表示不绘制中间部分,为某些场合提高性能 bool CRenderEngine::DrawImageString(HDC hDC, CPaintManagerUI* pManager, const RECT& rc, const RECT& rcPaint, LPCTSTR pStrImage, LPCTSTR pStrModify) { if ((pManager == NULL) || (hDC == NULL)) return false; // 1、aaa.jpg // 2、file='aaa.jpg' res='' restype='0' dest='0,0,0,0' source='0,0,0,0' corner='0,0,0,0' // mask='#FF0000' fade='255' hole='false' xtiled='false' ytiled='false' } void CRenderEngine::DrawImage(HDC hDC, HBITMAP hBitmap, const RECT& rc, const RECT& rcPaint, const RECT& rcBmpPart, const RECT& rcCorners, bool alphaChannel, BYTE uFade, bool hole, bool xtiled, bool ytiled)
PaintBkColor(hDC); // 绘制背景颜色 PaintBkImage(hDC); // 绘制背景图 PaintStatusImage(hDC); // 绘制状态图 PaintText(hDC); // 绘制文本 PaintBorder(hDC); // 绘制边框