【问题标题】:Label's position is different in xp and 7标签的位置在 xp 和 7 中不同
【发布时间】:2011-07-04 12:36:42
【问题描述】:

我做了一个小应用程序(winforms)来显示板球比分(世界才刚刚开始,耶)。 它在 xp 中运行良好,但在 win 7 中,与 xp 中的位置相比,标签显示的位置下降了几个像素,这完全破坏了一切。 (我希望这很清楚)

这里是 exe:[已编辑]

它在 xp 中的外观; http://imgur.com/emcKG.jpg

它在 7 中的样子(大约):http://imgur.com/sdqry.jpg

也有人可以确认我的应用程序需要哪个 .net 吗?我认为它的 .net 2.0,因为目标框架设置为 .Net 2.0。

谢谢

编辑:下次不会发布 exe。对不起!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Xml;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        int numberOfMatches = 0;
        int selectedmatch = 0;
        string[,] data;

        string fileToParse = Path.GetTempPath() + "cricketfile.xml";
        int matchToShow = 0;
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd,
                         int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

        public Form1()
        {

            InitializeComponent();

            int X = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
            this.Location = new System.Drawing.Point(X, -5);


            if (rkApp.GetValue("cricketscore") == null)
            {
                startWithWindowsToolStripMenuItem.Checked = false ;
            }
            else
            {
                startWithWindowsToolStripMenuItem.Checked = true ;
            }
            this.Region = new Region(new Rectangle(10, 10, 197, 17));
            ToolTip tooltip = new ToolTip();
            tooltip.SetToolTip(pictureBox1, " right click for settings, left click to move");
            tooltip.SetToolTip(label1, " right click for settings, left click to move");
            fetchData();
            addContextEntries();
            chooseIndia();
            updateScore();

           // MessageBox.Show(data.GetLength(0).ToString());


            //foreach (string s in data)
            //{
            //    Console.WriteLine(s);
            //}

            timer1.Interval = 10 * 1000;
            timer1.Enabled = true;

        }
        public void addContextEntries()
        {
           // MessageBox.Show("num- " + numberOfMatches);
            List<ToolStripMenuItem> Mylist1 = new List<ToolStripMenuItem>();
            for (int i = 0; i < numberOfMatches; i++)
            {
                Mylist1.Add( new ToolStripMenuItem() );
                this.contextMenuStrip1.Items.Add(Mylist1[i]);
                Mylist1[i].Text = "See Match " + (i + 1) + "'s score";
                switch(i) 
                {
                    case 0:
                        Mylist1[i].Click += new System.EventHandler(this.match1);
                        break;
                    case 1:
                        Mylist1[i].Click += new System.EventHandler(this.match2);
                        break;
                    case 2:
                        Mylist1[i].Click += new System.EventHandler(this.match3);
                        break;
                    case 3:
                        Mylist1[i].Click += new System.EventHandler(this.match4);
                        break;
                    case 4:
                        Mylist1[i].Click += new System.EventHandler(this.match5);
                        break;
                }

            }

        }
        public void match1(object sender, EventArgs e)
        {
           // MessageBox.Show("match 1");
            matchToShow = 0;
            label1.Text = data[0, 0] + " " + data[0, 1];


        }
        public void match2(object sender, EventArgs e)
        {
          //  MessageBox.Show("match 2");
            matchToShow = 1;
            label1.Text = data[1, 0] + " " + data[1, 1];

        }
        public void match3(object sender, EventArgs e)
        {
            matchToShow = 2;
            label1.Text = data[2, 0] + " " + data[2, 1];

        }
        public void match4(object sender, EventArgs e)
        {
            matchToShow = 3;
            label1.Text = data[3, 0] + " " + data[3, 1];

        }
        public void match5(object sender, EventArgs e)
        {
            matchToShow = 4;
            label1.Text = data[4, 0] + " " + data[4, 1];

        }

        public void chooseIndia()
        {
            for (int i = 0; i < data.GetLength(0); i++)
            {
                // MessageBox.Show("i - " + i);
                if (data[i, 3].ToLower().Contains("australia"))
                {
                    matchToShow = i;
                    // MessageBox.Show("i - " + i);
                    break;
                }
            }
        }
        public void updateScore()
        {
            fetchData();
            //foreach (string s in data)
            //{
            //    Console.WriteLine(s);
            //}
            // MessageBox.Show("matchToShow- " + matchToShow);
            label1.Text = data[matchToShow,0] + " " + data[matchToShow ,1];
        }

        public void fetchData()
        {
            int matchnumber = -1;
            numberOfMatches = 0;
            WebClient Client = new WebClient();
            try
            {
                Client.DownloadFile("http://synd.cricbuzz.com/score-gadget/gadget-scores-feed.xml", fileToParse);
            }
            catch ( WebException we)
            {
                if (we.ToString().ToLower().Contains("connect to"))
                    ;//MessageBox.Show("unable to connect to server") ;
            }
            XmlTextReader xmlreader0 = new XmlTextReader(fileToParse);
            while (xmlreader0.Read())
            {
                if (xmlreader0.Name.ToString() == "match" && xmlreader0.NodeType == XmlNodeType.Element)
                {
                    ++numberOfMatches;
                }
            }
            data = new string[numberOfMatches, 4];
           // numberOfMatches = 0;
            // MessageBox.Show("matchnumbers - " + numberOfMatches);

            XmlTextReader xmlreader = new XmlTextReader(fileToParse);
            while (xmlreader.Read())
            {
                if (xmlreader.Name.ToString() == "header" && xmlreader.NodeType == XmlNodeType.Element)
                {
                    xmlreader.Read();

                    data[matchnumber, 0] = xmlreader.Value;
                    //  MessageBox.Show(xmlreader.Value);
                }


                if (xmlreader.Name == "description" && xmlreader.NodeType == XmlNodeType.Element)
                {
                    //  MessageBox.Show(xmlreader.Value);
                    xmlreader.Read();
                    //  MessageBox.Show("matched - " + xmlreader.Value);
                    data[matchnumber, 1] = xmlreader.Value;
                }

                if (xmlreader.Name == "url-text" && xmlreader.NodeType == XmlNodeType.Element)
                {
                    xmlreader.Read();
                    // MessageBox.Show(xmlreader.Value);
                    data[matchnumber, 2] = xmlreader.Value;
                }

                if (xmlreader.Name == "url-link" && xmlreader.NodeType == XmlNodeType.Element)
                {
                    xmlreader.Read();
                    data[matchnumber, 3] = xmlreader.Value;
                }

                if (xmlreader.Name.ToString() == "match" && xmlreader.NodeType == XmlNodeType.Element)
                {
                    matchnumber++;
                }

            }
            xmlreader.Close();
            xmlreader0.Close();

        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            updateScore();
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void switchColrosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if ( label1.ForeColor == System.Drawing.Color.Black)
                label1.ForeColor = System.Drawing.Color.White ;
            else
                label1.ForeColor = System.Drawing.Color.Black; 
        }

        private void startWithWindowsToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void startWithWindowsToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
        {
            if (startWithWindowsToolStripMenuItem.Checked == true)
            {
                rkApp.SetValue("cricketscore", Application.ExecutablePath.ToString());
            }
            else
            {
                rkApp.DeleteValue("cricketscore", false);
            }
        }

        private void showFullScorecardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(data[matchToShow, 3]);
        }

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        //public void findNumberOfMatches()
        //{
        //    if (xmlreader.Name.ToString() == "match" && xmlreader.NodeType == XmlNodeType.Element)
        //    {
        //        matchnumber++;
        //    }
        //}
    }
}

