duilib 快速入门
duilib入门基础一 容器之 BOX、VBOX、HBOX布局及其子控件align排列方式
duilib入门基础二 HBOX容器内 子控件margin定位规则
duilib入门基础三 VBOX容器内 子控件margin定位规则
duilib入门基础四 BOX容器内 子控件margin定位规则
duilib入门基础 六 见见世面,duilib 基本控件 Button 能实现的界面一览
duilib入门基础 七 见见世面,duilib 基本控件 复选框 单选框
duilib入门基础 八 见见世面,duilib 基本控件 滑动条
duilib入门基础 九 见见世面,duilib 基本控件 组合框
duilib入门基础 十 见见世面,duilib 基本控件 选项框 OptionBox
1 duilib 添加全屏按钮
最大化、还原按钮
<Box name="sys_menu_box" width="16" height="16" margin="0,0,20,0" valign="center">
<Button class="btn_white_max" name="maxbtn" valign="center" />
<Button class="btn_white_restore" name="restorebtn" visible="false" />
</Box>
当名字为maxbtn\ restorebtn时,在onclicked事件中,不用写这两个事件,duilib自动处理了
在,HandleMessage中,添加WM_SIZE消息处理方式
if (uMsg == WM_SIZE)
{
if (wParam == SIZE_RESTORED)
OnWndSizeMax(false);
else if (wParam == SIZE_MAXIMIZED)
OnWndSizeMax(true);
}
在OnWndSizeMax函数中,处理放大、还原 哪个按钮显示与隐藏
void NLSLiveForm::OnWndSizeMax(bool max)
{
if (!m_pRoot)
return;
FindControl(L"maxbtn")->SetVisible(!max);
FindControl(L"restorebtn")->SetVisible(max);
FindControl(L"maxbtn_cef")->SetVisible(!max);
FindControl(L"restorebtn_cef")->SetVisible(max);
}
要是想自己添加最大化、还原按钮,
<Box name="sys_menu_box" width="16" height="16" margin="0,0,20,0" valign="center">
<Button class="btn_white_max" name="maxbtn_cef" valign="center" />
<Button class="btn_white_restore" name="restorebtn_cef" visible="false" />
</Box>
需要再onclicked 添加
else if (name==L"maxbtn_cef")
{
PostMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);
}
else if (name==L"restorebtn_cef")
{
PostMessage(WM_SYSCOMMAND, SC_RESTORE, 0);
}
在OnWndSizeMax 设置
void NLSLiveForm::OnWndSizeMax(bool max)
{
if (!m_pRoot)
return;
FindControl(L"maxbtn")->SetVisible(!max);
FindControl(L"restorebtn")->SetVisible(max);
FindControl(L"maxbtn_cef")->SetVisible(!max); //自己添加的
FindControl(L"restorebtn_cef")->SetVisible(max); //自己添加的
}
或者,直接在onclicked中 处理显示与隐藏 也可以
else if (name == L"maxbtn_my")
{
PostMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);
FindControl(L"maxbtn_my")->SetVisible(false);
FindControl(L"restorebtn_my")->SetVisible(true);
}
else if (name == L"restorebtn_my")
{
isFullScreen = false;
PostMessage(WM_SYSCOMMAND, SC_RESTORE, 0);
FindControl(L"maxbtn_my")->SetVisible(true);
FindControl(L"restorebtn_my")->SetVisible(false);
}
全屏按钮
<!-- 全屏按钮 -->
<Box name="scale_menu_box" margin="0,10,10,0" width="26" height="26" halign="right">
<Button name="max_scale_btn" normalimage="icon_fullscreen_n.png" hotimage="icon_fullscreen_h.png" pushedimage="icon_fullscreen_p.png" valign="center" tooltiptext="全屏" visible="true" />
<Button name="restore_scale_btn" visible="false" normalimage="icon_halfscreen_n.png" hotimage="icon_halfscreen_h.png" pushedimage="icon_halfscreen_p.png" valign="center" tooltiptext="退出全屏" />
</Box>
点击全屏按钮(也是全屏、还原两种状态)
else if (name==L"max_scale_btn")
{
EnterFullScreen(true);
}
else if (name == L"restore_scale_btn")
{
EnterFullScreen(false);
}
设计不好的话,还原后, 最大化 按钮位置可能不对,此处为防止位置不对,全屏时,先用sendmessage进行了窗口还原,记录下此时的窗口位置。
EnterFullScreen(bool bFull)
{
static LONG oldStyle; //记录下原窗口style
static RECT rc; //记录下还原时的位置
if (bFull) {
FindControl(L"max_scale_btn")->SetVisible(false); //全屏后 显示 还原按钮
FindControl(L"restore_scale_btn")->SetVisible(true);
/
FindControl(L"caption_hbox")->SetVisible(false);
FindControl(L"bottomParentPane")->SetVisible(false);
//为了使得还原后,最大化按钮 正常位置,先还原
//使用send 完成还原后,再往下走,记录下还原时的位置rc
SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0);
isFullScreen = true;
GetWindowRect(m_hWnd, &rc);
oldStyle = SetWindowLong(m_hWnd, GWL_STYLE, WS_POPUP);
int w = GetSystemMetrics(SM_CXSCREEN);
int h = GetSystemMetrics(SM_CYSCREEN);
SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, w, h, SWP_SHOWWINDOW);
}
else {
FindControl(L"max_scale_btn")->SetVisible(true); //还原后,显示全屏按钮
FindControl(L"restore_scale_btn")->SetVisible(false);
/
FindControl(L"caption_hbox")->SetVisible(true);
FindControl(L"bottomParentPane")->SetVisible(true);
isFullScreen = false;
SetWindowLong(m_hWnd, GWL_STYLE, oldStyle);
//还原后,使用参数HWND_NOTOPMOST,取消窗口置顶,否则摄像头选择等窗口看不见
SetWindowPos(m_hWnd, HWND_NOTOPMOST, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_SHOWWINDOW);
}
}
全屏后,点击键盘的esc按钮,先退出全屏
duilib重载OnEsc
virtual void OnEsc(BOOL &bHandled) override;
void ChatroomForm::OnEsc(BOOL &bHandled)
{
bHandled = TRUE;
if (isFullScreen)
{
EnterFullScreen(false);
return;
}
}
多屏下 全屏与最大化 处理
1)最大化与还原
void ChatroomForm::FullScreen(bool bFull)//学生端只显示PPT
{
static LONG oldStyle; //记录下原窗口style
static RECT rc; //记录下还原时的位置
if (bFull) {
isFullScreen_ = true;
MaxWindow();
}
else {
isFullScreen_ = false;
RestoreWindow(false);
}
}
void ChatroomForm::MaxWindow(){
ui::Control *pCtrl = FindControl(L"maxbtn_my");
if (!pCtrl->IsVisible())
return;
PostMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);
SetControlVisible(L"maxbtn_my", false);
SetControlVisible(L"restorebtn_my", true);
}
void ChatroomForm::RestoreWindow(bool bJudgeRestoreCtrlVisible){
if (bJudgeRestoreCtrlVisible){
ui::Control *pCtrl = FindControl(L"restorebtn_my");
if (pCtrl&&!pCtrl->IsVisible())
return;
}
PostMessage(WM_SYSCOMMAND, SC_RESTORE, 0);
SetControlVisible(L"maxbtn_my", true);
SetControlVisible(L"restorebtn_my", false);
}
上面调用 PostMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0); 使得窗口最大化,但不是全屏,因为任务栏还没有遮挡住
2)全屏处理
void ChatroomForm::FullScreenPPT_Stu(bool bFull)//学生端只显示PPT
{
static LONG oldStyle; //记录下原窗口style
static RECT rc; //记录下还原时的位置
if (bFull) {
isFullScreen_ = true;
//为了使得还原后,最大化按钮 正常位置,先还原
//使用send 完成还原后,再往下走,记录下还原时的位置rc
SendMessage(WM_SYSCOMMAND, SC_RESTORE, 0);
GetWindowRect(m_hWnd, &rc); //记录原先的窗口位置 以便还原
CMonitor* pMonitor = g_monitorMng.GetNearestMonitor(m_hWnd);
RECT rect;
pMonitor->GetMonitorRect(rect); //获得窗口所在屏幕的 屏幕矩形位置 充满此屏幕
oldStyle = SetWindowLong(m_hWnd, GWL_STYLE, WS_POPUP);
int w = abs(rect.right - rect.left);
int h = abs(rect.bottom - rect.top);
SetWindowPos(m_hWnd, HWND_TOPMOST, rect.left, rect.top, w, h, SWP_SHOWWINDOW);
}
else {
isFullScreen_ = false;
RestoreWindow(false);
ui::Control* pRootBox = (ui::Control*)FindControl(L"rootBox");
if (pRootBox)
pRootBox->SetBorderColor(L"green");
ChangeSkin(XNWBK_Green);
SetWindowLong(m_hWnd, GWL_STYLE, oldStyle);
//还原窗口位置,使用参数HWND_NOTOPMOST,取消窗口置顶,否则摄像头选择等窗口看不见
SetWindowPos(m_hWnd, HWND_NOTOPMOST, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, SWP_SHOWWINDOW);
}
}