none
Semi-transparent form background RRS feed

  • Question

  • How can i get a semi-transparent background for my form? Something like this @3:07 youtube.com/watch?v=EnVzC6-eoro.
    • Edited by sqrt505 Saturday, April 27, 2013 3:33 PM
    • Moved by CoolDadTx Wednesday, May 1, 2013 2:13 PM Winforms related?
    Saturday, April 27, 2013 3:32 PM

Answers

  • Hi sqrt505,

    You can create a semi-transparent form easily in WPF. If you need to create semi-transparent form in Winform, you can consider combine two forms into one like the sample below.

    public partial class Form1 : Form
        {
            Form2 f2;
            public Form1()
            {
                InitializeComponent();
                this.BackColor = Color.Red;
                this.TransparencyKey = Color.Red;
                this.StartPosition = FormStartPosition.Manual;
                this.Location = new Point(200, 200);
                f2 = new Form2();
                f2.StartPosition = FormStartPosition.Manual;
                f2.Location = new Point(200, 200);
                f2.Show();
    
                this.LocationChanged += new EventHandler(Form1_LocationChanged);
                this.SizeChanged += new EventHandler(Form1_SizeChanged);
                this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
            }
    
            void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                f2.Close();
            }
    
            void Form1_SizeChanged(object sender, EventArgs e)
            {
                f2.Size = this.Size;
            }
    
            void Form1_LocationChanged(object sender, EventArgs e)
            {
                f2.Location = this.Location;
            }
    
          
        }

     public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
                this.BackColor = Color.FromArgb(64, 64, 64);
                this.Opacity = 0.7;
            }
    
           
        }

    Good day.


    Bob Shen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 1, 2013 3:19 AM

