您的位置:首页 > 产品设计 > UI/UE

第一个duilib程序--HelloWorld

2012-11-19 23:00 387 查看
duilib是一个windows下的皮肤库,用win32写的。。。

先看个效果图吧:



这个图片里有源代码,右键保存下图标,把后缀改为zip,即可。由于skin目录下的图片不便上传,需要自己加入合适的图片。

要使用duilib库,必须先把库导入,代码如下:

#include "xxx\UIlib.h" //xxx为UIlib.h的路径
using namespace Duilib; //Duilib为库自定义的名字空间

#ifdef _DEBUG
#ifdef _UNICODE
#pragma comment(lib, "xxx\Duilib_ud.lib")
#else
#pragma comment(lib, "xxx\Duilib_d.lib")
#endif
#else
#ifdef _UNICODE
#progma comment(lib, "xxx\Duilib_u.lib")
#else
#progma comment(lib, "xxx\Duilib.lib")
#endif


使用duilib库的程序和win32程序一样也是从WinMain开始的。在WinMain函数中,一般是这样做的:

CPaintManagerUI::SetInstance(hInstance);//将程序实例与皮肤绘制管理器挂钩
CPaintMamagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath() + TEXT("skin"));//设置皮肤库的资源路径,资源有图片、xml文件等
CPaintManagerUI::SetResourceZip(/*路径*/);//皮肤库支持压缩文件,这里指定压缩文件路径
//new一个类,这个类继承自CWindowWnd类
//调用类的Create函数创建窗口,这里会发送WM_CREATE消息,而这个类一般会在HandleMessage函数中处理WM_CREATE消息
//创建完窗口后,可以调用该类的SetIcon(IDI_HW)函数来设置任务栏上显示的图标
//然后调用CPaintManagerUI::MessageLoop(),进入消息循环


在duilib中每个窗口均要定义一个CPaintManagerUI成员对象用来管理整个窗口的绘制。

duilib中的窗口均继承自CWindowWnd类,在CWindowWnd类中有虚函数HandleMessage来处理Windows消息(如WM_CREATE、WM_SIZE等)。另外,如果你的窗口想要响应鼠标的点击、编辑框内容改变等消息的话,可以把你的窗口类继承INotify接口,这样你的窗口上的一个按钮被点击了,可以在继承自INotify接口的Notify函数中进行处理。

在自己定义的窗口类中一般这样来处理HandleMessage:

LRESULT CHelloWorld::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;
default:
bHandled = FALSE; break;
}

if (bHandled) return lRes;
if (m_pm.MessageHandler(uMsg, wParam, lParam, lRes) != 0)//没有处理,这传送给窗口绘制管理器处理,Notify函数将会在这里的m_pm.MessageHandler函数中被调用
return lRes;
return CWindowWnd::HandleMessage(uMsg, wParam, lParam);//都不处理则有CWindowWnd处理
}


INotifyUI接口的Notify()由CPaintManagerUI::MessageHandler调用。继承INotifyUI接口的类对象会被加入到CPaintManagerUI的m_aNotifiers数组中,而要加入m_aNotifiers数组一般由窗口类自己在OnCreate函数调用CPaintManagerUI的静态方法AddNotifier将自己加入到m_aNotifiers中。

而在自己的窗口类的OnCreate函数中,通常调用m_pm.Init(m_hWnd)来把自己的窗口句柄与窗口绘制管理器挂接在一起,即用于向CPaintManagerUI提供窗口句柄及窗口上下文句柄。

LRESULT CHelloWorld::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
m_pm.Init(m_hWnd);    // 把自己的窗口句柄与窗口绘制管理器挂接在一起
CDialogBuilder builder;
CControlUI* pRoot = builder.Create(TEXT("HelloWorld.xml"), (UINT)0, NULL, &m_pm); // 根据xml中的配置创建控件
ASSERT(pRoot && "Failure to parse XML");
m_pm.AttachDialog(pRoot); // 把这些控件绘制到本窗口上
m_pm.AddNotifier(this);    // 把自己加入到CPaintManagerUI的m_aNotifiers数组中,用于处理Notify函数
Init();
return 0;

}


