在WPF中启用最大化,最小化和还原窗口(禁用手动调整大小)

时间:2022-04-25 07:16:48

I need to enable the following on my application (C# WPF application):

我需要在我的应用程序(C#WPF应用程序)上启用以下内容:

  1. Have normal size of 1024*768
  2. 正常尺寸为1024 * 768

  3. The user can maximize the application
  4. 用户可以最大化应用程序

  5. The user can minimize the application
  6. 用户可以最小化应用程序

  7. The user can restore the application (1024*768)
  8. 用户可以恢复应用程序(1024 * 768)

  9. The user cannot manually resize the application by draging its border.
  10. 用户无法通过拖动边框手动调整应用程序的大小。

There isn't any ResizeMode the fulfills all of those requirements. Is there any way to do do?

没有任何ResizeMode满足所有这些要求。有什么办法吗?

2 个解决方案

#1


6  

I've finally found a relatively decent solution.

我终于找到了一个相对不错的解决方案。

The idea is to overide the OnStateChanged event of the window, cancel the Min/Max constraints and refresh it.

我们的想法是覆盖窗口的OnStateChanged事件,取消Min / Max约束并刷新它。

If the window is not maximized, we simply apply back the Min/Max constraints

如果窗口没有最大化,我们只需应用最小/最大约束

    protected override void OnStateChanged(EventArgs e)
    {
        if (WindowState == WindowState.Maximized)
        {
            MinWidth = 0;
            MinHeight = 0;
            MaxWidth = int.MaxValue;
            MaxHeight = int.MaxValue;

            if (!m_isDuringMaximizing)
            {
                m_isDuringMaximizing = true;
                WindowState = WindowState.Normal;
                WindowState = WindowState.Maximized;
                m_isDuringMaximizing = false;
            }
        }
        else if (!m_isDuringMaximizing)
        {
            MinWidth = 1024;
            MinHeight = 768;
            MaxWidth = 1024;
            MaxHeight = 768;
        }

        base.OnStateChanged(e);
    }

#2


0  

You can listen to the Window.SizeChanged event, and inside your handler manually set Width and Height back to 1027 and 768. It still allows the user to drag the window's edges to resize, but then the window returns to it's set size. The drawback of this is that the window has a "seizure" whenever the user tries resizing--not the prettiest thing to see. Minimize and Maximize work as normal.

您可以侦听Window.SizeChanged事件,并在处理程序内手动将Width和Height设置回1027和768.它仍允许用户拖动窗口的边缘以调整大小,但随后窗口将返回其设置大小。这样做的缺点是每当用户尝试调整大小时窗口都会出现“占用” - 这不是最令人讨厌的东西。最大限度地减少工作并最大限度地发挥作用。

#1


6  

I've finally found a relatively decent solution.

我终于找到了一个相对不错的解决方案。

The idea is to overide the OnStateChanged event of the window, cancel the Min/Max constraints and refresh it.

我们的想法是覆盖窗口的OnStateChanged事件,取消Min / Max约束并刷新它。

If the window is not maximized, we simply apply back the Min/Max constraints

如果窗口没有最大化,我们只需应用最小/最大约束

    protected override void OnStateChanged(EventArgs e)
    {
        if (WindowState == WindowState.Maximized)
        {
            MinWidth = 0;
            MinHeight = 0;
            MaxWidth = int.MaxValue;
            MaxHeight = int.MaxValue;

            if (!m_isDuringMaximizing)
            {
                m_isDuringMaximizing = true;
                WindowState = WindowState.Normal;
                WindowState = WindowState.Maximized;
                m_isDuringMaximizing = false;
            }
        }
        else if (!m_isDuringMaximizing)
        {
            MinWidth = 1024;
            MinHeight = 768;
            MaxWidth = 1024;
            MaxHeight = 768;
        }

        base.OnStateChanged(e);
    }

#2


0  

You can listen to the Window.SizeChanged event, and inside your handler manually set Width and Height back to 1027 and 768. It still allows the user to drag the window's edges to resize, but then the window returns to it's set size. The drawback of this is that the window has a "seizure" whenever the user tries resizing--not the prettiest thing to see. Minimize and Maximize work as normal.

您可以侦听Window.SizeChanged事件,并在处理程序内手动将Width和Height设置回1027和768.它仍允许用户拖动窗口的边缘以调整大小,但随后窗口将返回其设置大小。这样做的缺点是每当用户尝试调整大小时窗口都会出现“占用” - 这不是最令人讨厌的东西。最大限度地减少工作并最大限度地发挥作用。