开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 2625|回复: 0

[求助] 对话界面,我用panel做的容器,在里面每个消息就是个label

[复制链接]

0

精华

0

贡献

0

赞扬

帖子
1
软币
67
在线时间
1 小时
注册时间
2018-5-11
发表于 2018-5-13 21:25:06 | 显示全部楼层 |阅读模式
一开始都正常,但超过panel大小,通过滚动条查看时,每个label的间距是依次增大的,能帮我看看是哪的问题吗
        private void ReceiveMsg(string strMsg)
        {
            Graphics vGraphics = CreateGraphics();
            SizeF vSizeF = vGraphics.MeasureString(strMsg, Font);
            int dStrLength = Convert.ToInt32(Math.Ceiling(vSizeF.Width));//消息长度
            System.Windows.Forms.Label msglabel = new System.Windows.Forms.Label();
            msglabel.Name = "msglabel" + msglabelnum.ToString();
            msglabel.Text = strMsg;
            msglabel.TextAlign = ContentAlignment.MiddleLeft;
            if (dStrLength / 300 == 0)                                                   //定义每个消息最大长度为300像素
            {
                msglabel.Size = new Size(dStrLength, 25);
                msglabel.Location = new Point(3, msglocY);                //msglocY是下次label的Y坐标。
                msglocY += 35;                                                          //每个短息来的时候下次Y坐标都下移35
            }
            else
            {
                msglabel.Size = new Size(300, 20 * (dStrLength / 300+1));//长度超过300的就换行显示
                msglabel.Location = new Point(3, msglocY);
                msglocY += 20 * (dStrLength / 300 + 1)+10;
            }
            msglabel.BackColor = Color.LightSkyBlue;
            Label_Paint(msglabel);
            panel2.Controls.Add(msglabel);
            panel2.VerticalScroll.Value = panel2.VerticalScroll.Maximum;
        }

回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-3-29 21:46

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表