为了账号安全,请及时绑定邮箱和手机立即绑定

如何在WPF中创建自定义窗口镶边?

如何在WPF中创建自定义窗口镶边?

森栏 2019-12-13 09:42:48
如何为WPF窗口创建基本的自定义窗口镶边,该样式不包含关闭按钮,而仍然是可移动且可调整大小的窗口?
查看完整描述

3 回答

?
GCT1015

TA贡献1827条经验 获得超4个赞

您设置窗口的WindowStyle="None",然后构建自己的窗口界面。您需要构建自己的“最小/最大/关闭/拖动”事件处理程序,但仍会保持“调整大小”。


例如:


<Window 

    WindowState="Maximized" 

    WindowStyle="None"

    WindowStartupLocation="CenterScreen"

    MaxWidth="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Width}"

    MaxHeight="{Binding Source={x:Static SystemParameters.WorkArea}, Path=Height}"

>


    <DockPanel x:Name="RootWindow">

        <DockPanel x:Name="TitleBar" DockPanel.Dock="Top">

            <Button x:Name="CloseButton" Content="X"

                    Click="CloseButton_Click"                 

                    DockPanel.Dock="Right" />

            <Button x:Name="MaxButton" Content="Restore" 

                    Click="MaximizeButton_Click"

                    DockPanel.Dock="Right" />

            <Button x:Name="MinButton" Content="Min"

                    Click="MinimizeButton_Click"

                    DockPanel.Dock="Right" />


            <TextBlock HorizontalAlignment="Center">Application Name</TextBlock>

        </DockPanel>


        <ContentControl Content="{Binding CurrentPage}" />

    </DockPanel>


</Window>

下面是一些常见窗口功能的示例代码


/// <summary>

/// TitleBar_MouseDown - Drag if single-click, resize if double-click

/// </summary>

private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)

{

    if(e.ChangedButton == MouseButton.Left)

        if (e.ClickCount == 2)

        {

            AdjustWindowSize();

        }

        else

        {

            Application.Current.MainWindow.DragMove();

        }

 }


/// <summary>

/// CloseButton_Clicked

/// </summary>

private void CloseButton_Click(object sender, RoutedEventArgs e)

{

   Application.Current.Shutdown();

}


/// <summary>

/// MaximizedButton_Clicked

/// </summary>

private void MaximizeButton_Click(object sender, RoutedEventArgs e)

{

    AdjustWindowSize();

}


/// <summary>

/// Minimized Button_Clicked

/// </summary>

private void MinimizeButton_Click(object sender, RoutedEventArgs e)

{

    this.WindowState = WindowState.Minimized;

}


/// <summary>

/// Adjusts the WindowSize to correct parameters when Maximize button is clicked

/// </summary>

private void AdjustWindowSize()

{

    if (this.WindowState == WindowState.Maximized)

    {

        this.WindowState = WindowState.Normal;

        MaximizeButton.Content = "1";

    }

    else

    {

        this.WindowState = WindowState.Maximized;

        MaximizeButton.Content = "2";

    }


}


查看完整回答
反对 回复 2019-12-13
  • 3 回答
  • 0 关注
  • 612 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信