以上就是关于duilib程序的一个建立过程。

贴代码先:

// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//

#pragma once

#define WIN32_LEAN_AND_MEAN             // 从 Windows 头中排除极少使用的资料
// Windows 头文件:
#include <windows.h>
#include <objbase.h>

// TODO: 在此处引用程序需要的其他头文件
#include "..\..\DuiLib\UIlib.h"

using namespace DuiLib;

#ifdef _DEBUG
#   ifdef _UNICODE
#       pragma comment(lib, "..\\..\\bin\\DuiLib_ud.lib")
#   else
#       pragma comment(lib, "..\\..\\bin\\DuiLib_d.lib")
#   endif
#else
#   ifdef _UNICODE
#       pragma comment(lib, "..\\..\\bin\\DuiLib_u.lib")
#   else
#       pragma comment(lib, "..\\..\\bin\\DuiLib.lib")
#   endif
#endif


// stdafx.cpp : 只包括标准包含文件的源文件
// HelloWorld.pch 将作为预编译头
// stdafx.obj 将包含预编译类型信息

#include "stdafx.h"

// TODO: 在 STDAFX.H 中
// 引用任何所需的附加头文件,而不是在此文件中引用


// HelloWorld.h
#pragma once

class CHelloWorld: public CWindowWnd, public INotifyUI    // 继承自CWindowWnd和INotifyUI
{
//////////////////////////////////////////////////////////////////////////
// 构造函数及自定义函数
public:
CHelloWorld();
void Init();    // 界面控件一些初始化、比如某个按钮最开始是禁用状态就应该在这个时候处理...
void OnOK();    // 点击OK按钮的处理
void OnClose();    // 点击Close按钮的处理

// WM_CREATE消息的处理
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
// WM_NCHITTEST消息的处理
LRESULT OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnGetMinMaxInfo(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);

//////////////////////////////////////////////////////////////////////////
// 继承自CWindowWnd类
public:
LPCTSTR GetWindowClassName() const ;    // 纯虚函数,必须有实现
UINT GetClassStyle() const;
void OnFinalMessage(HWND /*hWnd*/);    // 窗口接收到最后一条消息的处理
LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);    // 消息响应函数

//////////////////////////////////////////////////////////////////////////

// 继承自INotifyUI接口
public:
void Notify(TNotifyUI& msg);

//////////////////////////////////////////////////////////////////////////

// 成员变量
//////////////////////////////////////////////////////////////////////////
private:
CButtonUI*        m_pBtnOK;    // 按钮控件
CButtonUI*        m_pBtnClose;// 按钮控件

CPaintManagerUI m_pm;    // 窗口绘制器
};


// HelloWorld.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "HelloWorld.h"
#include "resource.h"

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow)
{
CPaintManagerUI::SetInstance(hInstance);    // 将程序实例与皮肤绘制管理器挂钩
// 设置皮肤库的资源路径,资源有图片、xml文件等
CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath() + TEXT("skin"));

HRESULT Hr = ::CoInitialize(NULL);
if( FAILED(Hr) ) return 0;

CHelloWorld* pHW = new CHelloWorld();
if (pHW == NULL)
return 0;
pHW->Create(NULL, TEXT("Hello World"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE | WS_EX_APPWINDOW);
pHW->SetIcon(IDI_HW);
pHW->CenterWindow();
pHW->ShowWindow();
CPaintManagerUI::MessageLoop();

::CoUninitialize();
return 0;
}

CHelloWorld::CHelloWorld()
: m_pBtnOK(NULL)
, m_pBtnClose(NULL)
{

}

