一、创建子窗口
1、创建主窗口,添加两个 Button
<?xml version="1.0" encoding="UTF-8"?>
<Window size="960,540" mininfo="600,400" caption="0,0,0,32" sizebox="4,4,4,4">
<Font id="0" shared="true" name="宋体" size="18" bold="false" underline="false" italic="false" />
<Font id="1" shared="true" name="宋体" size="20" bold="false" underline="false" italic="false" />
<Font id="2" shared="true" name="宋体" size="16" bold="false" underline="false" italic="false" />
<Default shared="true" name="Button" value=" height="30" width="100" normalimage="file='common/button_normal.bmp'" hotimage="file='common/button_over.bmp'" pushedimage="file='common/button_down.bmp'" font="0"" />
<Default shared="true" name="Option" value="textcolor="#FFbac0c5" hottextcolor="#FF386382" selectedtextcolor="#FF386382" disabledtextcolor="#FFbac0c5" textpadding="18,2,0,0" align="left" selectedimage="file='common/RadioBtnSel.png' source='0,0,13,13' dest='0,9,14,23'" normalimage="file='common/RadioBtnNon.png' source='0,0,13,13' dest='0,9,14,23'"" />
<Default shared="true" name="CheckBox" value="textcolor="#FFbac0c5" hottextcolor="#FF386382" selectedtextcolor="#FF386382" disabledtextcolor="#FFbac0c5" textpadding="20,2,0,0" align="left" selectedimage="file='common/checked.png' dest='0,8,16,24'" normalimage="file='common/unchecked.png' dest='0,8,16,24'"" />
<VerticalLayout bkcolor="#FFDFFDF0" width="300" height="318"> <!-- 整个窗口使用 HorizontalLayout 布局 -->
<VerticalLayout>
<Button name="btnLogin" text="登录" float="true" pos="300,190,400,220"/>
<Button name="btnQuit" text="退出" float="true" pos="500,190,600,220"/>
</VerticalLayout>
</VerticalLayout>
</Window>
创建子窗口,样式和主窗口一样,唯一不同的一点就是大小不同
<?xml version="1.0" encoding="UTF-8"?>
<Window size="600,400" mininfo="600,400" caption="0,0,0,32" sizebox="4,4,4,4">
<Font id="0" shared="true" name="宋体" size="18" bold="false" underline="false" italic="false" />
<Font id="1" shared="true" name="宋体" size="20" bold="false" underline="false" italic="false" />
<Font id="2" shared="true" name="宋体" size="16" bold="false" underline="false" italic="false" />
<Default shared="true" name="Button" value=" height="30" width="100" normalimage="file='common/button_normal.bmp'" hotimage="file='common/button_over.bmp'" pushedimage="file='common/button_down.bmp'" font="0"" />
<Default shared="true" name="Option" value="textcolor="#FFbac0c5" hottextcolor="#FF386382" selectedtextcolor="#FF386382" disabledtextcolor="#FFbac0c5" textpadding="18,2,0,0" align="left" selectedimage="file='common/RadioBtnSel.png' source='0,0,13,13' dest='0,9,14,23'" normalimage="file='common/RadioBtnNon.png' source='0,0,13,13' dest='0,9,14,23'"" />
<Default shared="true" name="CheckBox" value="textcolor="#FFbac0c5" hottextcolor="#FF386382" selectedtextcolor="#FF386382" disabledtextcolor="#FFbac0c5" textpadding="20,2,0,0" align="left" selectedimage="file='common/checked.png' dest='0,8,16,24'" normalimage="file='common/unchecked.png' dest='0,8,16,24'"" />
<VerticalLayout bkcolor="#FFDFFDF0" width="300" height="318"> <!-- 整个窗口使用 HorizontalLayout 布局 -->
<VerticalLayout>
<Button name="btnLogin" text="登录" float="true" pos="100,190,200,220"/>
<Button name="btnQuit" text="退出" float="true" pos="300,190,400,220"/>
</VerticalLayout>
</VerticalLayout>
</Window>
当点击登录按钮的时候再创建一个子窗口,然后居中显示
void MainWnd::OnClick(DuiLib::TNotifyUI& msg)
{
if (msg.pSender->GetName() == "btnLogin")
{
_childWnd = new ChildWnd();
_childWnd->Create(_ownerWnd,_T("Child"), UI_WNDSTYLE_FRAME, 0);
_childWnd->CenterWindow();
_childWnd->ShowWindow(true, false);
}
else if (msg.pSender->GetName() == "btnQuit")
{
Close();
}
}
运行观看结果
这个时候我们可以随意的操作子窗口和主窗口,主窗口关闭的时候子窗口也会关闭,但是这里有一个问题就是,子窗口永远在主窗口的上方,除非我们把子窗口移动到边上去
感觉这个应该和 Windows 的相关属性有关系,目前可以想到的一种办法是,不创建子窗口,也是创建一个单独的窗口,Create 时将第一个参数设置为 NULL,这样就会创建两个互相独立的窗口
二、模态对话框和非模态对话框
1、模态对话框:在程序运行的过程中,若出现了模态对话框,那么主窗口将无法发送消息,直到模态对话框退出才可以发送。也就是说如果子窗口是一个模态对话框,那么只有在子窗口退出之后才能继续操作主窗口。还是继续上面的例子
void MainWnd::OnClick(DuiLib::TNotifyUI& msg)
{
if (msg.pSender->GetName() == "btnLogin")
{
_childWnd = new ChildWnd();
_childWnd->Create(_ownerWnd,_T("Child"), UI_WNDSTYLE_FRAME, 0);
_childWnd->CenterWindow();
// 模态对话框
_childWnd->ShowModal();
}
else if (msg.pSender->GetName() == "btnQuit")
{
Close();
}
}
这个时候在运行程序就会发现,当创建子窗口后市没有办法操作主窗口了,只有当子窗口退出才能继续操作子窗口,我们可以看一下 ShowModal 里面的代码
UINT CWindowWnd::ShowModal()
{
ASSERT(::IsWindow(m_hWnd));
UINT nRet = 0;
HWND hWndParent = GetWindowOwner(m_hWnd);
::ShowWindow(m_hWnd, SW_SHOWNORMAL);
::EnableWindow(hWndParent, FALSE);
MSG msg = { 0 };
while( ::IsWindow(m_hWnd) && ::GetMessage(&msg, NULL, 0, 0) ) {
if( msg.message == WM_CLOSE && msg.hwnd == m_hWnd ) {
nRet = msg.wParam;
::EnableWindow(hWndParent, TRUE);
::SetFocus(hWndParent);
}
if( !CPaintManagerUI::TranslateMessage(&msg) ) {
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
if( msg.message == WM_QUIT ) break;
}
::EnableWindow(hWndParent, TRUE);
::SetFocus(hWndParent);
if( msg.message == WM_QUIT ) ::PostQuitMessage(msg.wParam);
return nRet;
}
首先是显示子窗口,然后把父窗口 Enable 为 FALSE,接下来有个循环,一直到窗口无效或者被销毁才会退出
2、非模态对话框:在程序运行的过程中,若出现了非模态对话框,主窗口还可以发送消息。其实这个时候主窗口和子窗口是互不影响的
3、关于模态对话框和非模态对话框根据选择来使用
三、参考资料
1、模态对话框和非模态对话框:https://blog.csdn.net/u013776188/article/details/75007780