gpt4 book ai didi

wpf - 如何在WPF中创建半透明表单

转载 作者:行者123 更新时间:2023-12-04 05:34:27 24 4
gpt4 key购买 nike

我必须在WPF中开发一个半透明的窗体,但是控件不应该是透明的。

我尝试了其他类似设置opacity = 0.5的操作,但没有结果。

  • 我知道只有将WindowStyle设置为None时,AllowTransparency才能设置为True,但是我还需要显示Border以及

  • 更新:
    帕夫洛·格拉兹科夫(Pavlo Glazkov),您对此解决方案有何看法
    <Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Opacity="1" AllowsTransparency="True" WindowStyle="None" Background="Transparent">
    <Grid Background="Transparent">
    <Border Margin="2,2,12,34" Name="border1" BorderBrush="Lavender" BorderThickness="5" CornerRadius="20,0,20,0"></Border>
    <Button Height="23" Margin="93,101,110,0" Name="button1" VerticalAlignment="Top" Background="CadetBlue" Foreground="White">Hello WPF</Button>
    <Button Height="24" Margin="0,8,20,0" Name="button2" VerticalAlignment="Top" HorizontalAlignment="Right" Width="21" Click="button2_Click">X</Button>
    </Grid>
    </Window>

    最佳答案

    首先,您需要将AllowTransperency设置为True。然后,您可以将窗口的背景设置为透明(达到所需程度)的笔刷:

    <Window x:Class="MyWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    WindowStyle="None"
    AllowsTransparency="True"
    Background="{DynamicResource WindowBackground}">
    <Window.Resources>
    <SolidColorBrush x:Key="WindowBackground"
    Color="White"
    Opacity="0.5"/>
    </Window.Resources>
    ...
    </Window>

    请注意,只有将 AllowTransperency设置为 True,才能将 WindowStyle设置为 None

    更新:
    如果您不想将 WindowStyle设置为 None并希望保留标准边框和窗口按钮,则可以选择另一种方法,该方法仅适用于具有Windows Aero主题的Windows Vista/7。

    诀窍在于,您可以使用以下代码将“玻璃”区域扩展到整个窗口:
    public static class WindowUtils
    {
    /// <summary>
    /// Extends the glass area into the client area of the window
    /// </summary>
    /// <param name="window">Window to extend the glass on.</param>
    /// <param name="thikness">Thickness of border to extend.</param>
    public static void ExtendGlass(this Window window, Thickness thikness) {
    try {
    int isGlassEnabled = 0;
    Win32.DwmIsCompositionEnabled(ref isGlassEnabled);
    if (Environment.OSVersion.Version.Major > 5 && isGlassEnabled > 0) {
    // Get the window handle
    var helper = new WindowInteropHelper(window);
    var mainWindowSrc = HwndSource.FromHwnd(helper.Handle);

    if (mainWindowSrc != null) {
    if (mainWindowSrc.CompositionTarget != null) {
    mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent;
    }

    // Get the dpi of the screen
    System.Drawing.Graphics desktop =
    System.Drawing.Graphics.FromHwnd(mainWindowSrc.Handle);

    float dpiX = desktop.DpiX / 96;
    float dpiY = desktop.DpiY / 96;

    // Set Margins
    var margins = new MARGINS {
    cxLeftWidth = (int)(thikness.Left * dpiX),
    cxRightWidth = (int)(thikness.Right * dpiX),
    cyBottomHeight = (int)(thikness.Bottom * dpiY),
    cyTopHeight = (int)(thikness.Top * dpiY)
    };

    window.Background = Brushes.Transparent;

    Win32.DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
    }
    }
    else {
    window.Background = SystemColors.WindowBrush;
    }
    }
    catch (DllNotFoundException) {

    }
    }
    }

    public class Win32
    {
    [DllImport("dwmapi.dll")]
    public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);

    [DllImport("dwmapi.dll")]
    public static extern int DwmIsCompositionEnabled(ref int en);

    [DllImport("user32.dll")]
    public static extern bool SetCursorPos(int X, int Y);


    [DllImport("User32", EntryPoint = "ClientToScreen", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)]
    public static extern int ClientToScreen(IntPtr hWnd, [In, Out] POINT pt);
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct MARGINS
    {
    public int cxLeftWidth;
    public int cxRightWidth;
    public int cyTopHeight;
    public int cyBottomHeight;
    }

    [StructLayout(LayoutKind.Sequential)]
    public class POINT
    {
    public int x = 0;
    public int y = 0;
    }

    要将玻璃扩展到整个窗口,您需要在窗口的SizeChanged事件处理程序中调用ExtendGlass扩展方法,并传递一个覆盖整个窗口的 Thickness:
    public MyWindow() {
    InitializeComponent();

    SizeChanged += OnSizeChanged;
    }

    private void OnSizeChanged(object sender, SizeChangedEventArgs e) {
    double horisontalThickness = Width / 2;
    double verticalThickness = Height / 2;

    var glassThickness = new Thickness(horisontalThickness, verticalThickness, horisontalThickness, verticalThickness);

    this.ExtendGlass(glassThickness);
    }

    关于wpf - 如何在WPF中创建半透明表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4922202/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com