如何?WPF 窗口 - 最大化,不调整大小/移动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3318385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:57:40  来源:igfitidea点击:

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

wpfwindowwindow-resizemaximize-window

提问by Thieres Tembra

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.

我正在尝试制作一个已经最大化打开的 WPF 窗口,没有调整大小/移动(在系统菜单中,也没有在边框中)。它应该一直最大化,除非用户最小化它。

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

我试图把 WindowState="Maximized" 和 ResizeMode="CanMinimize",但是当窗口打开时,它覆盖了任务栏(我不想要它)。

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.

我有一个到 WndProc 的钩子,它取消了 SC_MOVE 和 SC_SIZE。我还可以使用 WndProc 中的条件进行此控制,例如“如果命令被还原并被最小化,还原,否则,阻止”等等。

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

但我的观点是我们是否有另一种方式来实现。感谢阅读伙计们 =)

回答by StepUp

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

有必要WindowState="Maximized" ResizeMode="NoResize"在您的窗口的 xaml 中写入 :

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

回答by Bindya

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

回答by Henryk Budzinski

WindowState="Maximized"
ResizeMode="NoResize"
WindowStyle="None"

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

WindowStyle="None" 做你想做的事,但是......你失去了窗口标题、关闭按钮和其他问题。

Visit WindowStyle="None" some problems

访问WindowStyle="None"的一些问题

回答by Marko

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.

正如 Tergiver 指出的那样,这在纯粹的 WPF 方式中是不可能的。您必须使用 P/Invoke。至于为什么窗口打开时会覆盖任务栏,我认为您通过取消SC_MOVE和SC_SIZE来搞乱一些必需的调用。也许您应该在窗口加载后取消这些调用。