void CHelloWorld::Init()    // 本例中此函数其实没有做任何事情
{
m_pBtnOK = static_cast<CButtonUI *>(m_pm.FindControl(TEXT("OK")));
if (m_pBtnOK == NULL)
return;
// m_pBtnOK->OnNotify += MakeDelegate(this, &CHelloWorld::OnOK);
// MakeDelegate的实现感觉很强大,这样也可以实现把OK按钮的时间转接到OnOK来处理

m_pBtnClose = static_cast<CButtonUI *>(m_pm.FindControl(TEXT("Close")));
if (m_pBtnClose == NULL)
return;
//m_pBtnClose->OnNotify += MakeDelegate(this, &CHelloWorld::OnClose);
}

void CHelloWorld::OnOK()
{
Close();    // CWindowWnd继承来的函数Close,是关闭自己
}

void CHelloWorld::OnClose()
{
Close();
}

LPCTSTR CHelloWorld::GetWindowClassName() const
{
return TEXT("HelloWorld");
}

UINT CHelloWorld::GetClassStyle() const
{
return UI_CLASSSTYLE_FRAME | CS_DBLCLKS;
}

void CHelloWorld::OnFinalMessage( HWND /*hWnd*/ )
{
delete this;
}

LRESULT CHelloWorld::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_DESTROY:
::PostQuitMessage(0);
bHandled = FALSE;
break;

case WM_NCHITTEST:
lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled);
break;

case WM_KEYDOWN:
if (wParam == VK_ESCAPE)
{
OnClose();
}
break;

case WM_SIZE:
lRes = OnSize(uMsg, wParam, lParam, bHandled);
break;

case WM_NCACTIVATE:
lRes = OnNcActivate(uMsg, wParam, lParam, bHandled);
break;

case WM_GETMINMAXINFO:
lRes = true;
OnGetMinMaxInfo(uMsg, wParam, lParam, bHandled);
break;

case WM_SYSCOMMAND:
lRes = OnSysCommand(uMsg, wParam, lParam, bHandled);
break;

case WM_NCCALCSIZE:
lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled);
break;

default:
bHandled = FALSE; break;
}

if (bHandled) return lRes;
if (m_pm.MessageHandler(uMsg, wParam, lParam, lRes) != 0)
return lRes;
return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
}

LRESULT CHelloWorld::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);
//RECT rcClient;
//::GetClientRect(*this, &rcClient);
//::SetWindowPos(*this, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, SWP_FRAMECHANGED);

m_pm.Init(m_hWnd);    // 把自己的窗口句柄与窗口绘制管理器挂接在一起
CDialogBuilder builder;
// 根据xml的配置来创建控件
CControlUI* pRoot = builder.Create(TEXT("HelloWorld.xml"), (UINT)0, NULL, &m_pm);
ASSERT(pRoot && "Failure to parse XML");
m_pm.AttachDialog(pRoot);    // 把上面的控件绘制到本窗口上,之前有把m_hWnd传给m_pm
m_pm.AddNotifier(this);    // 把自己加入到CPaintManagerUI的m_aNotifiers数组中,用于处理Notify函数
Init();
return 0;

}

LRESULT CHelloWorld::OnSize( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
{
SIZE szRoundCorner = m_pm.GetRoundCorner();    // GetRoundCorner用来获取xml中的Window标签中roundcorner属性值,该值指示圆角的长宽
if( !::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0) ) {
CRect rcWnd;
::GetWindowRect(*this, &rcWnd);
rcWnd.Offset(-rcWnd.left, -rcWnd.top);    // rcWnd.right就成为了窗口的宽度了
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);
}

bHandled = FALSE;
return 0;
}

LRESULT CHelloWorld::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);

