用win7编写的winforms app在win xp上看起来不一样.为什么? [英] winforms app written in win7 looks different on win xp. why?

查看:24
本文介绍了用win7编写的winforms app在win xp上看起来不一样.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 win 7 上用 winforms (.net 4.0) 编写了一个简单的应用程序.应用程序看起来是我想要的,但是当我在 windows xp 上尝试时,一切看起来都不同.

I have written a simple app in winforms (.net 4.0) on win 7. Application looks how I want but when I tried it on windows xp everything looks different.

我创建了一个示例来展示它在 win 7 和 xp 上的外观.我该怎么做才能在两个系统上拥有相同的外观?问题不仅在于背景和字体颜色,还在于控件.在这里,我展示了 numericupdown 的外观,但表格布局也有问题.

I have created a sample example to show how it looks on win 7 and xp. What can I do to have the same look on both systems? The problem is not only with the background and font color but with the controls too. Here I show how the numericupdown looks but with table layout I have problem too.

 private void InitializeComponent()
    {
        this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
        this.SuspendLayout();
        // 
        // numericUpDown1
        // 
        this.numericUpDown1.DecimalPlaces = 2;
        this.numericUpDown1.Increment = new decimal(new int[] {
        1,
        0,
        0,
        131072});
        this.numericUpDown1.Location = new System.Drawing.Point(21, 26);
        this.numericUpDown1.Maximum = new decimal(new int[] {
        1,
        0,
        0,
        0});
        this.numericUpDown1.Name = "numericUpDown1";
        this.numericUpDown1.Size = new System.Drawing.Size(54, 22);
        this.numericUpDown1.TabIndex = 0;
        // 
        // groupBox1
        // 
        this.groupBox1.Location = new System.Drawing.Point(21, 82);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(226, 99);
        this.groupBox1.TabIndex = 1;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "groupBox1";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.SystemColors.ActiveCaption;
        this.ClientSize = new System.Drawing.Size(407, 331);
        this.Controls.Add(this.groupBox1);
        this.Controls.Add(this.numericUpDown1);
        this.Name = "Form1";
        this.Text = "Form1";
        ((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
        this.ResumeLayout(false);

    }

我没有修改过 xp 颜色主题.我在两台装有 win xp 的不同计算机上得到了相同的结果.

I have no modified the xp color themes. I have the same result on two diffrent computers with win xp.

推荐答案

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

这些是给您带来麻烦的陈述.我会先选择简单的,不要让表单的BackColor与标题颜色相同.如果要选择主题颜色,则只需选择控制"颜色.尽管您通常会将旧战舰变成灰色.选择中性柔和的颜色是您最好的选择,但尊重用户的偏好绝不会给您带来麻烦.

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.

这篇关于用win7编写的winforms app在win xp上看起来不一样.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