请告诉我如何在桌面应用程序中将中心与列表框的文本对齐。
我在 Visual Studio 2005 中使用 C#.Net。
我正在使用 Windows 窗体。
最佳答案
您可以将 ListBox 的 DrawMode
属性设置为 DrawMode.OwnerDrawFixed
,这样您就可以控制每个项目的整个图形表示。例如:
ListBox listBox = new ListBox();
listBox.DrawMode = DrawMode.OwnerDrawFixed;
listBox.DrawItem += new DrawItemEventHandler(listBox_DrawItem);
void listBox_DrawItem(object sender, DrawItemEventArgs e)
{
ListBox list = (ListBox)sender;
if (e.Index > -1)
{
object item = list.Items[e.Index];
e.DrawBackground();
e.DrawFocusRectangle();
Brush brush = new SolidBrush(e.ForeColor);
SizeF size = e.Graphics.MeasureString(item.ToString(), e.Font);
e.Graphics.DrawString(item.ToString(), e.Font, brush, e.Bounds.Left + (e.Bounds.Width / 2 - size.Width / 2), e.Bounds.Top + (e.Bounds.Height / 2 - size.Height / 2));
}
}
关于c# - 如何使用 c#.net 在桌面应用程序中使列表框文本居中对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2943330/