C# wpf 在style中自定义标题栏及无边框窗口

本文介绍了如何在WPF中通过Style和CS代码实现自定义标题栏、窗口阴影、拖动功能及无边框窗口。详细步骤包括定义资源字典、创建关联的CS文件以及在Style中引用CS代码来实现按钮命令和拖动逻辑。这种方法提高了代码的复用性和项目一致性。
摘要由CSDN通过智能技术生成

wpf自定义标题栏系列

第一章 自定义标题栏
第二章 添加窗口阴影
第三章 style中定义标题栏(本章)
第四章 style使用参数及模板定义标题栏



前言

本文是《C# wpf 一种style中调用cs代码的方法》的示例,同时也是《C# wpf 自定义标题栏及无边框窗口》的完善,将标题栏及无边框放到了style中,且实现了放大缩小关闭按钮以及拖动功能。这样在一个项目中有多个窗口需要使用自定义无边框效果,就可以直接复用即可,省去了每个窗口都要添加相同的界面和逻辑代码。


一、如何实现?

1.定义资源字典

添加一个名为WindowStyles资源字典。
在这里插入图片描述
定义窗口无边框的Style样式根据《C# wpf 自定义标题栏及无边框窗口》的窗口中的代码转化成Style即可。
示例如下:

<Style x:Key="WindowStyle_Normal_Gray" TargetType="Window">
    <Setter Property="AllowsTransparency" Value="True" />
    <Setter Property="WindowStyle" Value="None" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="ResizeMode" Value="NoResize" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <Grid Margin="10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <!--标题栏-->
                    <Grid Background="Gray" Height="50" >
                        <!--省略-->
                    </Grid>
                    <!--客户区-->
                    <ContentPresenter Grid.Row="1"/>
                </Grid>
            </ControlTemplate>              
        </Setter.Value>
    </Setter>
</Style>

2.定义cs文件

定义一个与资源字典名称相同的cs文件,WindowStyles.xaml.cs。
如下:
在这里插入图片描述
在WindowStyles.xaml.cs文件中定义按钮的command,可以在WindowStyles的静态构造方法中实现具体逻辑。

 public class WindowStyles
    {
        //关闭窗体
        public static ICommand CloseWindowCommand
        {
            get;
            private set;
        }
        //最小化窗体
        public static ICommand MinimizeWindowCommand
        {
            get;
            private set;
        }
        //最大化窗体
        public static ICommand MaximizeWindowCommand
        {
            get;
            private set;
        }
}

还要实现鼠标拖动功能,根据《C# wpf 一种style中调用cs代码的方法》定义一个用于初始化的附加属性,名称为IsDragMoveable。在PropertyChangedCallback中实现拖动逻辑。

示例如下:

//由于style中不能给事件赋值,但是可以在附加属性改变事件中获取控件对象,此时再给控件事件赋值。
public static bool GetIsDragMoveable(DependencyObject obj)
{
    return (bool)obj.GetValue(IsDragMoveableProperty);
}
public static void SetIsDragMoveable(DependencyObject obj, bool value)
{
    obj.SetValue(IsDragMoveableProperty, value);
}
// Using a DependencyProperty as the backing store for IsDragMoveable.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsDragMoveableProperty =
    DependencyProperty.RegisterAttached("IsDragMoveable", typeof(bool), typeof(WindowStyles), new PropertyMetadata(false,PropertyChangedCallback));

3.资源字典中关联cs代码

关闭按钮

<Button Command="{x:Static local:WindowStyles.CloseWindowCommand}"\>

最小化按钮

<Button Command="{x:Static local:WindowStyles.MinimizeWindowCommand}"\>

最大化按钮

<Button Command="{x:Static local:WindowStyles.MaximizeWindowCommand}"\>

鼠标拖动

 <!--标题栏-->
<Grid  local:WindowStyles.IsDragMoveable="True" >

二、使用方法

引用资源字典后,直接给style赋值即可,但是要注意的是,在Window中引用资源字典只能使用DynamicResource关联style。

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:WpfApp1"     
        mc:Ignorable="d"
        Title="MainWindow" Height="720" Width="1280"
        Style="{DynamicResource WindowStyle_Normal_Gray}"
        >
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="WindowStyles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid Background="White">
        <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#999999" FontSize="128" Text="Hello Word!"></TextBlock>
    </Grid>
</Window>    

效果预览
在这里插入图片描述


三、完整代码

https://download.csdn.net/download/u013113678/35730298


总结

