简体   繁体   English

以编程方式最大化窗口并防止用户更改窗口状态

[英]Maximize a window programmatically and prevent the user from changing the windows state

How do I maximize a window programmatically so that it cannot be resized once it reaches the maximized state (for example, maximize Internet Explorer and see it)?如何以编程方式最大化窗口,使其在达到最大化状态后无法调整大小(例如,最大化 Internet Explorer 并查看它)?

I set FormWindowState property as我将 FormWindowState 属性设置为

this.WindowState = FormWindowState.Maximized;
this.MaximizedBounds = (x,y);

but it doesn't work.但它不起作用。 How do I do this?我该怎么做呢?

The window I want to maximize is a window in my application.我想最大化的窗口是我的应用程序中的一个窗口。

When your form is maximized, set its minimum size = max size, so user cannot resize it.当你的表单最大化时,设置它的最小尺寸 = 最大尺寸,这样用户就不能调整它的大小。

    this.WindowState = FormWindowState.Maximized;
    this.MinimumSize = this.Size;
    this.MaximumSize = this.Size;

You were close... after your code of你很接近......在你的代码之后

WindowState = FormWindowState.Maximized;

THEN, set the form's min/max size capacity to the value once its sized out.然后,将表单的最小/最大大小容量设置为大小后的值。

MinimumSize = this.Size;
MaximumSize = this.Size;

To stop the window being resizeable once you've maximised it you need to change the FormBorderStyle from Sizable to one of the fixed constants:要在最大化窗口后停止调整窗口大小,您需要将FormBorderStyleSizable更改为固定常量之一:

FixedSingle
Fixed3D
FixedDialog

From the MSDN Page Remarks section:MSDN 页面备注部分:

The border style of the form determines how the outer edge of the form appears.窗体的边框样式决定了窗体外边缘的显示方式。 In addition to changing the border display for a form, certain border styles prevent the form from being sized.除了更改窗体的边框显示外,某些边框样式还会阻止调整窗体大小。 For example, the FormBorderStyle.FixedDialog border style changes the border of the form to that of a dialog box and prevents the form from being resized.例如,FormBorderStyle.FixedDialog 边框样式将窗体的边框更改为对话框的边框并防止调整窗体的大小。 The border style can also affect the size or availability of the caption bar section of a form.边框样式还会影响表单标题栏部分的大小或可用性。

It will change the appearance of the form if you pick Fixed3D for example, and you'll probably have to do some work if you want the form to restore to non-maximised and be resizeable again.例如,如果您选择Fixed3D ,它将改变表单的外观,如果您希望表单恢复到非最大化状态并再次调整大小,您可能需要做一些工作。

Change the property WindowState to System.Windows.Forms.FormWindowState.Maximized , in some cases if the older answers doesn't works.将属性WindowState更改为System.Windows.Forms.FormWindowState.Maximized ,在某些情况下,如果旧答案不起作用。

So the window will be maximized, and the other parts are in the other answers.所以窗口会被最大化,其他部分在其他答案中。

When I do this, I get a very small square screen instead of a maxed screen. 当我这样做时,我得到一个很小的正方形屏幕,而不是最大屏幕。 Yet, when I only use the FormWindowState.Maximized, it does give me a full screen. 但是,当我仅使用FormWindowState.Maximized时,它确实为我提供了全屏显示。 Why is that? 这是为什么?

        public partial class Testscherm : Form
    {
            public Testscherm()

            {

                    this.WindowState = FormWindowState.Maximized;
                    this.MaximizeBox = false;
                    this.MinimizeBox = false;
                    this.MinimumSize = this.Size;
                    this.MaximumSize = this.Size;
                    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                    InitializeComponent();


            }

To programmatically maximize the windowstate you can use:要以编程方式最大化窗口状态,您可以使用:

 this.WindowState = FormWindowState.Maximized;
 this.MaximizeBox = false;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM