[WPF] Window 屬性:透明背景的應用程式

發表日期:
2022.05.31
/
分類:
Dev
設定 Window 屬性 為了讓視窗背景能透明,首先須在最上層的 Window 中定義 AllowsTransparency 與 WindowStyle 兩項屬性。當 AllowsTransparency 為 True 時,WindowStyle 必須為 None,否則無法編譯。 添加

設定 Window 屬性

為了讓視窗背景能透明,首先須在最上層的 Window 中定義 AllowsTransparency 與 WindowStyle 兩項屬性。當 AllowsTransparency 為 True 時,WindowStyle 必須為 None,否則無法編譯。

添加位置如第九行:

<Window x:Class="WPF_TransparentBackground_Tutorial.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_TransparentBackground_Tutorial"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        AllowsTransparency="True" WindowStyle="None">
    <Grid>
        
    </Grid>
</Window>

設定 Window.Background 屬性

手動加入 Window.Background 元件,並在其中加入含有透明度的筆刷即完成透明背景應用程式的建置。例如 Opacity 為 0 的 SolidColorBrush,會讓整個應用程式變成完全透明:

<Window.Background>
    <SolidColorBrush Opacity="0"></SolidColorBrush>
</Window.Background>

也可以使用 ImageBrush 來放含有透明背景的圖片:

<Window.Background>
    <ImageBrush ImageSource="/Background.png"></ImageBrush>
</Window.Background>

假設我們直接在 Window 設定透明度,會讓其他元件也變透明,包含按鈕、文字等,所以才需要將他單獨拉出來設定。

comments powered by Disqus