定义几个按钮,和导航栏,都给个Name
<materialDesign:ColorZone
Padding="16" x:Name="ColorZone"/>
<Button x:Name="BtnMin" Content="—" Style="{StaticResource MaterialDesignFlatMidBgButton}"></Button>
<Button x:Name="BtnMax" Content="" Style="{StaticResource MaterialDesignFlatMidBgButton}"></Button>
<Button x:Name="BtnClose" Content="" Style="{StaticResource MaterialDesignFlatMidBgButton}"></Button>
最小化
this.WindowState = WindowState.Minimized;
//最小化按钮事件
BtnMin.Click += (s, e) =>
{
this.WindowState = WindowState.Minimized;
};
最大化,还原正常窗口
最大化
this.WindowState = WindowState.Maximized;
还原正常窗口
this.WindowState = WindowState.Normal;
最大化之前判断当前窗口是否已经是最大化,如果是最大化,窗口恢复正常,否则最大化
判断窗口是否最大化:
if (this.WindowState == WindowState.Maximized)
BtnMax.Click += (s, e) =>
{
//判断是否以及最大化,最大化就还原窗口,否则最大化
if (this.WindowState == WindowState.Maximized)
this.WindowState = WindowState.Normal;
else
this.WindowState = WindowState.Maximized;
};
关闭窗口
this.Close();
//关闭按钮事件
BtnClose.Click += (s, e) =>
{
this.Close();
};
拖动窗口
this.DragMove();
ColorZone.MouseMove += (s, e) =>
{
//判断鼠标是此元素否在拖动,如果是则开启拖动
if (e.LeftButton == MouseButtonState.Pressed)
this.DragMove();
};
双击事件,用于放大,还原窗口
ColorZone.MouseDoubleClick += (s, e) =>
{
//判断窗口是否正常,正常则放大,否则还原正常窗口
if (this.WindowState == WindowState.Normal)
this.WindowState = WindowState.Maximized;
else
this.WindowState = WindowState.Normal;
};