4
votes

I am creating a WPF window with a custom chrome, so I setted ResizeMode="NoResize" and WindowStyle="None" to implement my own chrome. However, there is an issue while maximizing the borderless window: it takes the whole screen.

I found the following trick to fix part of the issue: http://chiafong6799.wordpress.com/2009/02/05/maximizing-a-borderlessno-caption-window/

This successfully restrain the window size to prevent from covering a taskbar. However, if the user have his taskbar positionned at the left or at the top, this won't work, as the window is at position 0,0.

Is there any way to retrieve more accurately the available area, or to query the user taskbar's position so I can position the maximized window accordingly?

2

2 Answers

5
votes

I had a quick play around and it seems that setting the Windows Left and Top properties is ignored when setting WindowState.Maximized with a borderless form.

One workaround would be to ignore the WindowState functions and create your own Maximize/Restore functions

Rough example.

public partial class MainWindow : Window
{
    private Rect _restoreLocation;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void MaximizeWindow()
    {
        _restoreLocation = new Rect { Width = Width, Height = Height, X = Left, Y = Top };
        System.Windows.Forms.Screen currentScreen;
        currentScreen = System.Windows.Forms.Screen.FromPoint(System.Windows.Forms.Cursor.Position);
        Height = currentScreen.WorkingArea.Height;
        Width = currentScreen.WorkingArea.Width;
        Left = currentScreen.WorkingArea.X;
        Top = currentScreen.WorkingArea.Y;
    }

    private void Restore()
    {
        Height = _restoreLocation.Height;
        Width = _restoreLocation.Width;
        Left = _restoreLocation.X;
        Top = _restoreLocation.Y;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        MaximizeWindow();
    }

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        Restore();
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            DragMove();
        }
        base.OnMouseMove(e);
    }
}

Xaml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="74.608" Width="171.708" ResizeMode="NoResize" WindowStyle="None">
    <Grid>
        <Button Content="Max" HorizontalAlignment="Left" Margin="0,29,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
        <Button Content="Restore" HorizontalAlignment="Left" Margin="80,29,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_2"/>
    </Grid>
</Window>

Obviously you will want to clean this code up, but it seems to work wherever the Taskbar is located, However you may need to add some logic to get the correct Left, Top if the users font DPI is larger than 100%

2
votes

Another way to do this is by handling the WM_GETMINMAXINFO Win32 message. The code here shows how to do that.

Note that there are a few things that I would do differently, such as returning IntPtr.Zero instead of (System.IntPtr)0 in WindowProc and making MONITOR_DEFAULTTONEAREST a constant. But that's just coding style changes, and doesn't affect the net result.

Also make sure to pay attention to the update where the WindowProc is hooked during the SourceInitialized event instead of OnApplyTemplate. That's the better place to do it. If you're implementing a class derived from Window, then another option is to override OnSourceInitialized to hook the WindowProc instead of attaching to the event. That's what I normally do.