我的Windows窗体应用程序大小在不同的笔记本上发生了变化

编码我的项目进展顺利。 但今天我发现了一个问题。

我的主笔记本具有全高清(1920×1080)分辨率,我在这里编写我的项目。 当我将主笔记本的分辨率更改为1280×1024,1280×800或1024×768时,没有问题。 我的应用程序的分辨率是1024×768,并没有崩溃。 这是版画屏幕。

普通屏幕

但我的其他笔记本有1366×768分辨率。 我在这台笔记本上运行我的应用程序。 哦! 有一种失望。 我的应用程序屏幕移动 这是糟糕的印刷品。

屏幕不好

我不懂为什么。 我该怎么做才能纠正这个错误?

它源于不同的DPI设置。 您可以在表单加载中执行此操作:

// Get DPI width float x = this.CreateGraphics().DpiX; // If screen is width if (x == 120) // Get big image from Resources this.BackgroundImage = Properties.Resources.BigImage; else // Get small image from Resources this.BackgroundImage = Properties.Resources.SmallImage; 

您可以检查两个屏幕的DPI设置是否相同。 你可以通过控制面板或显示选项来实现这一点(我记不起来了,我没有在我面前安装Windows 7)(你的高清笔记本电脑可能有120 DPI,另一方面有96 DPI) )。

在您的程序中,将窗体的AutoScaleMode设置为None然后重试。

这是一个有助于如何处理自动缩放表单的资源:
Windows窗体中的自动缩放

我在Windows窗体表单中遇到了同样的问题。 我所要做的就是将表单的AutoScaleMode设置从Font更改为DPI,并将FormBorderStylefor设置从Fixed更改为Sizable。 现在Windows窗体表单在桌面和笔记本电脑上正确显示。

没有直接的解决方案来解决像素问题。 但我们可以自己做。 首先,我们必须在表单中找到可用的控件,然后我们必须调整它们的大小。

添加一个类“IdentifyControls.cs”,它标识表单的所有控件并返回应用程序中的控件列表。 可以在应用程序选择项目中添加一个类 – >从菜单栏添加类。 然后输入

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace FallingFlowers // Falling Flowers is the name of my project { public static class IdentifyControl { public static List findControls(Control c) { List list = new List(); foreach (Control control in c.Controls) list.Add(control); return list; } } } 

然后在您的应用程序中添加另一个类,例如“demo.cs”:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; using System.IO; namespace FallingFlowers // Falling Flowers is the name of my project { public class demo { public static void relocate(Form fo, int ox, int oy) { List list = new List(); list = IdentifyControl.findControls(fo); for (int i = 0; i < list.Count; i++) reposition(list[i], ox, oy); } public static void reposition(Control c, int ox, int oy) { int x, y; x = ((c.Location.X * Screen.PrimaryScreen.Bounds.Width) / ox); y = ((c.Location.Y * Screen.PrimaryScreen.Bounds.Height) / oy); c.Location = new Point(x, y); x = ((c.Width * Screen.PrimaryScreen.Bounds.Width) / ox); y = ((c.Height * Screen.PrimaryScreen.Bounds.Height) / oy); c.Width = x; c.Height = y; if (c is Label || c is Button) resizeText(c, ox, oy); } public static void resizeText(Control l, int ox, int oy) { float s; float txtsize = l.Font.Size; s = ((txtsize * Screen.PrimaryScreen.Bounds.Width) / ox)+1; l.Font = new Font(l.Font.Name, s,l.Font.Style); } public static void repositionForm(Form f, int ox, int oy) { int x, y; x = (f.Location.X * Screen.PrimaryScreen.Bounds.Width) / ox; y = (f.Location.Y * Screen.PrimaryScreen.Bounds.Width) / oy; f.Location = new Point(x, y); } } } 

此类包含重定位控件,调整文本大小和调整窗体大小的方法。

在表单的load事件中调用这些函数。

重新定位表单中的所有控件

 demo.relocate(this, 1366, 768); 

这里1366和768是开发应用程序的原始分辨率。

要重新定位表单:

 demo.repositionForm(this, 1366, 768); 

1366和768是开发应用程序的原始分辨率。

对你来说它将是demo.relocate(this, 1920, 1080);

我希望这会对你有所帮助: - )...

我同意屏幕设置的代码可以帮助您找到问题。

但似乎您要设置图片来设置坐标点而不是相对于屏幕尺寸的点。 您可能希望使坐标成为屏幕大小的比例,以使显示始终看起来不错。