标签的位置在xp和7中是不同的

时间:2011-02-25 18:45:59

标签: 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++;
        //    }
        //}
    }
}

btw如何通过exe验证,以便普通人可以毫无畏惧地使用它? virustotal.com?

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

3 个答案:

答案 0 :(得分:2)

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

也许这是解决问题或dpi问题。他们也改变了从一个OS到另一个OS的颜色?

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

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

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

答案 1 :(得分:2)

        this.Region = new Region(new Rectangle(10, 10, 197, 17));

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

答案 2 :(得分:1)

这与Windows XP和Windows 7上DPI settings的差异有关。

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