Search code examples
.netwpfxamlpinvoke

WPF Form disapears whenever I add `NoResize` property


I am trying to use WPF to create a dock application in the style of the Dell Dock and it is going well so far.

My WPF form appears like so on my screen:

John Dock

The XAML for my form is below:

<Window x:Class="WpfApplication7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="130" Width="525" Loaded="Window_Loaded" Background="Transparent" WindowStyle="None">
    <Grid>
    </Grid>
</Window>

I found a tutorial on the Microsoft website to add the Aero Glass effect. With this in mind, my code is below:

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;

namespace WpfApplication7
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            MakeTransparent();

            Topmost = true;


            var screenWidth = SystemInformation.PrimaryMonitorSize.Width;
            var startingPoint = (screenWidth / 2) - (Width / 2);

            Top = 2;
            Left = startingPoint;
        }

        private void MakeTransparent()
        {
            IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle;
            HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
            mainWindowSrc.CompositionTarget.BackgroundColor = System.Windows.Media.Color.FromArgb(0, 0, 0, 0);

            Graphics desktop = Graphics.FromHwnd(mainWindowPtr);
            float DesktopDpiX = desktop.DpiX;
            float DesktopDpiY = desktop.DpiY;

            MARGINS margins = new MARGINS()
            {
                cxLeftWidth = 0,
                cxRightWidth = Convert.ToInt32(Width) * Convert.ToInt32(Width),
                cyTopHeight = 0,
                cyBottomHeight = Convert.ToInt32(Height) * Convert.ToInt32(Height)
            };

            int hr = 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);
    }
}

So far so good. However, my form is resizable which I don't want so I set the ResizeMode property to NoResize and whenever I do this my form is nowhere to be found when launched.

Why does this happen?

I tried to get around this by resseting the form size on the SizeChanged event, but this only if you try to resize from the right or bottom of the form, if you try to resize from the top of left, it moves the form which I don't want either.

Thanks


Solution

  • What is happening is since you are trying to extend your AeroBlur throughout the entire window, once you get rid of all window chrome it will not work, and since your Windows background color is Transparent it disappears on you. It does not appear that there is an way to extend Aero Glass easily without some Chrome which by allowing the form to be resized, gives you. The only link I could that mentions AeroBlur and Chromeless windows is this