duilib开发(十一):创建子窗口

一、创建子窗口

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=&quot;30&quot; width=&quot;100&quot; normalimage=&quot;file=&apos;common/button_normal.bmp&apos;&quot; hotimage=&quot;file=&apos;common/button_over.bmp&apos;&quot; pushedimage=&quot;file=&apos;common/button_down.bmp&apos;&quot; font=&quot;0&quot;" />
  <Default shared="true" name="Option" value="textcolor=&quot;#FFbac0c5&quot; hottextcolor=&quot;#FF386382&quot; selectedtextcolor=&quot;#FF386382&quot; disabledtextcolor=&quot;#FFbac0c5&quot; textpadding=&quot;18,2,0,0&quot; align=&quot;left&quot; selectedimage=&quot;file='common/RadioBtnSel.png' source='0,0,13,13' dest='0,9,14,23'&quot; normalimage=&quot;file='common/RadioBtnNon.png' source='0,0,13,13' dest='0,9,14,23'&quot;" />
  <Default shared="true" name="CheckBox" value="textcolor=&quot;#FFbac0c5&quot; hottextcolor=&quot;#FF386382&quot; selectedtextcolor=&quot;#FF386382&quot; disabledtextcolor=&quot;#FFbac0c5&quot; textpadding=&quot;20,2,0,0&quot; align=&quot;left&quot; selectedimage=&quot;file='common/checked.png' dest='0,8,16,24'&quot; normalimage=&quot;file='common/unchecked.png' dest='0,8,16,24'&quot;" />

  <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=&quot;30&quot; width=&quot;100&quot; normalimage=&quot;file=&apos;common/button_normal.bmp&apos;&quot; hotimage=&quot;file=&apos;common/button_over.bmp&apos;&quot; pushedimage=&quot;file=&apos;common/button_down.bmp&apos;&quot; font=&quot;0&quot;" />
  <Default shared="true" name="Option" value="textcolor=&quot;#FFbac0c5&quot; hottextcolor=&quot;#FF386382&quot; selectedtextcolor=&quot;#FF386382&quot; disabledtextcolor=&quot;#FFbac0c5&quot; textpadding=&quot;18,2,0,0&quot; align=&quot;left&quot; selectedimage=&quot;file='common/RadioBtnSel.png' source='0,0,13,13' dest='0,9,14,23'&quot; normalimage=&quot;file='common/RadioBtnNon.png' source='0,0,13,13' dest='0,9,14,23'&quot;" />
  <Default shared="true" name="CheckBox" value="textcolor=&quot;#FFbac0c5&quot; hottextcolor=&quot;#FF386382&quot; selectedtextcolor=&quot;#FF386382&quot; disabledtextcolor=&quot;#FFbac0c5&quot; textpadding=&quot;20,2,0,0&quot; align=&quot;left&quot; selectedimage=&quot;file='common/checked.png' dest='0,8,16,24'&quot; normalimage=&quot;file='common/unchecked.png' dest='0,8,16,24'&quot;" />

  <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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值