在网上找了好久都没有较好的或者代码,都是说自己绘画上去就可以,我还是直接贴图吧
listbox 属性调为OwnerDrawVariable
//listbox内容居
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
StringFormat strFmt = new StringFormat
{
Alignment = StringAlignment.Center, //文本垂直居中
LineAlignment = StringAlignment.Center //文本水平居中
};
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds, strFmt);
}
listbox在form窗体居中
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
int x = (int)(0.5 * (this.Width - listBox1.Width));
int y = listBox1.Location.Y;
listBox1.Location = new Point(x, y);
}
本文介绍了一种在Windows Forms应用中使ListBox组件居中显示的方法,并通过自定义DrawItem事件实现ListBox项的垂直和水平居中。通过调整ListBox的位置和使用Graphics.DrawString方法结合StringFormat,可以确保ListBox在Form上的位置居中且内容居中。
1978





