How to? WPF Window - Maximized, No Resize/Move
Asked Answered
K

4

14

I'm trying to make a WPF window that opens already maximized, with no resize/move (in systemmenu, nor in border). It should be maximized all the time, except when the user minimize it.

I tried to put WindowState="Maximized" and ResizeMode="CanMinimize", but when window opens, it covers the task bar (i don't want it).

I have an hook to WndProc that cancels the SC_MOVE and SC_SIZE. I also can make this control with conditions in WndProc like "if command is restore and is minimized, restore, else, block" and so on.

But my point is if we have another way to make it. Thankz for read guys =)

Klagenfurt answered 23/7, 2010 at 13:8 Comment(3)
This is a common problem found in all Windows Application Frameworks. App Frameworks are great, they allow us to write applications very quickly. The problem is that they cannot cover every possible scenario and because their purpose is to hide the nitty, gritty details of the underlying system, when we find we need to have non-standard behavior we have to dig in and write "ugly" code.Cottingham
You want a window that can only be maximized or minimized with no normal state that can be resized/moved? but yet not be full screen?Boggs
@John Gardner, It's the exactly that i want.Klagenfurt
L
27

It is necessary to write WindowState="Maximized" ResizeMode="NoResize" in xaml of your window:

<Window x:Class="Miscellaneous.EditForm"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Edit Form" WindowState="Maximized" ResizeMode="NoResize"></Window>
Ludwig answered 28/3, 2013 at 12:44 Comment(1)
Setting the WindowState="Maximized" is incompatible with the SplashScreen feature (your main window will hide the splash screen). And if you don't start Maximized, then ResizeMode="NoResize" will prevent the window from being Maximized.Mussorgsky
R
3
    public Window1()
    {
         InitializeComponent();

          this.SourceInitialized += Window1_SourceInitialized;
    }

    private void Window1_SourceInitialized(object sender, EventArgs e)
    {
        WindowInteropHelper helper = new WindowInteropHelper(this);
        HwndSource source = HwndSource.FromHwnd(helper.Handle);
        source.AddHook(WndProc);
    }

    const int WM_SYSCOMMAND = 0x0112;
    const int SC_MOVE = 0xF010;

    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {

        switch (msg)
        {
            case WM_SYSCOMMAND:
                int command = wParam.ToInt32() & 0xfff0;
                if (command == SC_MOVE)
                {
                    handled = true;
                }
                break;
            default:
                break;
        }
        return IntPtr.Zero;
    }
Roxyroy answered 26/1, 2013 at 8:1 Comment(1)
This code will just prevent the window from being moved or maximized. If instead you use SC_SIZE it allows the window to be maximized and minimized, but not resized. const int SC_SIZE = 0xF000; In my window xaml I use WindowState="Minimized" and ResizeMode="CanResize"Mussorgsky
D
3
WindowState="Maximized"
ResizeMode="NoResize"
WindowStyle="None"

WindowStyle="None" do what you want, but... you lose the window title, close button and have other problems.

Visit WindowStyle="None" some problems

Dell answered 11/1, 2016 at 13:18 Comment(0)
D
1

As Tergiver pointed out, this is not possible in a purely WPF manner. You have to use P/Invoke. As for why the window covers the taskbar when it opens, I think you are messing up some required calls by canceling SC_MOVE and SC_SIZE. Maybe you should cancel those calls after the window has been loaded.

Discovert answered 31/7, 2010 at 20:23 Comment(1)
PInvoke????? How about this in window constructor? // Prevent the window overlapping the taskbar this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight; this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;Photolysis

© 2022 - 2024 — McMap. All rights reserved.