c# - 标签的位置在 xp 和 7 中不同

标签 c# winforms windows-7 windows-xp

我制作了一个小应用程序 (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 ?

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

最佳答案

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

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

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

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

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

关于c# - 标签的位置在 xp 和 7 中不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5121293/

相关文章:

winapi - 带有默认图像/照片查看器的 Windows 7 上的 ShellExecuteEx

python - 当安装管理器未安装Python时,如何在Windows上安装MySQL Connector for Python?

c# - 需要创建显示一段时间内使用情况的报告

c# - 如何在列表中添加时间跨度值

c# - 在 wpf 中安排应用程序操作

c# - 如何设置我的自定义控件的描述以显示在工具箱中?

java - Eclipse 不导入 Android 项目

c# - 从排序列表中加权随机选择

c# - 通过查询参数处理 .NET Core 3.1 Web API 中的多个端点

c# - 在 .NET (ffmpeg) 中启动时进程停止