顺便说一句,我如何通过exe验证,让普通人可以无所畏惧地使用它? virustotal.com ?

编辑:哎呀,我错了,不仅仅是标签。标签左侧的图片框也向下移动了几个像素。

【问题讨论】:

  • 能否提供源代码?
  • 请不要发布任意可执行文件的链接。据我们所知,这可能是一种病毒。如果您需要帮助,请发布复制问题和屏幕截图的源代码。没有人会分解你的二进制文件来看看你在做什么。
  • -1 请发布屏幕截图并向我们展示一些代码,而不是期望我们运行一些我们没有理由信任的随机应用程序。请指定您使用的 UI 技术(WinForms、WPF 等),因为这可能有助于发现标签问题。此外,您问的是两个完全不同的问题,这两个问题都不是特别适合普通观众(这不是自定义的修复我的应用程序网站,而是一个问答网站)。
  • 是的,我应该发布截图..
  • 只是改变位置的一个标签,还是表单的其他元素?

标签: c# winforms windows-7 windows-xp


【解决方案1】:

从您的屏幕截图中可以看出,包含文本的整个框缩小了,并且有一个蓝色条覆盖了标签的一部分。

可能是分辨率问题或 dpi 问题。他们也将颜色从一个操作系统更改为另一个?

您可能需要使用静态硬编码值在代码中设置位置和其他属性,而不是让窗口从设计器视图中放置它或使用基于屏幕大小的一些可变数字

这在c#.net中应该是类似的

        label1.Left = 10;
        label1.Top = 10;

【讨论】:

  • 没用。将标签进一步推低。不过,一旦我在 7 上获得 Visual c# express,我会再多说一点
  • 我希望你没有使用 10 作为值,这只是一个示例,尝试使用较小的 #,比如 1 或 2
  • 成功了!设置为 top=1 就可以了。非常感谢。我对在 stackoverflow 上获得的帮助感到惊讶。即使是我愚蠢的菜鸟问题。谢谢大家。
【解决方案2】:
        this.Region = new Region(new Rectangle(10, 10, 197, 17));

删除那个。它会使您的窗口在具有更高视频点数/英寸设置的机器上太小。在Win7上很常见。较高的 DPI 会使字体以像素为单位更高。 Form.AutoScaleMode 属性通过使控件更大以适应更大的字体来自动调整。但是,您的区域没有增长,切断了控件的底部。不知道为什么首先使用区域,因为它是一个普通的矩形,我想你正在寻找 FormBorderStyle = None。表单不允许您将其设置得太小,但您可以在 OnLoad 方法中覆盖它。将 ClientSize 设置得足够大以适应重新缩放的控件。

【讨论】:

    【解决方案3】:

    这与 Windows XP 和 Windows 7 上的 DPI settings 不同有关。

    How to change DPI on Windows 7.
    How to change DPI on Windows XP.

    【讨论】:

    • 那么我该如何修复我的应用程序,所以两者看起来都一样
    猜你喜欢
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 2013-11-07
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-18
    • 2020-09-11
    相关资源
    最近更新 更多