
相較於win32/mfc,directui(win32)更加輕量化,在開發方式上更加現代化,使得其在c++應用方面成為主流,官方中提到了可以開發控件(插件),也就是說新的庫可以不寫在dui里,而獨立出來。研究了許久終於知道如何玩,故此總結一下:
一、創建Dui DLL控件庫
使用vc或vs創建一個win32 dll類型的項目,頭文件PriceTableUI.h如下:
#pragma once class DuiLib_API PriceTableUI : public CContainerUI//注意要導出類
{ public: PriceTableUI(); ~PriceTableUI(); LPCTSTR GetClass() const; LPVOID GetInterface(LPCTSTR pstrName); void DoEvent(TEventUI& event); void PaintText(HDC hDC); };
對應的代碼PriceTableUI.cpp如下:
#include "StdAfx.h"
#include "PriceTableUI.h"
PriceTableUI::PriceTableUI()
{
}
PriceTableUI::~PriceTableUI()
{
}
LPCTSTR PriceTableUI::GetClass() const
{
return _T("PriceTableUI");
}
LPVOID PriceTableUI::GetInterface( LPCTSTR pstrName )
{
if( _tcscmp(pstrName, _T("PriceTable")) == 0 )
return static_cast<PriceTableUI*>(this);
return CControlUI::GetInterface(pstrName);
}
void PriceTableUI::DoEvent( TEventUI& event )
{
if( event.Type == UIEVENT_SETFOCUS )
{
m_bFocused = true;
return;
}
if( event.Type == UIEVENT_KILLFOCUS )
{
m_bFocused = false;
return;
}
if( event.Type == UIEVENT_MOUSEENTER )//鼠標進入控件
{
return;
}
if( event.Type == UIEVENT_MOUSELEAVE )//鼠標離開控件
{
return;
}
//其它事件(消息)go to 到dui里自己看了
CControlUI::DoEvent(event);
}
void PriceTableUI::PaintText( HDC hDC )
{
RECT rect = m_rcPaint;
HBRUSH redHBrush = CreateSolidBrush(RGB(255,0,0));
FillRect(hDC,&rect,redHBrush);
TextOut(hDC,rect.left,rect.top,"china mobile",strlen("china mobile"));
}
為了簡化,這個示例沒有對基類函數進一步實現,有需求自行去dui庫里復制過來或者自己擴展即可。
二、在主程序中定向自定義控件
ControlEx.h如下:
#pragma once
#include "stdafx.h"
class CDialogBuilderCallbackEx : public IDialogBuilderCallback
{
public:
CControlUI* CreateControl(LPCTSTR pstrClass)
{
if( _tcscmp(pstrClass, "PriceTable") == 0 )
return new PriceTableUI;
return NULL;
}
};
三、調用控件
如此,即可在dui的xml界面文件里使用<PriceTable />或在程序中調用,比如:
PriceTableUI* table=static_cast<PriceTableUI*>(m_pm.FindControl("table1"));
