日期:2014-05-18  浏览次数:20889 次

Picturebox label
动态生成Picturebox还有label 如何让label的位置在picturebox上面居中显示呢?

------解决方案--------------------
C# code
        private void Form1_Load(object sender, EventArgs e)
        {
            PictureBox pic = new PictureBox();
            Image img = Image.FromFile(@"C:\Users\SaeWind\Desktop\123\1.png");
            pic.Image = (Image)img.Clone();
            img.Dispose();
            pic.Location = new Point(30,50);
            pic.Size = pic.Image.Size;
            

            Label lb = new Label();
            lb.Text = "为哈佛撒谎发觉个";
            Graphics g = Graphics.FromHwnd(lb.Handle);
            SizeF size=g.MeasureString(lb.Text, lb.Font);
            g.Dispose();
            lb.Location =  new Point(pic.Left + pic.Width / 2 - (int)size.Width / 2, pic.Top + pic.Height / 2 - (int)size.Height / 2);


            this.Controls.Add(lb);
            this.Controls.Add(pic);
        }