if( !::IsZoomed(*this) ) {
RECT rcSizeBox = m_pm.GetSizeBox();    // GetSizeBox用来获取xml中Window标签的sizebox属性,该属性指示你的鼠标移动到窗口边框多少个像素会变成指示符(这个指示符表示可以改变窗口大小的指示符)
if( pt.y < rcClient.top + rcSizeBox.top ) {
if( pt.x < rcClient.left + rcSizeBox.left ) return HTTOPLEFT;
if( pt.x > rcClient.right - rcSizeBox.right ) return HTTOPRIGHT;
return HTTOP;
}
else if( pt.y > rcClient.bottom - rcSizeBox.bottom ) {
if( pt.x < rcClient.left + rcSizeBox.left ) return HTBOTTOMLEFT;
if( pt.x > rcClient.right - rcSizeBox.right ) return HTBOTTOMRIGHT;
return HTBOTTOM;
}
if( pt.x < rcClient.left + rcSizeBox.left ) return HTLEFT;
if( pt.x > rcClient.right - rcSizeBox.right ) return HTRIGHT;
}

RECT rcCaption = m_pm.GetCaptionRect();    // GetCaptionRect用来获取xml中Window标签的caption属性,该属性指示标题栏的大小
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_pm.FindControl(pt));
if( pControl && _tcsicmp(pControl->GetClass(), _T("ButtonUI")) != 0 && _tcsicmp(pControl->GetClass(), _T("OptionUI")) != 0)
return HTCAPTION;
}

return HTCLIENT;
}

void CHelloWorld::Notify( TNotifyUI& msg )
{
if (msg.sType == TEXT("click"))    // click事件
{
if (msg.pSender->GetName() == TEXT("OK"))
OnOK();
if (msg.pSender->GetName() == TEXT("Close"))
OnClose();
}
else if (msg.sType == TEXT("windowinit"))
{
}
}