All replies

  • If you want to use the Aero blur then you can use the DwmEnableBlurBehindWindow api. Here's an example derived Window that utilizes this.
    public class BlurWindow : Window
    {
        #region Constants
    
        private const int WM_DWMCOMPOSITIONCHANGED = 0x031E;
        private const int DWM_BB_ENABLE = 0x1; 
    
        #endregion //Constants
    
        #region Structures
        [StructLayout( LayoutKind.Sequential )]
        private struct DWM_BLURBEHIND
        {
            public int dwFlags;
            public bool fEnable;
            public IntPtr hRgnBlur;
            public bool fTransitionOnMaximized;
        }
    
        [StructLayout( LayoutKind.Sequential )]
        private struct MARGINS
        {
            public int cxLeftWidth;
            public int cxRightWidth;
            public int cyTopHeight;
            public int cyBottomHeight;
        } 
        #endregion //Structures
    
        #region APIs
    
        [DllImport( "dwmapi.dll", PreserveSig = false )]
        private static extern void DwmEnableBlurBehindWindow(IntPtr hwnd, ref DWM_BLURBEHIND blurBehind);
    
        [DllImport( "dwmapi.dll" )]
        private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMargins);
    
        [DllImport( "dwmapi.dll", PreserveSig = false )]
        private static extern bool DwmIsCompositionEnabled(); 
    
        #endregion //APIs
    
        #region Constructor
        public BlurWindow()
        {
            this.WindowStyle = System.Windows.WindowStyle.None;
            this.ResizeMode = System.Windows.ResizeMode.NoResize;
            this.Background = Brushes.Transparent;
        } 
        #endregion //Constructor
    
        #region Base class overrides
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized( e );
    
            if ( Environment.OSVersion.Version.Major >= 6 )
            {
                var hwnd = new WindowInteropHelper( this ).Handle;
                var hs = HwndSource.FromHwnd( hwnd );
                hs.CompositionTarget.BackgroundColor = Colors.Transparent;
    
                hs.AddHook( new HwndSourceHook( this.WndProc ) );
                this.InitializeGlass( hwnd );
            }
        } 
        #endregion //Base class overrides
    
        #region Methods
    
        #region InitializeGlass
        private void InitializeGlass(IntPtr hwnd)
        {
            if ( !DwmIsCompositionEnabled() )
                return;
    
            // fill the background with glass
            var margins = new MARGINS();
            margins.cxLeftWidth = margins.cxRightWidth = margins.cyBottomHeight = margins.cyTopHeight = -1;
            DwmExtendFrameIntoClientArea( hwnd, ref margins );
    
            // initialize blur for the window
            DWM_BLURBEHIND bbh = new DWM_BLURBEHIND();
            bbh.fEnable = true;
            bbh.dwFlags = DWM_BB_ENABLE;
            DwmEnableBlurBehindWindow( hwnd, ref bbh );
        }
        #endregion //InitializeGlass
    
        #region WndProc
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if ( msg == WM_DWMCOMPOSITIONCHANGED )
            {
                this.InitializeGlass( hwnd );
                handled = false;
            }
    
            return IntPtr.Zero;
        } 
        #endregion //WndProc 
    
        #endregion //Methods
    }

    And here's a snippet of using the BlurWindow.

    var w = new BlurWindow();
    w.Width = 100;
    w.Height = 100;
    w.MouseLeftButtonDown += (s1, e1) => {
        ((Window)s1).DragMove();
        e1.Handled = true;
    };
    w.Background = new SolidColorBrush( Color.FromArgb( 75, 255, 0, 0 ) );
    w.Show();
    Saturday, April 27, 2013 3:42 PM
  • Thanks for your answer but Aero doesn't work on Windows 8.

    Ps. The application from the video works fine on Windows 8.

    Saturday, April 27, 2013 7:03 PM
  • Hi,

    The Form has a property called "Opacity". Try to set it to 50%.

    Saturday, April 27, 2013 7:09 PM
  • Yes, but the Opacity property is for the entire form - for the objects inside form too: labels, textboxes, etc. I want only for the form background.
    Saturday, April 27, 2013 7:33 PM
  • Hi sqrt505,

    You can create a semi-transparent form easily in WPF. If you need to create semi-transparent form in Winform, you can consider combine two forms into one like the sample below.

    public partial class Form1 : Form
        {
            Form2 f2;
            public Form1()
            {
                InitializeComponent();
                this.BackColor = Color.Red;
                this.TransparencyKey = Color.Red;
                this.StartPosition = FormStartPosition.Manual;
                this.Location = new Point(200, 200);
                f2 = new Form2();
                f2.StartPosition = FormStartPosition.Manual;
                f2.Location = new Point(200, 200);
                f2.Show();
    
                this.LocationChanged += new EventHandler(Form1_LocationChanged);
                this.SizeChanged += new EventHandler(Form1_SizeChanged);
                this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
            }
    
            void Form1_FormClosed(object sender, FormClosedEventArgs e)
            {
                f2.Close();
            }
    
            void Form1_SizeChanged(object sender, EventArgs e)
            {
                f2.Size = this.Size;
            }
    
            void Form1_LocationChanged(object sender, EventArgs e)
            {
                f2.Location = this.Location;
            }
    
          
        }

     public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
                this.BackColor = Color.FromArgb(64, 64, 64);
                this.Opacity = 0.7;
            }
    
           
        }

    Good day.


    Bob Shen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 1, 2013 3:19 AM
  • Hi Bob Shen,

    For Windows Form, I like your way on how you make one transparent form overlay one solid form. But I have a question here.

    1. We will have two forms in total right? 

    That means when I press Alt-Tab I am able to switch to any form to display to the user at a time. What I want to do for my project is to create a gray semi-transparent screen overlay with the original form and when I press Alt-Tab it will not switch to either only the semi-transparent color screen or the full-original form. What I want to do is to merge the two forms into one window form so that when I press Alt-Tab it will not toggle either screen to display but both.


    Tuesday, July 24, 2018 8:29 AM