对话界面,我用panel做的容器,在里面每个消息就是个label
一开始都正常,但超过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;
}
页:
[1]