Member Avatar for nblackburn

I have an application i am currently working on which uses Aero glass in some areas but the areas that don't have aero are shown in black.

I am trying to figure out why that is.

Code:

    using System.Runtime.InteropServices;
    using System.Windows.Interop;
    using System.Windows.Media;
    using System.Windows;
    using System;

    namespace Test
    {
    public partial class MainWindow
    {

        public const int WmDwmcompositionchanged = 0x031E; 

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

        [DllImport("dwmapi.dll")]
        static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMarInset);
        [DllImport("dwmapi.dll")]
        extern static int DwmIsCompositionEnabled(ref int en);

        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg != WmDwmcompositionchanged) return IntPtr.Zero;
            ExtendGlass(this, new Thickness(0,0,120,0));
            handled = true;
            return IntPtr.Zero;
        }

        public static void ExtendGlass(Window window, Thickness thikness)
        {
            try
            {
                var isGlassEnabled = 0;
                DwmIsCompositionEnabled(ref isGlassEnabled);
                if (Environment.OSVersion.Version.Major > 5 && isGlassEnabled > 0)
                {
                    var helper = new WindowInteropHelper(window);
                    var mainWindowSrc = HwndSource.
                        FromHwnd(helper.Handle);
                    if (mainWindowSrc != null)
                        if (mainWindowSrc.CompositionTarget != null)
                            mainWindowSrc.CompositionTarget.BackgroundColor =
                                Colors.Transparent;

                    var margins = new Margins
                    {
                        cxLeftWidth = (int) (thikness.Left),
                        cxRightWidth = (int) (thikness.Right),
                        cyBottomHeight = (int) (thikness.Bottom),
                        cyTopHeight = (int) (thikness.Top)
                    };

                    window.Background = Brushes.Transparent;

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

            }
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            ExtendGlass(this, new Thickness(50,0,0,0));
        }

    }
}

Preview:

http://i.imgur.com/dGmhdRp.png

Any help would be awesome, thanks.

I think it has something to do with the fact that you're trying to set your Window back colour to transparent. This won't work and gives you a black colour instead.

Member Avatar for nblackburn

So how would i fix this?

Give the Window background a different colour?

Member Avatar for nblackburn

Doesn't work as it causes the whole window to become black.

I was talking about this line: window.Background = Brushes.Transparent;
not this line: mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent;

Did you update both? The CompositionTarget is correct and should be transparent, the window.Background call shouldn't as winforms doesn't support transparency that way. I imagine it will be painting it with the default transparency key colour which is probably black. If you want a semi transparent window you will need to set the Opacity of the form, or, set the entire form area to be Aero Glass.

Member Avatar for nblackburn

I have tried playing around with both but none achieve the desired effect. It only works if i make the entire window glass but thats not i intended the application to look. Also this is a WPF application so its not using winforms.

Ahh! Yes okay you were doing it the correct way then.

This works a little differently in WPF. You don't want to edit the background colour of the Window, but rather the first child control under it (Most likely a grid)

You need to set the size of this control appropriately (This should be auto managed by your margins, but if not you can do it in code) and then set the background colour of this control to your desired colour.

I suggest setting your entire client area to AeroGlass. (You can do this by setting new Thickness(-1) when you call ExtendGlass) and then use controls such as Grid, StackPanel or Canvas to get the desired UI client area.

Member Avatar for nblackburn

I understand that but it still doesnt really explain why the window background is black in the first place as it should be the colour specified by the window background property. The code i used to achieve the aero-effect is WPF code as i understand so it should function without problems but it doesnt.

It is functioning as intended. That black space isn't what you know of as your window. The "Window" actually sits on top of this area (The ClientArea). What you're seeing is probably the artefact of DirectX/GDI drawing this area.

If you set your Window background colour to pink, that's why you get a pink area in your Window that doesn't conform to the size of the black area.

If you override the Direct3D default colour you should be able to change it to what you like. This is rather complicated. Setting your layout to the right size is far simpler.

EDIT: I've done some reading around and it seems that the transparency key for aero is black. This may have something to do with it as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.