简体   繁体   English

动态更改按钮大小

[英]Dynamically changing button size

I'm using .NET CF with Visual Studio 2008. 我在Visual Studio 2008中使用.NETCF。

In autogenerated Form.Designer.cs there is following code to specify button dimensions: 在自动生成的Form.Designer.cs中,有以下代码指定按钮尺寸:

this.buttonX.Size = new System.Drawing.Size(96, 24);

But when I use even the same code in my constructor dimensions "freak out" and button is much smaller. 但是,即使我在构造函数维度中甚至使用相同的代码,也会“变怪”,并且按钮要小得多。

public FormX()
{
    InitializeComponent();
    this.buttonX.Size = new System.Drawing.Size(96, 24);
}

As seen on screenshots, left side works fine and on the right side I changed the dimensions from my constructor and the button is smaller... 如屏幕截图所示,左侧工作正常,而右侧则更改了构造函数的尺寸,并且按钮较小。

EDIT: Just to clarify, button is created in designer view, and i'd like to change the button size(just size) dynamically afterwards. 编辑:只是为了澄清,按钮是在设计器视图中创建的,之后我想动态更改按钮的大小(即大小)。

在此处输入图片说明

The problem is caused by AutoScaling. 该问题是由AutoScaling引起的。 Your development machine has different DPI settings than your device. 开发机器的DPI设置与设备不同。 In the generated code, the development DPI settings are actually stored, and this is used to scale the height and width of your controls via a comparison to the device DPI. 在生成的代码中,实际存储了开发DPI设置,该设置用于通过与设备DPI进行比较来缩放控件的高度和宽度。

See https://msdn.microsoft.com/en-us/library/system.windows.forms.autoscalemode(v=vs.90).aspx for more information, plus there are a lot of articles on this topic as well. 有关更多信息,请参见https://msdn.microsoft.com/zh-cn/library/system.windows.forms.autoscalemode(v=vs.90).aspx ,此外,有关此主题的文章也很多。

You can change the AutoScaleMode and manage all the sizes yourself like shown here ( How to make compact framework custom controls AutoScale aware ), or you can apply the same size adjustments the framework is doing when you manually adjust your control sizes. 您可以更改AutoScaleMode并自己管理所有大小,如此处所示( 如何使紧凑的框架自定义控件能够自动缩放 ),也可以在手动调整控件大小时应用框架所做的相同大小调整。


If the control already exists and you are just trying to adjust its size, you can still scale the size yourself, similiar to what was done in some of the linked articles. 如果控件已经存在,而您只是想调整其大小,您仍然可以自己缩放大小,类似于某些链接文章中所做的。

Something like this (free-0hand, not syntax-checked): 像这样的东西(free-0,未经语法检查):

SizeF scale = this.AutoScaleFactor;
Size desiredSize = new Size(96, 24);
SizeF calculatedSize = new SizeF(desiredSize.Width * scale.Width, desiredSize.Height * scale.Height);
this.buttonX.Size = calculatedSize.ToSize();

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

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