给QListWidget的Item上添加了Label之后,怎样让item的高度自适应
1个回答
展开全部
Code Snippet
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MyControls
{
/// <summary>
/// A label that supports vertical auto resizing.
/// </summary>
public class Label : System.Windows.Forms.Label
{
private bool bChanging = false;
private SizeF MeasureStringExtended(Graphics g, string text, Font font, int desWidth)
{
string tempString = text;
string workString = "";
string outputstring = "";
int npos = 1;
int sp_pos = 0;
int sp_pos1 = 0;
int sp_pos2 = 0;
int sp_pos3 = 0;
bool bNeeded = false;
int line = 0;
int nWidth = 0;
//get original size
SizeF size = g.MeasureString(text, font);
if (size.Width > desWidth)
{
while(tempString.Length > 0)
{
//Check for the last lane
if (npos > tempString.Length)
{
outputstring = outputstring + tempString;
line++;
break;
}
workString = tempString.Substring(0, npos);
//get the current width
nWidth = (int)g.MeasureString(workString, font).Width;
//check if we've got out of the destWidth
if (nWidth > desWidth)
{
//try to find a space
sp_pos1 = workString.LastIndexOf(" ");
sp_pos2 = workString.LastIndexOf(";");
sp_pos3 = workString.IndexOf("\r\n");
if (sp_pos3 > 0)
{
sp_pos = sp_pos3;
bNeeded = false;
}
else
{
if ((sp_pos2 > 0) && (sp_pos2 > sp_pos1))
{
sp_pos = sp_pos2;
bNeeded = true;
}
else if (sp_pos1 > 0)
{
sp_pos = sp_pos1;
bNeeded = true;
}
else
{
sp_pos = 0;
bNeeded = true;
}
}
if (sp_pos > 0)
{
//cut out the wrap lane we've found
outputstring = outputstring + tempString.Substring(0,sp_pos + 1);
if (bNeeded) outputstring = outputstring + "\r\n";
tempString = tempString.Substring((sp_pos + 1), tempString.Length - (sp_pos + 1));
line++;
npos = 0;
}
else //no space
{
outputstring = outputstring + tempString.Substring(0,npos + 1);
if (bNeeded) outputstring = outputstring + "\r\n";
tempString = tempString.Substring(npos, tempString.Length - npos);
line++;
npos = 0;
}
}
npos++;
}
}
else
{
outputstring = text;
}
SizeF returnSize = g.MeasureString(outputstring, font);
bChanging = true;
this.Text = outputstring;
bChanging = false;
return returnSize;
}
private void InitializeComponent()
{
bChanging = false;
}
protected override void OnTextChanged(EventArgs e)
{
if(!bChanging)
{
Bitmap bmp = new Bitmap(175,175);
Graphics graphics = Graphics.FromImage(bmp);
SizeF size = new SizeF();
size = MeasureStringExtended(graphics,this.Text,this.Font,175);
this.Height = (int)size.Height;
base.OnTextChanged (e);
}
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MyControls
{
/// <summary>
/// A label that supports vertical auto resizing.
/// </summary>
public class Label : System.Windows.Forms.Label
{
private bool bChanging = false;
private SizeF MeasureStringExtended(Graphics g, string text, Font font, int desWidth)
{
string tempString = text;
string workString = "";
string outputstring = "";
int npos = 1;
int sp_pos = 0;
int sp_pos1 = 0;
int sp_pos2 = 0;
int sp_pos3 = 0;
bool bNeeded = false;
int line = 0;
int nWidth = 0;
//get original size
SizeF size = g.MeasureString(text, font);
if (size.Width > desWidth)
{
while(tempString.Length > 0)
{
//Check for the last lane
if (npos > tempString.Length)
{
outputstring = outputstring + tempString;
line++;
break;
}
workString = tempString.Substring(0, npos);
//get the current width
nWidth = (int)g.MeasureString(workString, font).Width;
//check if we've got out of the destWidth
if (nWidth > desWidth)
{
//try to find a space
sp_pos1 = workString.LastIndexOf(" ");
sp_pos2 = workString.LastIndexOf(";");
sp_pos3 = workString.IndexOf("\r\n");
if (sp_pos3 > 0)
{
sp_pos = sp_pos3;
bNeeded = false;
}
else
{
if ((sp_pos2 > 0) && (sp_pos2 > sp_pos1))
{
sp_pos = sp_pos2;
bNeeded = true;
}
else if (sp_pos1 > 0)
{
sp_pos = sp_pos1;
bNeeded = true;
}
else
{
sp_pos = 0;
bNeeded = true;
}
}
if (sp_pos > 0)
{
//cut out the wrap lane we've found
outputstring = outputstring + tempString.Substring(0,sp_pos + 1);
if (bNeeded) outputstring = outputstring + "\r\n";
tempString = tempString.Substring((sp_pos + 1), tempString.Length - (sp_pos + 1));
line++;
npos = 0;
}
else //no space
{
outputstring = outputstring + tempString.Substring(0,npos + 1);
if (bNeeded) outputstring = outputstring + "\r\n";
tempString = tempString.Substring(npos, tempString.Length - npos);
line++;
npos = 0;
}
}
npos++;
}
}
else
{
outputstring = text;
}
SizeF returnSize = g.MeasureString(outputstring, font);
bChanging = true;
this.Text = outputstring;
bChanging = false;
return returnSize;
}
private void InitializeComponent()
{
bChanging = false;
}
protected override void OnTextChanged(EventArgs e)
{
if(!bChanging)
{
Bitmap bmp = new Bitmap(175,175);
Graphics graphics = Graphics.FromImage(bmp);
SizeF size = new SizeF();
size = MeasureStringExtended(graphics,this.Text,this.Font,175);
this.Height = (int)size.Height;
base.OnTextChanged (e);
}
}
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询