开发者

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

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 =)


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>


    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;
    }


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