Windows窗体大小问题在不同的分辨率

我是窗口表单开发的新手,在开发了一些表格之后我注意到表单在不同的分辨率下没有正确显示,表格超出了屏幕的某些分辨率

我想知道是否有任何设置可以根据分辨率自动调整表单,或者是否有任何黑客或某些技术可以用来设计表单。

请详细说明您的答案,因为我对Windows窗体开发非常新鲜。

谢谢

这里的问题更可能是它没有按照你期望的方式工作。 在WinForms开发中,当您设计表单时,实际上是在设置其大小。 这遵循显示表单的机器上的默认字体大小的function,并且不直接与所讨论的显示器上的分辨率相关。 因此,如果您设计一个包含许多控件或大型控件的大型表单,它可能会以高分辨率显示,但不能以低分辨率显示。 为了更好地了解此大小调整,请查看您的Form1.Designer.cs文件,您将看到为控件设置的大小值。 这些尺寸不等于像素,但它们应该为您提供相对尺寸。 您可能还应该在MSDN或其他地方研究对话框单元

您可以编写一些代码来在表单加载事件中对该解决方案做出反应,但最终大小将受到您需要显示的小部件数量的部分限制。 如果您有多行编辑字段,网格控件,树控件或其他一些大小部件,您可以根据当前显示分辨​​率自动resize,并同时调整窗口大小。 但这是基于您的需求的特定于应用程序的决定,这就是为什么Windows不会尝试为您自动resize的原因。

如上所述,WPF提供了更灵活的表单定义模型,并且可以对重新调整的小部件更具react native,但最终如果表单足够忙,则WPF表单在较低分辨率上具有相同的问题。

您可以在load事件中循环表单上的每个控件,并重新缩放它们和表单本身,比例是您设计表单的屏幕尺寸与应用程序屏幕尺寸之间的比率正在努力。

  //this is a utility static class public static class Utility{ public static void fitFormToScreen(Form form, int h, int w) { //scale the form to the current screen resolution form.Height = (int)((float)form.Height * ((float)Screen.PrimaryScreen.Bounds.Size.Height / (float)h)); form.Width = (int)((float)form.Width * ((float)Screen.PrimaryScreen.Bounds.Size.Width / (float)w)); //here font is scaled like width form.Font = new Font(form.Font.FontFamily, form.Font.Size * ((float)Screen.PrimaryScreen.Bounds.Size.Width / (float)w)); foreach (Control item in form.Controls) { fitControlsToScreen(item, h, w); } } static void fitControlsToScreen(Control cntrl, int h, int w) { if (Screen.PrimaryScreen.Bounds.Size.Height != h) { cntrl.Height = (int)((float)cntrl.Height * ((float)Screen.PrimaryScreen.Bounds.Size.Height / (float)h)); cntrl.Top = (int)((float)cntrl.Top * ((float)Screen.PrimaryScreen.Bounds.Size.Height / (float)h)); } if (Screen.PrimaryScreen.Bounds.Size.Width != w) { cntrl.Width = (int)((float)cntrl.Width * ((float)Screen.PrimaryScreen.Bounds.Size.Width / (float)w)); cntrl.Left = (int)((float)cntrl.Left * ((float)Screen.PrimaryScreen.Bounds.Size.Width / (float)w)); cntrl.Font = new Font(cntrl.Font.FontFamily, cntrl.Font.Size * ((float)Screen.PrimaryScreen.Bounds.Size.Width / (float)w)); } foreach (Control item in cntrl.Controls) { fitControlsToScreen(item, h, w); } } } //inside form load event //send the width and height of the screen you designed the form for Utility.fitFormToScreen(this, 788, 1280); this.CenterToScreen(); 

这些都是您的应用程序(.Net)给出的更改。 因为现在知道哪个是活跃的forms。 因此,应用程序在您的表单中将分辨率更改为暗淡和/或厚度。 有时,表格只能看到白色。 这是由于您的计算机性能。