一、. this.label_Msg.AutoSize = false; //设置label空件不能自动大小
二、. 用代码控制label控件的大小
1.根据字符串、label的宽度 计算字符串的面积,函数如下:
/// <summary> /// 计算字符串的面积 /// </summary> /// <param name="msg">字符串</param> /// <param name="labelWidth">Label标签的宽度</param> /// <param name="labelFont">Label字体</param> /// <returns>字符串的面积</returns> private int GetMsgArea(string msg, float labelWidth, Font labelFont) { int msg_area = 0; if (msg.Contains('\n')) //含换行符 { string[] msg_rows = msg.Split('\n'); //测量每一行字符串的大小 foreach (string msg_row in msg_rows) { int msg_row_area = GetSingleLineStringArea(msg_row, labelWidth, labelFont); msg_area += msg_row_area; } } else //单行字符串 { msg_area = GetSingleLineStringArea(msg, labelWidth, labelFont); } return msg_area; } /// <summary> /// 测量单行字符串的面积 /// </summary> /// <param name="str">单行字符串(不含换行符\n)</param> /// <param name="labelWidth">Label的宽度</param> /// <param name="labelFont">Label字体</param> /// <returns>单行字符串的面积</returns> private int GetSingleLineStringArea(string str, float labelWidth, Font labelFont) { Size size = TextRenderer.MeasureText(str, labelFont); Size singleWordLength = TextRenderer.MeasureText("国", labelFont); int rowCountOfMsgInLabel = (int)Decimal.Ceiling((decimal)size.Width / (((decimal)labelWidth) - singleWordLength.Width)); //依据label宽度分隔字符串(label容纳字体宽度实际小于自身宽度) int msg_area = (int)(rowCountOfMsgInLabel * labelWidth * size.Height); //按label宽度分隔字符串成几行 * 行宽 * 行高 return msg_area; }
2.设置label的宽、高,直到label的面积可以容纳字符串,函数如下:
/// <summary> /// 初始化消息标签 /// </summary> private void InitLabelMsg() { string msg = this.message; msg = msg.Replace("\r", ""); //去掉影响测量的\r字符 //msg = msg.Replace("\n", ""); //计算字符串的面积 int msg_area = this.GetMsgArea(msg, this.label_Msg.Width, this.label_Msg.Font); //根据面积计算是否可以容纳消息(label自动换行的,所以长度超出label长度没事。) int label_area = this.label_Msg.Width * this.label_Msg.Height; float label_new_width = this.label_Msg.Width; //label的宽、高采用float型计算,精确 float label_new_height = this.label_Msg.Height; int msg_new_area = 0; int label_new_area = 0; float formProportion = (float)this.Height / (float)this.Width; //窗体高宽比例 if (msg_area > label_area) //超出面积 { do { label_new_width += 2; //Label宽度增加 label_new_height += 2 * formProportion; //Label高度按比例增加 msg_new_area = this.GetMsgArea(msg, label_new_width, this.label_Msg.Font); label_new_area = (int)(label_new_width * label_new_height); if (label_new_area >= msg_new_area) //新label大小够大了 { break; } } while (true); int dx = (int)label_new_width - this.label_Msg.Width; int dy = (int)label_new_height - this.label_Msg.Height; int formWidth = this.Width + dx; int formHeight = this.Height + dy; int maxFormWidth = 800; if (formWidth >= maxFormWidth) //最大宽度限制 { SetFormWidthFixed(msg, label_new_area, maxFormWidth); } else { this.label_Msg.Width = (int)label_new_width; //Label Dock=Fill,不设置大小亦可 this.label_Msg.Height = (int)label_new_height; this.Width = formWidth; this.Height = formHeight; } } this.label_Msg.Text = msg; }
3.当label的面积过大时,可以在label外层包一个panel,设置panel.AutoScroll = true;
/// <summary> /// 设置Form宽度固定,计算label标签大小,Form自动出现滚动条 /// </summary> /// <param name="msg">消息</param> /// <param name="label_area">当前label面积</param> /// <param name="formMaxWidth">Form最大宽度</param> private void SetFormWidthFixed(string msg ,int label_area , int formMaxWidth) { this.label_Msg.Dock = DockStyle.None; float label_width = formMaxWidth - this.label_Msg.Margin.Left - this.label_Msg.Margin.Right - 10; float label_height = (int)((float)label_area / label_width); //大致新高度 int msg_area = this.GetMsgArea(msg, label_width, this.label_Msg.Font); int label_new_area = label_area; do //重新计算label的高度 { label_height += 2; label_new_area = (int)(label_width * label_height); if (label_new_area >= msg_area) { break; } } while (true); this.label_Msg.Width = (int)label_width; this.label_Msg.Height = (int)label_height; float formProportion = (float)this.Height / (float)this.Width; //窗体高宽比例 this.Width = formMaxWidth; this.Height = (int)(formMaxWidth * formProportion); this.panel_Scroll.AutoScroll = true; }
三、效果图展示