其实这一部分的资料网上很多,也看了很多的例子,不过大部分都不是自己想要的结果,所以就综合网上的资料整理了一下,放在这里吧,虽然现在放弃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(); }