c# - 如何使用 c#.net 在桌面应用程序中使列表框文本居中对齐

标签 c# winforms listbox alignment

请告诉我如何在桌面应用程序中将中心与列表框的文本对齐。
我在 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/

相关文章:

c# - 在 WPF 中绘制一个可拉伸(stretch)的圆括号

c# - DotNetOpenAuth - 名称为 'OriginalHttpRequestUri' 的属性不存在异常

c# - 如何处理 MySqlDataAdapter 引入的数据集中单个列的数据?

c# - 如何使 PictureBox 使用最近邻重采样?

c# - 将 ListBox 放入 ScrollViewer : mouse wheel does not work

c# - 使用 Emgucv 比较两个图像

c# - 带有使用依赖注入(inject)创建的 View 模型的 Prism PopupWindowAction

c# - 现有图形转换为位图

c# - wpf 列表框行高

excel - 如何使用VBA在工作表中创建MSForms列表框?