DuiLib笔记之自定义标题栏以及响应按钮点击事件

时间:2024-04-14 09:23:34

在博文DuiLib笔记,基于WindowImplBase的基础模板的基础上,修改皮肤文件如下

<?xml version="1.0" encoding="utf-8"?>
<Window size="350,250" caption="0,0,0,35" sizebox="6,6,6,6" mininfo="300,200">
  <Font id="0" name="宋体" size="17" bold="true" underline="true" italic="true" />
  <!--窗口背景色#FFFFFFFF-->
  <VerticalLayout bkcolor="#FFFFFFFF">
    <!--标题栏高度35,背景色#FF12B7F5-->
    <HorizontalLayout height="35" bkcolor="#FF12B7F5">
      <Control bkimage="file='Icons/wolf_white_55x30.png'" bkcolor="0xFF000000" float="true" pos="15,3,70,33" />
      <!--标题DuiLib演示-->
      <Label text="DuiLib演示" textcolor="0xFFFFFFFF" font="0" float="true" pos="75,0,200,35" />
      <!--空白区-->
      <Control />
      <HorizontalLayout width="90">
        <!--最小化、最大化等按钮-->
        <Button name="minbtn" float="true" pos="5,8,25,28" tooltip="最小化"
                normalimage="file='Icons/minimize/minimize_white_20x20.png'" hotimage="file='Icons/minimize/minimize_red_20x20.png'" pushedimage="file='Icons/minimize/minimize_black_20x20.png'" />
        <Button name="maxbtn" float="true" pos="35,8,55,28" tooltip="最大化"
                normalimage="file='Icons/maximize/maximize_white_20x20.png'" hotimage="file='Icons/maximize/maximize_red_20x20.png'" pushedimage="file='Icons/maximize/maximize_black_20x20.png'" />
        <Button name="restorebtn" float="true" pos="35,8,55,28" tooltip="还原" visible="false"
                normalimage="file='Icons/restore_down/restore_down_white_20x20.png'" hotimage="file='Icons/restore_down/restore_down_red_20x20.png'" pushedimage="file='Icons/restore_down/restore_down_black_20x20.png'" />
        <Button name="closebtn" float="true" pos="65,8,85,28" tooltip="关闭"
                normalimage="file='Icons/close/close_white_20x20.png'" hotimage="file='Icons/close/close_red_20x20.png'" pushedimage="file='Icons/close/close_black_20x20.png'" />
      </HorizontalLayout>
    </HorizontalLayout>
    <!--居中的按钮-->
    <Button name="button" text="*不是免费的。" pos="-65,-15,65,15" float="0.5,0.5,0.5,0.5" tooltip="*不是免费的" />
  </VerticalLayout>
</Window>

因为在void WindowImplBase::OnClick(TNotifyUI &msg)里,已对窗口的最小化、最大化等操作做了处理

所以我们只需要把最小化、最大化等按钮的name设置为minbtn、maxbtn、restorebtn、closebtn就行了

当然,如果不喜欢这样的命名风格,可以在WindowImplBase::OnClick里进行修改

启动工程,程序界面如下图

DuiLib笔记之自定义标题栏以及响应按钮点击事件

要响应按钮的点击,只需重写WindowImplBase::OnClick

如下

virtual void OnClick(TNotifyUI &msg)
{
    const CDuiString &controlName = msg.pSender->GetName();
    if (controlName == TEXT("button"))
    {
        MessageBox(GetHWND(), TEXT("To win, to survive."), TEXT("为了胜利,为了生存。"), MB_ICONINFORMATION);
    }
    else
    {
        WindowImplBase::OnClick(msg);
    }
}

DuiLib笔记之自定义标题栏以及响应按钮点击事件

完整Demo链接:https://pan.baidu.com/s/1M6aYOeuwo96_ME1E6604VA,提取码:4i9a