C# WPF transparent window with a border

前端 未结 2 823
孤街浪徒
孤街浪徒 2021-02-08 17:57

I would like to make a simple application which is transparent but retains the \'normal\' borders, close button, minimize and maximize button.

I know how to make the win

相关标签:
2条回答
  • 2021-02-08 18:10

    I would first look at the (a)lpha setting in the rgb(a) color of the background color. The alpha setting sets the opacity of the object color.

    Although, I notice that as I'm posting this, there is another post before mine that looks more concise and would probably be more appropriate for you.

    0 讨论(0)
  • 2021-02-08 18:29

    I threw together a quick TransparencyConverter class based on this tutorial on Microsoft.com you can use for this purpose:

    using System;
    using System.Runtime.InteropServices;
    using System.Windows;
    using System.Windows.Interop;
    
    namespace WpfApplication2
    {
        class TransparencyConverter
        {
            private readonly Window _window;
    
            public TransparencyConverter(Window window)
            {
                _window = window;
            }
    
            public void MakeTransparent()
            {
                var mainWindowPtr = new WindowInteropHelper(_window).Handle;
                var mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
                if (mainWindowSrc != null)
                    if (mainWindowSrc.CompositionTarget != null)
                        mainWindowSrc.CompositionTarget.BackgroundColor = System.Windows.Media.Color.FromArgb(0, 0, 0, 0);
    
                var margins = new Margins
                {
                    cxLeftWidth = 0,
                    cxRightWidth = Convert.ToInt32(_window.Width) * Convert.ToInt32(_window.Width),
                    cyTopHeight = 0,
                    cyBottomHeight = Convert.ToInt32(_window.Height) * Convert.ToInt32(_window.Height)
                };
    
                if (mainWindowSrc != null) DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
            }
    
            [StructLayout(LayoutKind.Sequential)]
            public struct Margins
            {
                public int cxLeftWidth;
                public int cxRightWidth;
                public int cyTopHeight;
                public int cyBottomHeight;
            }
    
            [DllImport("DwmApi.dll")]
            public static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref Margins pMarInset);
        }
    }
    

    Once you have this in, add the Transparent Background attribute to your XAML and subscribe to the Window_Loaded event and call the MakeTransparent method:

    <Window etc etc Background="Transparent" Loaded="Window_Loaded">
    
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        var transparencyConverter = new TransparencyConverter(this);
        transparencyConverter.MakeTransparent();
    }
    

    A screenshot is below:

    Screenshot

    0 讨论(0)
提交回复
热议问题