且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用 win7 编写的 winforms 应用程序在 win xp 上看起来不同.为什么?

更新时间:2023-09-21 12:07:40

    this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.BackColor = System.Drawing.SystemColors.ActiveCaption;

这些是给您带来麻烦的陈述.我会先挑一个简单的,不要让表单的背景颜色与标题颜色相同.如果您想选择主题颜色,则只需选择控制"颜色.尽管您通常会让旧战舰变灰.选择中性柔和的颜色是您***的选择,但尊重用户的喜好永远不会给您带来麻烦.

These are the statements that cause your trouble. I'll pick-off the easy one first, don't make the BackColor of the form the same as the caption color. If you want to pick a theme color then only choose the "Control" color. Albeit that you'll typically get the old battleship gray. Picking a neutral pastel color is your best bet but honoring the user's preference will never get you into trouble.

AutoScaleDimensions 属性是根据视频适配器的 DPI 设置自动生成的.这与XP机器不同.你的开发机器上有 120 点/英寸,XP 上有 96 DPI(默认值).在 Win7 上,这是由看起来像标尺的小部件、控制面板 + 显示、设置自定义文本大小 (DPI)"设置的.

The AutoScaleDimensions property is auto-generated, based on the video adapter's DPI setting. Which is different the XP machine. You've got 120 dots-per-inch on your dev machine, 96 DPI (the default) on XP. On Win7, that's set by the widget that looks like a ruler, Control Panel + Display, "Set custom text size (DPI)".

AutoScaleMode 属性正确设置为字体.这确保所有控件都会自动缩放以适合字体大小.由于更高的 DPI 设置,它在您的 Win7 机器上更大.因此,窗体及其控件在 XP 机器上会缩小.NumericUpDown 控件的问题在于它有点问题(不止一种方式),它不能正确缩放向上/向下字形.它们按比例太大,没有为文本部分留出足够的空间.只需将其扩大一点即可解决问题.

The AutoScaleMode property is correctly set to Font. This ensures that all controls are automatically scaled to fit the font size. Which is larger on your Win7 machine because of the higher DPI setting. Accordingly, the form and its controls shrink on the XP machine. The problem with the NumericUpDown control is that its a bit buggy (in more than one way), it doesn't scale the up/down glyphs properly. They are proportionally too large, not leaving enough room for the text portion. Simply making it a bit wider solves the problem.

自动缩放相当丑陋,很少是 100% 完美的.***的办法是将您的开发机器切换到 96 dpi.一个非常常见的设置,今天仍然如此.放大放大几乎总是比缩小效果更好.

Auto-scaling is fairly ugly, it is rarely 100% perfect. The best thing to do is to switch your dev machine to 96 dpi. A very common setting, still today. Scaling up almost always works better than scaling down.