首页 > 解决方案 > 以最小尺寸(仅显示标题栏)而不是与其属性设置的值对应的尺寸加载表单

问题描述

我有一个表单,其中包含一个Menu Strip停靠在顶部、一个Status Strip停靠在底部和一个Panel停靠以填充上述控件之间的整个空间。我已将表单的属性设置为以下值:

在设计阶段:

AutoScaleMode: Dpi
AutoSize: false
AutoSizeMode: GrowOnly
DoubleBuffered: true
SizeGripStyle: Show

在运行时(在表单的构造函数中):

// Calculate the default size of the window on the basis of the ratio of the dimensions of the window to the dimension of the screen resolution of the machine used in development as the default dimensions of the window is aligned to that of the machine used to design it    
this.Size = new Size(Screen.GetWorkingArea(this.Location).Size.Width * (widthOfWindowInDesignPhase /horizontalResolutionOfTheDisplayInDesignPhase), Screen.GetWorkingArea(this.Location).Size.Height * (heightOfWindowInDesignPhase / verticalResolutionOfTheDisplayInDesignPhase)); 
this.MinimumSize = new Size(this.Size.Width, this.Size.Height);

我第一次尝试解决这个问题是修改AutoSizeandAutoSizeMode属性,但我需要将其设置为 aforestated 值,因为更改它们将不允许用户调整表单大小。我尝试的另一种方法也失败了,它是AutoSize将上述控件的属性设置false为强制窗体的子容器不调整大小。

提前致谢。

PS 相关表格截图:

以最小尺寸加载的表单

标签: c#.netwindowswinformssize

解决方案


当计算表单的大小作为语句(widthOfWindowInDesignPhase /horizontalResolutionOfTheDisplayInDesignPhase)(heightOfWindowInDesignPhase / verticalResolutionOfTheDisplayInDesignPhase)返回不能存储为整数的浮点值时,会发生(错误)数字错误。

适当的说法如下:

this.Size = new Size(Convert.ToInt32(Screen.GetWorkingArea(this.Location).Size.Width * (Convert.ToDouble(widthOfWindowInDesignPhase) / Convert.ToDouble(horizontalResolutionOfTheDisplayInDesignPhase))), Convert.ToInt32(Screen.GetWorkingArea(this.Location).Size.Height * (Convert.ToDouble(heightOfWindowInDesignPhase) / Convert.ToDouble(verticalResolutionOfTheDisplayInDesignPhase))));


推荐阅读