C# 防止用户调整窗口/表单大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1330339/
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-08-06 15:14:03  来源:igfitidea点击:

Prevent users from resizing the window/form size

c#winformsvisual-studio-2008

提问by Mary

User can change form size. I do not find a property of form that do not allow user to change form size.

用户可以更改表单大小。我没有找到不允许用户更改表单大小的表单属性。

回答by Ed S.

Set the min and max size to the same value.

将最小和最大大小设置为相同的值。

回答by ZippyV

Set the FormBorderStyleto another value like FixedSingleor FixedDialog.

将 设置FormBorderStyle为另一个值,例如FixedSingleFixedDialog

回答by Aric TenEyck

Change the BorderStyle to be one of the "Fixed" styles and remove the maximize button.

将 BorderStyle 更改为“固定”样式之一并删除最大化按钮。

回答by Adrian Godong

Change the FormBorderStyleto Fixed*.

FormBorderStyle更改为Fixed*

回答by novacara

Change FormBorderStyleto FixedDialog, FixedSingle, or Fixed3D. Also, if you do not want them to maximize the form set Maximizeto False.

更改FormBorderStyleFixedDialogFixedSingle或者Fixed3D。另外,如果您不希望它们最大化将表单设置Maximize为 False。

回答by Chris Dunaway

The form has MinimumSize and MaximumSize properties that you can set to control this. You might use this if you want to keep the standard form border.

该表单具有可设置的 MinimumSize 和 MaximumSize 属性来控制它。如果您想保留标准表单边框,您可以使用它。

回答by nawfal

There are a few of workarounds for this:

有一些解决方法:

  1. Set maximum size property to a value you prefer. If you do not want the application window to be shrunk as well, then set a minimum size property. If you prefer the application to have the exact same size as that of design time, then set both maximum size and minimum size as size of your window. (Once you set maximum size or minimum size from the designer, you can't resize your window programmatically, unless you re-set maximum size and minimum size programmatically again)

  2. Set FormBorderStyle to FixedSingle or FixedDialog. The difference in looks wont be noticeable for untrained eyes, but one considerable difference I'd found from my experience is that, when you make it FixedSingle, you can still change the size programmatically. With FixedDialog its not possible. That's a huge advantage for FixedSingle property. (If you want to change size of your window programmatically here after going for FixedDialog, then you got to programmatically change FormBorderStyle first, which would create a slight blink effect when running the application).

  1. 将最大尺寸属性设置为您喜欢的值。如果您不希望应用程序窗口也缩小,则设置最小尺寸属性。如果您希望应用程序具有与设计时完全相同的大小,则将最大大小和最小大小都设置为窗口大小。(一旦您从设计器中设置了最大尺寸或最小尺寸,您就无法以编程方式调整窗口大小,除非您再次以编程方式重新设置最大尺寸和最小尺寸)

  2. 将 FormBorderStyle 设置为 FixedSingle 或 FixedDialog。对于未受过训练的眼睛,外观上的差异不会很明显,但我从我的经验中发现的一个显着差异是,当您将其设为 FixedSingle 时,您仍然可以通过编程方式更改大小。使用 FixedDialog 是不可能的。这对于 FixedSingle 属性来说是一个巨大的优势。(如果您想在使用 FixedDialog 后以编程方式在此处更改窗口大小,那么您必须首先以编程方式更改 FormBorderStyle,这会在运行应用程序时产生轻微的闪烁效果)。

So simply go for FixedSingle. And to make sense, do the following:

所以只需选择FixedSingle。为了有意义,请执行以下操作:

a. Set maximize box property to false.

一种。将最大化框属性设置为 false。

b. Set SizeGripStyle to Hide.

湾 将 SizeGripStyle 设置为隐藏。

回答by Philo

You can change the border style to :

您可以将边框样式更改为:

BorderStyle - fixedToolWindow

边框样式 - 固定工具窗口

But you will loose the maximize and minimize buttons, custom buttons will be needed if you require those functionalities.

但是您将失去最大化和最小化按钮,如果您需要这些功能,则需要自定义按钮。

回答by Bassem Akl

From the FormProperties Window set:
1. FormBorderStyle -> FixedSingle.
2. MaximizeBox -> False.

表单属性窗口设置:
1. FormBorderStyle -> FixedSingle。
2. 最大化框 -> False。

回答by zdarova

Just add this 2 lines of C# code for your form (inside InitializeComponent() function):

只需为您的表单添加这 2 行 C# 代码(在 InitializeComponent() 函数内):

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.ImeMode = System.Windows.Forms.ImeMode.NoControl;

Tested with Visual Studio 2017 with .NET 4.6.1

使用 Visual Studio 2017 和 .NET 4.6.1 进行测试