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

listBox如何让item内容居中?
listBox如何让item居中显示呢?查了半天,没找到相关资料,谢谢

------解决方案--------------------
重载ListBox的OnDrawItem方法。

protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();

System.Drawing.StringFormat strFmt = new System.Drawing.StringFormat(System.Drawing.StringFormatFlags.NoClip);
strFmt.Alignment = System.Drawing.StringAlignment.Center;

e.Graphics.DrawString(this.Items[e.Index].Text, e.Font, new System.Drawing.SolidBrush(e.ForeColor), e.Bounds, strFmt);

base.OnDrawItem(e);
}