• WPF自定义标题栏——窗口移动和按钮状态转换


    其实这一部分的资料网上很多,也看了很多的例子,不过大部分都不是自己想要的结果,所以就综合网上的资料整理了一下,放在这里吧,虽然现在放弃WPF了,不过以后还是想接着学一下,先放着吧就。

    标题栏的移动:

    其实这部分还是很简单的,只需加一个<Grid>(其他布局也可以),然后响应鼠标移动事件就好了。事件响应时的代码也非常简单,判断现在鼠标是否在按下状态,如果是,调用窗口移动命令。

    <Grid Name="titleBar" VerticalAlignment="Top" Height="40" Background="#0F4378" MouseMove="TitleBar_MouseMove">
    </Grid>

    鼠标移动时间响应:

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

    窗口按钮状态转换:

    这里指的是普通,鼠标浮上,鼠标按下三种状态在同一张图片上,通过图片的偏移来实现转换。

    以最小化按钮为例:

    <Button Name="minBtn" Width="34" FocusVisualStyle="{x:Null}" Click="MinBtn_Click">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="Background">
                    <Setter.Value>
                        <DrawingBrush Stretch="Fill" TileMode="Tile" Viewport="0,0,3,1" Viewbox="0,0,1,1">
                            <DrawingBrush.Drawing>
                                <ImageDrawing ImageSource="res/btn_min.png" Rect="0,0,1,1"/>
                            </DrawingBrush.Drawing>
                        </DrawingBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}">
                            <Grid Background="{TemplateBinding Background}">
                                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background">
                            <Setter.Value>
                                <DrawingBrush Stretch="Fill" TileMode="Tile" Viewport="2,0,3,1" Viewbox="0,0,1,1">
                                    <DrawingBrush.Drawing>
                                        <ImageDrawing ImageSource="res/btn_min.png" Rect="0,0,1,1"/>
                                    </DrawingBrush.Drawing>
                                </DrawingBrush>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Background">
                            <Setter.Value>
                                <DrawingBrush Stretch="Fill" TileMode="Tile" Viewport="1,0,3,1" Viewbox="0,0,1,1">
                                    <DrawingBrush.Drawing>
                                        <ImageDrawing ImageSource="res/btn_min.png" Rect="0,0,1,1"/>
                                    </DrawingBrush.Drawing>
                                </DrawingBrush>
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

    同样实现窗口的最小化和关闭也比较容易:

    private void MinBtn_Click(object sender, RoutedEventArgs e)
    {
        this.WindowState = WindowState.Minimized;
    }
    private void CloseBtn_Click(object sender, RoutedEventArgs e)
    {
        mainWindow.Close();
    }

    原文地址:http://blog.csdn.net/eilotyan/article/details/6532917

  • 相关阅读:
    Python的条件判断和循环
    Python分支和循环结构的练习
    Python的变量和运算符
    Python简介
    Git的使用及网络编程多线程多进程
    函数面向对象编程及文件的读取
    函数模块字符串列表
    python变量运算符分支结构循环结构及例题
    集合
    三级菜单
  • 原文地址:https://www.cnblogs.com/duqingsong/p/3091107.html
Copyright © 2020-2023  润新知