WPF窗体透明控件不透明实例

2020-12-15 15:10:02 浏览数 (1)

目的:遮挡下面的窗体

WPF 窗口去除顶部边框(正宗无边框)

最近在做一个大屏展示视频图片的项目,功能并不复杂,半天的工作量吧,一开始同事采用的Unity3D进行开发,但是里面要播放4K视频,Unity 的短板就是视频的播放了,今晚就要交付了,我一早就来公司,决定用WPF重新开发一版,各项功能都好了,唯独顶部总是显示一条白色的边,已经设置WindowStyle为None了也没用,幸得网上大神提供的资料,终于解决了这个小问题。

XAML内容如下:

代码语言:javascript复制
<Window x:Class="WPF_VideoPlayer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPF_VideoPlayer"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="2880" Loaded="Window_Loaded" WindowChrome.WindowChrome="{DynamicResource WindowChromeKey}" AllowsTransparency="True" WindowStyle="None" ResizeMode="NoResize" WindowState="Maximized" Topmost="True" Background="Black">

    <Window.Resources>
        <WindowChrome x:Key="WindowChromeKey">
            <WindowChrome.ResizeBorderThickness>
                <Thickness>0</Thickness>
            </WindowChrome.ResizeBorderThickness>
        </WindowChrome>
    </Window.Resources>

    <Grid>
    </Grid>
</Window>

具有透明背景且包含不透明控件的WPF窗口

代码语言:javascript复制
<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"
        AllowsTransparency="True" WindowStyle="None">
    <Window.Background>
        <SolidColorBrush Opacity="0.5" Color="White"/>
    </Window.Background>
    <Grid>
        <Button Width="200" Height="50">button</Button>
    </Grid>
</Window>

项目XAML:

代码语言:javascript复制
<Window x:Class="WpfApp5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp5"
        mc:Ignorable="d"
        Title="MainWindow" Height="1080" Width="1920"  WindowChrome.WindowChrome="{DynamicResource WindowChromeKey}" AllowsTransparency="True" WindowStyle="None" ResizeMode="NoResize" WindowState="Maximized" Topmost="True" >
    <Window.Resources>
        <WindowChrome x:Key="WindowChromeKey">
            <WindowChrome.ResizeBorderThickness>
                <Thickness>0</Thickness>
            </WindowChrome.ResizeBorderThickness>
        </WindowChrome>
    </Window.Resources>
    <Window.Background>
        <SolidColorBrush Opacity="0" Color="White"/>
    </Window.Background>
    <!--<Grid ShowGridLines="True">-->
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="50"></RowDefinition>
            <RowDefinition Height="395"></RowDefinition>
            <RowDefinition Height="402"></RowDefinition>
            <RowDefinition Height="186"></RowDefinition>
            <RowDefinition Height="49"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="540"/>
            <ColumnDefinition Width="398"/>
            <ColumnDefinition Width="508"/>
            <ColumnDefinition Width="833"/>
        </Grid.ColumnDefinitions>
        <Button Grid.Row="1" Grid.Column="0" Name="btn1"  Content="实 时 图 像" VerticalContentAlignment="Center" FontSize="40" FontFamily="Microsoft YaHei UI" Foreground="White" Background="#0850a0" Opacity="1"/>
        <Button Grid.Row="1" Grid.Column="1" Grid.RowSpan="2" Name="btn2"  Content="截 取 图 像" Foreground="White" VerticalContentAlignment="Center" FontSize="40" FontFamily="Microsoft YaHei UI" Background="#0850a0" Opacity="1"/>
        <Button Grid.Row="1" Grid.Column="3" Grid.RowSpan="2" Name="btn3"   Background="#0850a0" Opacity="1"/>
        <Button Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="3" Name="btn4"   Background="#0850a0" Opacity="1"/>
        <Button  Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="4" Name="btn5"   Background="#0850a0" Opacity="1"/>
    </Grid>
</Window>

0 人点赞