本文讲述了一种在style中自定义标题栏无边框窗口的方法。其内部的实现其实没那么简单,尤其是实现按钮逻辑以及拖动逻辑,需要在style中关联cs代码,这就要求我们熟悉绑定技巧、附加属性还有ContentPresenter 。但是好处也是明显的,及时具有了通用性,无论是哪个窗体都可以使用。

自定义winform 窗口标题栏 主要代码 public partial class ZForm : Form { private bool moving = false; private Point oldMousePosition; public new FormBorderStyle FormBorderStyle { get { return base.FormBorderStyle; } set { if (value != FormBorderStyle.Sizable && value != FormBorderStyle.SizableToolWindow) { titlepanel.Controls.Remove(button2); } base.FormBorderStyle = value; } } #region 隐藏父类的属性,使其不可见 [Browsable(false)] public new string Text { get { return titlelabel.Text; } set { } } [Browsable(false)] public new bool ControlBox { get { return false; } set { base.ControlBox = false; } } #endregion [Browsable(true)] [EditorBrowsable(EditorBrowsableState.Always)] [Description("窗体标题")] public string Title { get { return titlelabel.Text; } set { titlelabel.Text = value; } } [Browsable(true)] [EditorBrowsable(EditorBrowsableState.Always)] [Description("窗体标题字体样式")] public Font TitleFont { get { return titlelabel.Font; } set { titlelabel.Font = value; } } [Browsable(true)] [EditorBrowsable(EditorBrowsableState.Always)] [Description("窗体标题字体颜色")] public Color TitleColor { get { return titlelabel.ForeColor; } set { titlelabel.ForeColor = value; } } [Browsable(true)] [EditorBrowsable(EditorBrowsableState.Always)] [Description("窗体标题栏背景色")] public Color TitleBarBackColor { get { return titlepanel.BackColor; } set { titlepanel.BackColor = value; } } public new bool MaximizeBox { get { return titlepanel.Contains(button2); } set { if (!value) { titlepanel.Controls.Remove(button2); } else if (!titlepanel.Contains(button2)) { titlepanel.Controls.Add(button2); } } } public new bool MinimizeBox { get { return titlepanel.Contains(button3); } set { if (!value) { titlepanel.Controls.Remove(button3); } else if (!titlepanel.Contains(button3)) { titlepanel.Controls.Add(button3); } } } private void ResetTitlePanel() { base.ControlBox = false; base.Text = null; SetToolTip(button1, "关闭"); button2.Size = button1.Size; SetToolTip(button2, "最大化或还原"); button3.Size = button1.Size; SetToolTip(button3, "最小化"); } private void SetToolTip(Control ctrl, string tip) { new ToolTip().SetToolTip(ctrl, tip); } public ZForm() { InitializeComponent(); ResetTitlePanel(); } private void Titlebutton_Click(object sender, EventArgs e) { Button btn = (Button)sender; switch (btn.Tag.ToString()) { case "close": { this.Close(); break; } case "max": { if (this.WindowState == FormWindowState.Maximized) { this.WindowState = FormWindowState.Normal; } else { this.WindowState = FormWindowState.Maximized; } break; } case "min": { if (this.WindowState != FormWindowState.Minimized) { this.WindowState = FormWindowState.Minimized; } break; } } } private void Titlepanel_MouseDown(object sender, MouseEventArgs e) { if (this.WindowState == FormWindowState.Maximized) { return; } //Titlepanel.Cursor = Cursors.NoMove2D; oldMousePosition = e.Location; moving = true; } private void Titlepanel_MouseUp(object sender, MouseEventArgs e) { //Titlepanel.Cursor = Cursors.Default; moving = false; } private void Titlepanel_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && moving) { Point newPosition = new Point(e.Location.X - oldMousePosition.X, e.Location.Y - oldMousePosition.Y); this.Location += new Size(newPosition); } } private void Titlepanel_DoubleClick(object sender, EventArgs e) { if (titlepanel.Contains(button2)) { button2.PerformClick(); } } private void titlepanel_ControlRemoved(object sender, ControlEventArgs e) { switch (e.Control.Name) { case "button2": { if (titlepanel.Contains(button3)) { button3.Left = button1.Left - button1.Width; } break; } } } private void titlepanel_ControlAdded(object sender, ControlEventArgs e) { switch (e.Control.Name) { case "button2": { if (titlepanel.Contains(button3)) { button3.Left = button2.Left - button2.Width; } break; } case "button3": { if (titlepanel.Contains(button2)) { button3.Left = button2.Left - button2.Width; } break; } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeOfCC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值