wpf中如何将窗口设置为透明

本文介绍了如何使用WPF创建一个带有按钮的透明窗口,并详细解释了XAML代码中关于窗口透明度设置的关键属性,包括WindowStyle、AllowsTransparency和Opacity等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文链接:https://zhidao.baidu.com/question/428786490350231092.html
XAML代码:

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        WindowStyle="None" AllowsTransparency="True" Opacity="0.5">
    <Grid>
        <Button Height="25" Width="80" Content="关闭" VerticalAlignment="Center" HorizontalAlignment="Center" Click="Button_Click"></Button>
    </Grid>
</Window>

后台代码:

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }

注意:
(1)只有将Window设置为无边框(WindowStyle=“None”)时,才可设置窗口的透明度;否则,系统会报错。
(2)Window的AllowsTransparency必须设置为true.
[trænsˈpærənsi]
n. 透明度; 透明; 透明性; 透明的东西;
(3)透明度属性Opacity=1时,窗口不透明;Opacity=0时,窗口全透明。
[əʊˈpæsəti]
n. 不透明性; 费解; 难懂; 模糊;

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值