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

本文介绍如何使用WPF实现窗口的拖动、最小化及关闭功能。通过自定义标题栏和按钮状态转换,实现了鼠标拖动窗口、按钮的鼠标悬停效果及不同状态间的切换。此外还提供了具体的代码示例。
其实这一部分的资料网上很多,也看了很多的例子,不过大部分都不是自己想要的结果,所以就综合网上的资料整理了一下,放在这里吧,虽然现在放弃 WPF了,不过以后还是想接着学一下,先放着吧就。

  标题栏的移动:

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

< 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();
}

本文来自eilotyan的博客,原文地址:http://blog.youkuaiyun.com/eilotyan/archive/2011/06/08/6532917.aspx

转载于:https://www.cnblogs.com/mane/archive/2011/06/17/2083836.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值