本文配套程序下载地址为: http://pan.baidu.com/share/link?shareid=358034&uk=3995556177
界面的xml配好后,就开始写实现代码。先上代码
class CMainFrameWnd : public CWindowWnd, public INotifyUI, public IMessageFilterUI
{
public:
CMainFrameWnd() { m_fixed = FALSE; m_errorcode = 0;};
LPCTSTR GetWindowClassName() const { return _T("UIMainFrame"); };
UINT GetClassStyle() const { return UI_CLASSSTYLE_DIALOG; };
void OnFinalMessage(HWND /*hWnd*/)
{
m_pm.RemovePreMessageFilter(this);
delete this;
}
void Init()
{
CContainerUI* pAccountCombo;
pAccountCombo = static_cast<CContainerUI*>(m_pm.FindControl(_T("ContainerUIStep2")));
pAccountCombo->SetVisible(FALSE);
pAccountCombo = static_cast<CContainerUI*>(m_pm.FindControl(_T("ContainerUIStep1")));
pAccountCombo->SetVisible(TRUE);
pAccountCombo = static_cast<CContainerUI*>(m_pm.FindControl(_T("ContainerUIStep3")));
pAccountCombo->SetVisible(FALSE);
}
void BtnClose_Click()
{
PostQuitMessage(0);
}
void BtnWin7_Click()
{
m_bOsType = WIN_WIN7;
CLabelUI* pAccountEdit = static_cast<CLabelUI*>(m_pm.FindControl(_T("LabelUIDescription")));
pAccountEdit->SetText(_T("正在下载 Win7 系统到您的计算机..."));
CContainerUI* pAccountCombo = static_cast<CContainerUI*>(m_pm.FindControl(_T("ContainerUIStep2")));
pAccountCombo->SetVisible(TRUE);
pAccountCombo = static_cast<CContainerUI*>(m_pm.FindControl(_T("ContainerUIStep1")));
pAccountCombo->SetVisible(FALSE);
CProgressUI* pProcess = static_cast<CProgressUI*>(m_pm.FindControl(_T("ProgressDownload")));
pProcess->SetValue(0);
CLabelUI* pWaitLabel = static_cast<CLabelUI*>(m_pm.FindControl(_T("LabelWaiting")));
pWaitLabel->SetText(_T("开始下载, 请耐心等待..."));
g_status = TO_GET_CONFIG; // start
::SetTimer(m_hWnd, TIMER_ID_CHECKDOWNLOADWND, 1000, NULL);
}
void Notify(TNotifyUI& msg)
{
if( msg.sType == _T("click") )
{
// 关闭按钮
if( msg.pSender->GetName() == _T("BtnClose") )
{
BtnClose_Click();
return;
}
else if ( msg.pSender->GetName() == _T("BtnXP"))
{
BtnXP_Click();
return;
}
else if (msg.pSender->GetName() == _T("BtnWin7") )
{
BtnWin7_Click();
return;
}
// 重启动完成安装
else if (msg.pSender->GetName() == _T("ButtonUIReboot"))
{
ButtonUIReboot_Click();
return;
}
}
else if( msg.sType == _T("itemselect") )
{
if( msg.pSender->GetName() == _T("accountcombo") )
{
CEditUI* pAccountEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("accountedit")));
if( pAccountEdit )
pAccountEdit->SetText(msg.pSender->GetText());
}
}
}
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
SetIcon(IDI_ICON_WINGIRL); // 加上以后运行的时候有图标
LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
styleValue &= ~WS_CAPTION;
::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
m_pm.Init(m_hWnd);
m_pm.AddPreMessageFilter(this);
CDialogBuilder builder;
CControlUI* pRoot;
if (IsWindowsVistaUp())
pRoot = builder.Create(_T("UIMainWin7.xml"), (UINT)0, NULL, &m_pm);
else
pRoot = builder.Create(_T("UIMainXP.xml"), (UINT)0, NULL, &m_pm);
ASSERT(pRoot && "Failed to parse XML");
m_pm.AttachDialog(pRoot);
m_pm.AddNotifier(this);
Init();
return 0;
}
LRESULT OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
if( ::IsIconic(*this) ) bHandled = FALSE;
return (wParam == 0) ? TRUE : FALSE;
}
LRESULT OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
return 0;
}
LRESULT OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
return 0;
}
LRESULT 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_pm.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_pm.FindControl(pt));
if (!m_fixed)
if( pControl && _tcscmp(pControl->GetClass(), _T("ButtonUI")) != 0 )
return HTCAPTION;
}
return HTCLIENT;
}
void ShowMessage(CString msg)
{
CLabelUI* pAccountEdit = static_cast<CLabelUI*>(m_pm.FindControl(_T("LabelWaiting")));
pAccountEdit->SetText(msg);
}
LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
SIZE szRoundCorner = m_pm.GetRoundCorner();
if( !::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0) )
{
CRect 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);
}
bHandled = FALSE;
return 0;
}
//
界面的xml配好后,就开始写实现代码。先上代码
class CMainFrameWnd : public CWindowWnd, public INotifyUI, public IMessageFilterUI
{
public:
CMainFrameWnd() { m_fixed = FALSE; m_errorcode = 0;};
LPCTSTR GetWindowClassName() const { return _T("UIMainFrame"); };
UINT GetClassStyle() const { return UI_CLASSSTYLE_DIALOG; };
void OnFinalMessage(HWND /*hWnd*/)
{
m_pm.RemovePreMessageFilter(this);
delete this;
}
void Init()
{
CContainerUI* pAccountCombo;
pAccountCombo = static_cast<CContainerUI*>(m_pm.FindControl(_T("ContainerUIStep2")));
pAccountCombo->SetVisible(FALSE);
pAccountCombo = static_cast<CContainerUI*>(m_pm.FindControl(_T("ContainerUIStep1")));
pAccountCombo->SetVisible(TRUE);
pAccountCombo = static_cast<CContainerUI*>(m_pm.FindControl(_T("ContainerUIStep3")));
pAccountCombo->SetVisible(FALSE);
}
void BtnClose_Click()
{
PostQuitMessage(0);
}
void BtnWin7_Click()
{
m_bOsType = WIN_WIN7;
CLabelUI* pAccountEdit = static_cast<CLabelUI*>(m_pm.FindControl(_T("LabelUIDescription")));
pAccountEdit->SetText(_T("正在下载 Win7 系统到您的计算机..."));
CContainerUI* pAccountCombo = static_cast<CContainerUI*>(m_pm.FindControl(_T("ContainerUIStep2")));
pAccountCombo->SetVisible(TRUE);
pAccountCombo = static_cast<CContainerUI*>(m_pm.FindControl(_T("ContainerUIStep1")));
pAccountCombo->SetVisible(FALSE);
CProgressUI* pProcess = static_cast<CProgressUI*>(m_pm.FindControl(_T("ProgressDownload")));
pProcess->SetValue(0);
CLabelUI* pWaitLabel = static_cast<CLabelUI*>(m_pm.FindControl(_T("LabelWaiting")));
pWaitLabel->SetText(_T("开始下载, 请耐心等待..."));
g_status = TO_GET_CONFIG; // start
::SetTimer(m_hWnd, TIMER_ID_CHECKDOWNLOADWND, 1000, NULL);
}
void Notify(TNotifyUI& msg)
{
if( msg.sType == _T("click") )
{
// 关闭按钮
if( msg.pSender->GetName() == _T("BtnClose") )
{
BtnClose_Click();
return;
}
else if ( msg.pSender->GetName() == _T("BtnXP"))
{
BtnXP_Click();
return;
}
else if (msg.pSender->GetName() == _T("BtnWin7") )
{
BtnWin7_Click();
return;
}
// 重启动完成安装
else if (msg.pSender->GetName() == _T("ButtonUIReboot"))
{
ButtonUIReboot_Click();
return;
}
}
else if( msg.sType == _T("itemselect") )
{
if( msg.pSender->GetName() == _T("accountcombo") )
{
CEditUI* pAccountEdit = static_cast<CEditUI*>(m_pm.FindControl(_T("accountedit")));
if( pAccountEdit )
pAccountEdit->SetText(msg.pSender->GetText());
}
}
}
LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
SetIcon(IDI_ICON_WINGIRL); // 加上以后运行的时候有图标
LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
styleValue &= ~WS_CAPTION;
::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);
m_pm.Init(m_hWnd);
m_pm.AddPreMessageFilter(this);
CDialogBuilder builder;
CControlUI* pRoot;
if (IsWindowsVistaUp())
pRoot = builder.Create(_T("UIMainWin7.xml"), (UINT)0, NULL, &m_pm);
else
pRoot = builder.Create(_T("UIMainXP.xml"), (UINT)0, NULL, &m_pm);
ASSERT(pRoot && "Failed to parse XML");
m_pm.AttachDialog(pRoot);
m_pm.AddNotifier(this);
Init();
return 0;
}
LRESULT OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
if( ::IsIconic(*this) ) bHandled = FALSE;
return (wParam == 0) ? TRUE : FALSE;
}
LRESULT OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
return 0;
}
LRESULT OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
return 0;
}
LRESULT 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_pm.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_pm.FindControl(pt));
if (!m_fixed)
if( pControl && _tcscmp(pControl->GetClass(), _T("ButtonUI")) != 0 )
return HTCAPTION;
}
return HTCLIENT;
}
void ShowMessage(CString msg)
{
CLabelUI* pAccountEdit = static_cast<CLabelUI*>(m_pm.FindControl(_T("LabelWaiting")));
pAccountEdit->SetText(msg);
}
LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
SIZE szRoundCorner = m_pm.GetRoundCorner();
if( !::IsIconic(*this) && (szRoundCorner.cx != 0 || szRoundCorner.cy != 0) )
{
CRect 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);
}
bHandled = FALSE;
return 0;
}
//
// ...
//
}
问题来了,程序编译出来后,没有图标,而且运行时在任务栏也没有图标。然后看了所有的duilib例子,都是这样。于是研究了一下,在资源文件中加入图标,并在oncreate中调用SetIcon(IDI_ICON_XXX),问题就解决了。
之前看到同事做的工具,觉得duilib功能还差很多,比如不支持动画,界面设计工具功能简单。但是现在用duilib开发了这个软件后,发现duilib实际上挺好用的。虽然duilib的界面设计工具不怎么好用,但是直接写xml界面也简单。而且可以先用设计工具设计局部,然后把代码拷贝到界面xml文件中。我在设计界面的过程中就常用这个小技巧。
问题来了,程序编译出来后,没有图标,而且运行时在任务栏也没有图标。然后看了所有的duilib例子,都是这样。于是研究了一下,在资源文件中加入图标,并在oncreate中调用SetIcon(IDI_ICON_XXX),问题就解决了。
之前看到同事做的工具,觉得duilib功能还差很多,比如不支持动画,界面设计工具功能简单。但是现在用duilib开发了这个软件后,发现duilib实际上挺好用的。虽然duilib的界面设计工具不怎么好用,但是直接写xml界面也简单。而且可以先用设计工具设计局部,然后把代码拷贝到界面xml文件中。我在设计界面的过程中就常用这个小技巧。