【WPF】自定义标题栏

【WPF】自定义标题栏,第1张

上一篇我们说了去掉丑陋的标题栏变成无边框,这一篇我渗扒启们说一下怎么制作自己想要的漂亮标题栏代替系统默认的。

在WPF中自定义窗体标题栏,首先需要将窗体的WindowStyle属性设置为None,隐藏掉WPF窗体的自带标题栏。然后可以在窗体内部自定义一个标题栏。

后台的事件

后台的事件:

#region 标题栏事件

        /// <summary>        /// 窗口移动事件

        /// </summary>        private void TitleBar_MouseMove(object sender, MouseEventArgs e)

        {

            if (e.LeftButton == MouseButtonState.Pressed)

            {

                this.DragMove()

            }

        }

        int i = 0

        /// <summary>        /// 标题栏双击事件

        /// </summary>

        private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)

        {

            i += 1

            System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer()

 此迟           timer.Interval = new TimeSpan(0, 0, 0, 0, 300)

            timer.Tick += (s, e1) =>{ timer.IsEnabled = falsei = 0}

            timer.IsEnabled = true

            if (i % 2 == 0)

            {

                timer.IsEnabled = false

                i = 0

                this.WindowState = this.WindowState == WindowState.Maximized ?

                              WindowState.Normal : WindowState.Maximized

   丛如         }

        }

        /// <summary>        /// 窗口最小化

        /// </summary>        private void btn_min_Click(object sender, RoutedEventArgs e)

        {

            this.WindowState = WindowState.Minimized//设置窗口最小化        }

        /// <summary>        /// 窗口最大化与还原

        /// </summary>        private void btn_max_Click(object sender, RoutedEventArgs e)

        {

            if (this.WindowState == WindowState.Maximized)

            {

                this.WindowState = WindowState.Normal//设置窗口还原            }

            else            {

                this.WindowState = WindowState.Maximized//设置窗口最大化            }

        }

        /// <summary>        /// 窗口关闭

        /// </summary>

        private void btn_close_Click(object sender, RoutedEventArgs e)

        {

            this.Close()

        }

        #endregion 标题栏事件

给对应的按钮加上事件就可以了

在 WPF 中,您可以使用自定义模板(Template)来创建一个好看的标题栏。以下是一个简单的示例,演示如何创建一个具有渐变背景和自定义按钮的标题栏:

<Window x:Class="MyWindow"

title="My Window"

WindowStyle="None"

AllowsTransparency="True"

Background="Transparent">

<Window.Template>

<ControlTemplate TargetType="{x:Type Window}">

<Border BorderThickness="1"

BorderBrush="Gray"

CornerRadius="5">

<Grid>

<Grid.RowDefinitions>

<RowDefinition Height="30" />

<RowDefinition Height="*" />

</Grid.RowDefinitions>

<Border Grid.Row="0"

Background="#FF7F7F7F">

<StackPanel Orientation="Horizontal">

<TextBlock Text="{TemplateBinding Title}"

Margin="5,0,0,0"

VerticalAlignment="Center"

FontSize="14" />

<StackPanel Orientation="Horizontal"

Margin="5,0,0,0">

<Button Content="⚙"

Width="20"

Height="20"

Margin="5,0,0,0"

VerticalAlignment="Center"

HorizontalAlignment="Left"

Background="#FF7F7F7F"

BorderThickness="0"

Foreground="White"

FontSize="14"

ToolTip="Settings" />

<Button Content="❐耐简"

Width="20"

Height="20"

Margin="5,0,0,0"

VerticalAlignment="昌芦裤Center"

HorizontalAlignment="Left"

Background="#FF7F7F7F"

BorderThickness="0"哗野

Foreground="White"

FontSize="14"

ToolTip="Minimize"

Click="MinimizeButton_Click" />

<Button Content="❌"

Width="20"

Height="20"

Margin="5,0,0,0"

VerticalAlignment="Center"

HorizontalAlignment="Left"

Background="#FF7F7F7F"

BorderThickness="0"

Foreground="White"

FontSize="14"

ToolTip="Close"

Click="CloseButton_Click" />

</StackPanel>

</StackPanel>

</Border>

<Border Grid.Row="1"

Background="White">

<ContentPresenter />

</Border>

</Grid>

</Border>

</ControlTemplate>

</Window.Template>

</Window>

这个示例中,我们创建了一个 ControlTemplate,并将它的 TargetType 设置为 Window,然后在模板中定义了一个 Border,作为整个窗口的容器。然后,我们将 Grid 放置在 Border 中,以便我们可以将窗口分成两个部分:标题栏和内容部分。在标题栏中,我们使用了一个 StackPanel,其中包含一个 TextBlock 和三个 Button,用于显示窗口的标题、设置按钮、最小化按钮和关闭按钮。

我们使用了许多属性来定制标题栏的外观,包括颜色、大小、边框、字体大小等等。您可以根据自己的需要进行更改和修改。

如果你是想显示代码,弊型而不是呈现xaml代码对应的控件,那就用TextBlock 2.如果想动态呈现租告猜控件友颂,参考XamlReader http://technet.microsoft.com/zh-cn/system.windows.markup.xamlreader(de-de).aspx


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11976684.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存