运行后效果如下:
1)点击“允许以最顶层方式显示窗口”,将会使窗体显示在任何其它窗体的最上面(顶层)是通过设置window窗口类的Topmost属性实现的,点击“禁止以最顶层方式显示窗口”将会使窗体在获得焦点后才能在其它窗口最上面显示
2)最大化,最小化,还原窗口,主要是通过对WindowState的设置来实现的。
xaml的代码:
<Window x:Class="Wpf5.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="600" Width="300">
<Window.Resources>
<!--渐变-->
<LinearGradientBrush x:Key="MyGradiend" StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="AliceBlue" Offset="0.0"></GradientStop>
<GradientStop Color="Yellow" Offset="0.5"></GradientStop>
<GradientStop Color="AliceBlue" Offset="1"></GradientStop>
</LinearGradientBrush>
<LinearGradientBrush x:Key="MyGradiend1" StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="White" Offset="0.0"></GradientStop>
<GradientStop Color="DarkOrange" Offset="0.5"></GradientStop>
</LinearGradientBrush>
</Window.Resources>
<Grid ShowGridLines="False">
<!--将Grid划分成七行-->
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<!--按钮设置-->
<Button Name="button1" Foreground="YellowGreen" FontSize="15" BorderBrush="Green" BorderThickness="2" FontFamily="楷体" Grid.Row="0" Grid.RowSpan="1" Width="200" Height="40" VerticalAlignment="Center" Content="允许以最顶层方式显示窗口" Click="button1_Click"></Button>
<Button Name="button2" Foreground="YellowGreen" FontSize="15" BorderBrush="Green" BorderThickness="2" FontFamily="楷体" Grid.Row="1" Grid.RowSpan="1" Width="200" Height="40" VerticalAlignment="Center" Content="禁止以最顶层方式显示窗口" Click="button2_Click"></Button>
<Button Name="button3" Foreground="Black" FontSize="15" BorderBrush="Red" BorderThickness="2" FontFamily="楷体" Grid.Row="2" Grid.RowSpan="1" Width="150" Height="40" VerticalAlignment="Center" Content="最大化显示窗口" Click="button3_Click"></Button>
<Button Name="button4" Foreground="Black" FontSize="15" BorderBrush="Red" BorderThickness="2" FontFamily="楷体" Grid.Row="3" Grid.RowSpan="1" Width="150" Height="40" VerticalAlignment="Center" Content="最小化显示窗口" Click="button4_Click"></Button>
<Button Name="button5" Foreground="Black" FontSize="15" BorderBrush="Red" BorderThickness="2" FontFamily="楷体" Grid.Row="4" Grid.RowSpan="1" Width="150" Height="40" VerticalAlignment="Center" Content="还原窗口" Click="button5_Click"></Button>
<Button Name="button6" Foreground="Black" FontSize="15" BorderBrush="Red" BorderThickness="2" FontFamily="楷体" Grid.Row="5" Grid.RowSpan="1" Width="150" Height="40" VerticalAlignment="Center" Content="关闭窗口" Click="button6_Click"></Button>
</Grid>
</Window>
c#代码:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
/*设置按钮的背景色*/
button1.Background = (Brush)TryFindResource("MyGradiend");
button2.Background = (Brush)TryFindResource("MyGradiend");
button3.Background = (Brush)TryFindResource("MyGradiend1");
button4.Background = (Brush)TryFindResource("MyGradiend1");
button5.Background = (Brush)TryFindResource("MyGradiend1");
button6.Background = (Brush)TryFindResource("MyGradiend1");
}
private void button1_Click(object sender, RoutedEventArgs e)
{
this.Topmost = true;//窗口最顶层显示
}
private void button2_Click(object sender, RoutedEventArgs e)
{
this.Topmost = false;//窗口获得焦点后才能在最顶层显示
}
private void button3_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Maximized;//窗口最大化
}
private void button4_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;//窗口最小化
}
private void button5_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Normal;//窗口还原
}
private void button6_Click(object sender, RoutedEventArgs e)
{
this.Close();//关闭窗口
}
}
来自:http://hi.baidu.com/oztecyunjcdejxs/item/296170ffa84be4cb531c26bf