duiLib 利用布局文件显示一个窗口并响应事件
窗口布局文件:
<?xml version="1.0" encoding="UTF-8"?>
<!--界面布局-->
<!-- 窗口设置 -->
<Window roundcorner="5,5" size="600,450" mininfo="80,60" ><!--定义默认字体--><Font id="0" name="微软雅黑" size="12" bold="false" default="true" shared="true" /><Font id="1" name="微软雅黑" size="12" bold="true" shared="true" /><VerticalLayout bkcolor="#FFFFFFFF" bordersize="2" bordercolor="#FF0934DA"><!--标题栏区域--><HorizontalLayout name="header" height="36" bkcolor="#FF4775CC"><HorizontalLayout><Control width="10"/><Label name="apptitle" text="我是标题" font="1" textcolor="#FFFFFFFF" /></HorizontalLayout> <HorizontalLayout height="25" width="96"><Button name="minbtn" tooltip="最小化" height="18" width="26" normalimage="file='minimize.png'" hotimage="file='minimize_h.png'" pushedimage="file='minimize_p.png'"/><Button name="maxbtn" tooltip="最大化" height="18" width="25" normalimage="file='maximize.png'" hotimage="file='maximize_h.png'" pushedimage="file='maximize_p.png'"/><Button name="restorebtn" visible="false" tooltip="恢复" height="18" width="25" normalimage="file='restore.png'" hotimage="file='restore_h.png'" pushedimage="file='restore_p.png'"/> <Button name="closebtn" tooltip="关闭" height="18" width="43" normalimage="file='close.png'" hotimage="file='close_h.png'" pushedimage="file='close_p.png'"/> </HorizontalLayout></HorizontalLayout><VerticalLayout name="body" padding="10,10,2,2"><Text name="text_str" wordbreak="true" text="安能摧眉折腰事权贵,使我不得开心颜" fontColor="0xFF333333" textPadding="5,5,5,5"/><Control height="10" /><Edit name="edit_str" width="120" height="26" textpadding="1,1,1,1" bkcolor="#FFFAEBD7"/><Control height="10" /><Button name="btn_test" text="测试" height="30" width="120" normalimage="file='button.png'" hotimage="file='button_h.png'" pushedimage="file='button_p.png'"/> </VerticalLayout></VerticalLayout>
</Window>
其中资源文件:
事件处理:
notify函数代码如下:
// 核心功能:控件事件分发、消息传递枢纽
void Notify(TNotifyUI& msg)
{if (msg.sType == _T("click")) {//if (msg.pSender->GetName() == _T("I服了You")) { // 即点击的根控件root,代码见下面// Close(); // 关闭窗口//}if (msg.pSender->GetName() == _T("closebtn")) { // 点击关闭窗口Close();} else if (msg.pSender->GetName() == _T("minbtn")) { // 最小化//ShowWindow(m_hWnd, SW_MINIMIZE); // 不起作用::ShowWindow(m_hWnd, SW_MINIMIZE); // 通过全局作用域符强制调用Win32 API函数}else if (msg.pSender->GetName() == _T("maxbtn")) { // 最大化::ShowWindow(m_hWnd, SW_MAXIMIZE);// 隐藏最大化按钮CButtonUI* maxBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("maxbtn")));if (maxBtn) {maxBtn->SetVisible(false);}// 显示恢复按钮CButtonUI* restoreBtn = static_cast<CButtonUI*>(m_pm.FindControl(_T("restorebtn")));if (restoreBtn) {restoreBtn->SetVisible(true);}}else if (msg.pSender->GetName() == _T("restorebtn")) { // 恢复::ShowWindow(m_hWnd, SW_RESTORE);CControlUI* restoreBtn = static_cast<CControlUI*>(m_pm.FindControl(_T("restorebtn")));if (restoreBtn) restoreBtn->SetVisible(false);CControlUI* maxBtn = static_cast<CControlUI*>(m_pm.FindControl(_T("maxbtn")));if (maxBtn) maxBtn->SetVisible(true);} else if (msg.pSender->GetName() == _T("btn_test")) {// 测试读取输入框文本:CEditUI* editStr = static_cast<CEditUI*>(m_pm.FindControl(_T("edit_str")));if (editStr) {CDuiString strText = editStr->GetText(); // 获取文本内容CTextUI* textStr = static_cast<CTextUI*>(m_pm.FindControl(_T("text_str")));if (textStr) textStr->SetText(strText); // 刷新文本组件// 测试显示消息盒MessageBox(m_hWnd, L"安能摧眉折腰事权贵,使我不得开心颜!", L"消息标题", MB_OK | MB_ICONINFORMATION);}}}
}
运行:
随意输入点文本,比如“老顽童”,点击测试按钮:
ok. 测试了右上角最小化、最大化、恢复的按钮功能也ok. 但是窗口不能用鼠标拖动,后面有时间再看下。