LRESULT CHelloWorld::OnNcActivate( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
{
if( ::IsIconic(*this) ) bHandled = FALSE;
return (wParam == 0) ? TRUE : FALSE;
}

LRESULT CHelloWorld::OnGetMinMaxInfo( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
{
MONITORINFO oMonitor = {};
oMonitor.cbSize = sizeof(oMonitor);
::GetMonitorInfo(::MonitorFromWindow(*this, MONITOR_DEFAULTTOPRIMARY), &oMonitor);
CRect rcWork = oMonitor.rcWork;
rcWork.Offset(-rcWork.left, -rcWork.top);

LPMINMAXINFO lpMMI = (LPMINMAXINFO) lParam;
lpMMI->ptMaxPosition.x    = rcWork.left;
lpMMI->ptMaxPosition.y    = rcWork.top;
lpMMI->ptMaxSize.x        = rcWork.right;
lpMMI->ptMaxSize.y        = rcWork.bottom;

bHandled = FALSE;
return 0;
}

LRESULT CHelloWorld::OnSysCommand( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
{
if (wParam == SC_CLOSE)
{
bHandled = TRUE;
SendMessage(WM_CLOSE);
return 0;
}
#if defined(WIN32) && !defined(UNDER_CE)
BOOL bZoomed = ::IsZoomed(*this);
LRESULT lRes = CWindowWnd::HandleMessage(uMsg, wParam, lParam);
if( ::IsZoomed(*this) != bZoomed )
{
}
#else
LRESULT lRes = CWindowWnd::HandleMessage(uMsg, wParam, lParam);
#endif
return lRes;
}

LRESULT CHelloWorld::OnNcCalcSize( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
{
return 0;
// wParam为TRUE时,返回0将会使窗口的大小变为客户区的大小,也就是说这将把窗口的标题栏、窗口边框移除,只显示客户区
}


// xml皮肤配置文件
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<Window size="400,250" caption="0,0,0,30" roundcorner="6,6" sizebox="4,4,4,4" mininfo="100,50" maxinfo="500,300">
<VerticalLayout  bkcolor="#FF000000">
<HorizontalLayout width="400" height="30" bkimage="file='top_bg.png' corner='5,5,5,5'">
<HorizontalLayout widht="80">
<Button name="OK" text="OK" align="center" width="80" height="30" textcolor="#FFC8C8C8" disabletextcolor="#FFA7A6AA" normalimage="file='BtnOK.png' source='0,0,80,30'" hotimage="file='BtnOK.png' source='80,0,160,30'" pushedimage="file='BtnOK.png' source='160,0,240,30'" disabledimage="file='BtnOK.png' source='240,0,320,30'" />
</HorizontalLayout>
<HorizontalLayout width="240">
<Label name="caption" text="Hello World" textcolor="#FFFFFFFF" align="center" />
</HorizontalLayout>
<HorizontalLayout widht="80">
<Button name="Close" text="Close" align="center" width="79" height="30" textcolor="#FFC8C8C8" disabletextcolor="#FFA7A6AA" normalimage="file='BtnClose.png' source='0,0,79,30'" hotimage="file='BtnClose.png' source='80,0,158,30'" pushedimage="file='BtnClose.png' source='159,0,237,30'" disabledimage="file='BtnClose.png' source='238,0,316,30'" />
</HorizontalLayout>
</HorizontalLayout>
<VerticalLayout bkimage="file='MBbottombg.png' corner='6,6,6,6'">

</VerticalLayout>
</VerticalLayout>
</Window>


其他:

1.开始写完程序运行后发现任务栏上没有图标,原来可以用SetIcon函数来设置,SetIcon函数的参数是一个资源ID(UINT类型),可以自己绘制一个图标或者导入一个,然后将该图标的id传入即可,还是比较方便的。注意的是SetIcon不需要使用MAKEINTRESOURCE来转换,在SetIcon函数内调用了MAKEINTRESOURCE。

2.在用窗口类的Create函数时,指定UI_WNDSTYLE_FRAME为第三个参数时,窗口可以双击最大化,而使用UI_WNDSTYLE_DIALOG时无法最大化。原来

#define UI_WNDSTYLE_FRAME      (WS_VISIBLE | WS_OVERLAPPEDWINDOW)
#define UI_WNDSTYLE_CHILD      (WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN)
#define UI_WNDSTYLE_DIALOG     (WS_VISIBLE | WS_POPUPWINDOW | WS_CAPTION | WS_DLGFRAME | WS_CLIPSIBLINGS | WS_CLIPCHILDREN)


WS_OVERLAPPEDWINDOW默认带有WM_MAXIMIZEBOX属性,固默认可以双击最大化。WS_POPUPWINDOW默认有WS_BORDER,WS_POPUP和WS_SYSMENU属性,没有WM_MAXIMIZEBOX属性,固POPUPWINDOW默认无法双击最大化。

3.WM_NCCALCSIZE消息的处理,MSDN中有这么一段描述


When wParam is TRUE, simply returning 0 without processing the NCCALCSIZE_PARAMS rectangles will cause the client area to resize to the size of the window, including the window frame. This will remove the window frame and caption items from your window, leaving only the client area displayed.


也就是说这个消息处理时返回0,则窗口将会没有标题栏和外边框,只有客户区了。

4.WM_NCHITTEST消息的处理,可以返回HTTOPLEFT(窗口的左上角,鼠标变为可调边框标识)、HTTOPRIGHT等。返回HTCAPTION标识击中为标题栏,返回HTCLIENT标识击中为客户区。

5.按键消息的处理:可继承IMessageFilterUI,该接口有MessageHandler纯虚函数,自己要实现对按键的处理;在OnCreate中把自己加入到绘制管理器对象的m_aPreMessageFilters预处理消息数组中。这样在绘制管理器的MessageLoop()中将调用TranslateMessage按键处理,在TranslateMessage又将调用PreMessageHandler,然后PreMessageHandler中又将调用MessageHandler函数(即那个需要处理按键消息的窗口类的函数)

6.foreimage可以在按钮的背景上加入前景图片;控件的float设为true时,改变窗口大小,控件的显示会有问题,只能显示控件的一部分,解决办法是使用布局来控制控件位置,不用设置控件位置;relativePos要与float="true"同用,可调节百分比;pos属性也要与float="true"同用才可调节控件位置;皮肤科刷新有问题,必须依赖于背景颜色;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: