最顶层显示、窗体最大最小化及button按钮背景渐变

运行后效果如下:

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

在VBA编程中隐藏最大化和最小化窗口按钮,可以使用在表单的 Initialize 事件中使用以下代码: Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, _ ByVal lpWindowName As String) As Long Private Declare Function GetSystemMenu Lib "user32" _ (ByVal hwnd As Long, _ ByVal bRevert As Long) As Long Private Declare Function RemoveMenu Lib "user32" _ (ByVal hMenu As Long, _ ByVal nPosition As Long, _ ByVal wFlags As Long) As Long Private Const MF_BYPOSITION = &H400& Private Const MF_REMOVE = &H1000& Private Sub UserForm_Initialize() Dim hwnd As Long Dim hMenu As Long hwnd = FindWindow("ThunderDFrame", Me.Caption) hMenu = GetSystemMenu(hwnd, False) Call RemoveMenu(hMenu, 6, MF_BYPOSITION) Call RemoveMenu(hMenu, 5, MF_BYPOSITION) End Sub 解释下代码的具体含义: 1. FindWindow: 它在Windows中寻找具有指定的类名和窗口名的顶层窗口。 2. GetSystemMenu:获取窗口菜单句柄。例如,这能用来得到窗口的“控制”菜单,我们可以向其中加入一些自定义的菜单项。 3. RemoveMenu: 删除具有指定位置和标志的菜单项,该函数要求我们给它提供菜单句柄、要删除的位置以及标志。 菜单项位置可以通过Windows API文档来确定,但是,Windows不总是一致的,不同的操作系统版本也许就不同。 上述代码将最大化和最小化按钮在窗口的菜单中移除。有了这些代码,你可以在你自己的VBA项目中应用,也可以改写代码应用到Excel、Access 或者 PowerPoint 等其他 VBA项目中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值