欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

wpf 窗口自定义标题栏

程序员文章站 2022-03-01 13:04:41
...

XAML内容:

​
<Window x:Class="Demo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell"
        Title="MainWindow" Height="650" Width="1325" FontFamily="{StaticResource SiyuanHeiTi}" FontSize="14"  Foreground="{DynamicResource  foreground_Main }" WindowStyle="None"  WindowState="Maximized" Icon="/Demo;component/Images/logo.png">

    <shell:WindowChrome.WindowChrome><!--保证窗口边框可以拖拽-->
        <shell:WindowChrome CaptionHeight="1"  ResizeBorderThickness="1,1,1,5"> </shell:WindowChrome>
    </shell:WindowChrome.WindowChrome>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"></RowDefinition>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="39"></RowDefinition>
        </Grid.RowDefinitions>

        <Border  Grid.Row="0" BorderThickness="0,0,0,2" BorderBrush="{DynamicResource  borderWindow_Color }">
            <Canvas Margin="1" Background="{DynamicResource backcolor_Main}" MouseMove="WrapPanel_MouseMove" MouseLeftButtonDown="Canvas_MouseLeftButtonDown" Height="50"  >
                <Label  Width="30" Height="30" HorizontalAlignment="Left" Canvas.Left="12"  Canvas.Top="7"  >
                    <Label.Background>
                        <ImageBrush ImageSource="pack://application:,,,/Demo;component/Images/logo.png"></ImageBrush>
                    </Label.Background>
                </Label>
                <Label Canvas.Left="45" Canvas.Top="7" Foreground="{DynamicResource  foreground_Main }" Content="系统" VerticalContentAlignment="Center" FontSize="20" FontWeight="Bold"></Label>
                <Menu Visibility="Visible" Canvas.Left="270" Canvas.Top="10" Background="Transparent" BorderBrush="{DynamicResource borderWindow_Color}" BorderThickness="0" VerticalContentAlignment="Center" VerticalAlignment="Center" >
                    <MenuItem Header="操作">
                    </MenuItem>
                    <MenuItem Name="menuSetting" Header="配置" >
                    </MenuItem>

                </Menu>
                <DockPanel Canvas.Right="5" Canvas.Top="10"   HorizontalAlignment="Right" >
                    <Button Margin="5" Style="{DynamicResource MyTitleButton}" Click="MenuItemAbout_Click" Name="btnAbout" Width="18" Height="18">
                        <Button.Background>
                            <ImageBrush ImageSource="pack://application:,,,/Demo;component/Images/about.png"></ImageBrush>
                        </Button.Background>
                    </Button>
                    <Button Margin="5" Style="{DynamicResource MyTitleButton}" Click="btnMin_Click" Name="btnMin" Width="18" Height="18">
                        <Button.Background>
                            <ImageBrush ImageSource="pack://application:,,,/Demo;component/Images/min.png"></ImageBrush>
                        </Button.Background>
                    </Button>
                    <Button Margin="5"  Style="{DynamicResource MyTitleButton}"  Width="18" Height="18" Name="btnMax" Click="btnMax_Click" >
                        <Button.Background>
                            <ImageBrush x:Name="btnMaxIcon" ImageSource="pack://application:,,,/Demo;component/Images/max.png"></ImageBrush>
                        </Button.Background>
                    </Button>
                    <Button Margin="5,5,10,5" Style="{DynamicResource MyTitleButton}" Width="16" Height="16" Name="btnClose" Click="btnClose_Click" >
                        <Button.Background>
                            <ImageBrush ImageSource="pack://application:,,,/Demo;component/Images/close.png"></ImageBrush>
                        </Button.Background>
                    </Button>
                </DockPanel>
            </Canvas>
        </Border>
<Grid Grid.Row="1">
<!--内容-->
</Grid>
        <StackPanel  Grid.Row="2" Height="30" >
<!--状态栏-->
            <Label x:Name="lbDatapath" Content="" BorderBrush="{DynamicResource border_Color}" BorderThickness="0.5"></Label>
        </StackPanel>
</Window>

​

下面是标题拖动及缩放事件代码:

​

        #region 标题事件
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 窗口放大
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMax_Click(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == System.Windows.WindowState.Maximized)
            {
                this.WindowState = System.Windows.WindowState.Normal;
                btnMaxIcon.ImageSource = AppHelper.Img_MaxIcon;
            }
            else
            {
                this.WindowState = System.Windows.WindowState.Maximized;
                btnMaxIcon.ImageSource = AppHelper.Img_NormalIcon;
            }
        }
        /// <summary>
        /// 最小化窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMin_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;
        }

        /// <summary>
        /// 窗口拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WrapPanel_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }
        private long lastTitleLeftDownTime=0;
        /// <summary>
        /// 双击放大
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            long ticks= DateTime.Now.Ticks/1000;
            if (ticks - 2000 > lastTitleLeftDownTime)
            {
                lastTitleLeftDownTime = ticks;
            }
            else {
                lastTitleLeftDownTime = 0;
                btnMax_Click(null, null);
            }
        }
        /// <summary>
        /// 双击最大化或还原
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Label_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {

            if (this.WindowState == System.Windows.WindowState.Maximized)
            {
                this.WindowState = System.Windows.WindowState.Normal;
                btnMaxIcon.ImageSource = AppHelper.Img_MaxIcon;
            }
            else
            {
                this.WindowState = System.Windows.WindowState.Maximized;
                btnMaxIcon.ImageSource = AppHelper.Img_NormalIcon;
            }
        }
        /// <summary>
        /// 关于
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItemAbout_Click(object sender, RoutedEventArgs e)
        {
            SettingGUI.WindowAbout winAbout = new SettingGUI.WindowAbout() { WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen };
            winAbout.ShowDialog();
        }

        #endregion

​

 

 

相关标签: WPF