WPF中自定义窗体标题栏

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

<Grid Grid.Row=" 0" x:Name="TitleBar" MouseMove="TitleBar_MouseMove" >
            <TextBlock Text="这是标题栏" FontSize="15" />
</Grid>
注意,我们给TitleBar添加了MouseMove事件,后台处理代码:

private void TitleBar_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }

如果没有为自定义的TitleBar添加MouseMove事件,那么就无法拖动窗体。

当然我写的这个标题栏比较简单,只是为了演示,大家可以扩充,根据需求放置最大化、最小化、关闭按钮等。

前台所有代码:

<Window x:Class="WpfStudy.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" WindowStyle="None"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  WindowStartupLocation="CenterScreen" Topmost="False"
    SizeToContent="WidthAndHeight"     >    
    <Grid >       
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="150"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="300"/>           
        </Grid.ColumnDefinitions>
        <Grid Grid.Row=" 0" x:Name="TitleBar" Height="Auto" MouseMove="TitleBar_MouseMove" Background="Bisque">
            <TextBlock Text="这是标题栏" FontSize="15" />
        </Grid>
        <Grid Grid.Row=" 1" Background="Azure"></Grid>        
    </Grid>
</Window>

效果图:


这个示例够简单了,实在是不能再简化了~~

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值