怎样做? WPF 窗口 - 最大化,不调整大小/移动

发布于 2024-09-11 19:33:46 字数 314 浏览 6 评论 0原文

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

我尝试将 WindowState="Maximized" 和 ResizeMode="CanMinimize" 放在一起,但是当窗口打开时,它会覆盖任务栏(我不想要它)。

我有一个 WndProc 挂钩,可以取消 SC_MOVE 和 SC_SIZE。 我还可以使用 WndProc 中的条件进行此控制,例如“如果命令是恢复并且最小化,则恢复,否则,阻止”等等。

但我的观点是我们是否有其他方法可以做到这一点。 感谢大家的阅读 =)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

抱猫软卧 2024-09-18 19:33:46

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

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

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>
凉世弥音 2024-09-18 19:33:46
    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;
    }
    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;
    }
星軌x 2024-09-18 19:33:46
WindowState="Maximized"
ResizeMode="NoResize"
WindowStyle="None"

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

访问WindowStyle="None"一些问题

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

椒妓 2024-09-18 19:33:46

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文