如何使用c#.net使Listbox的文本中心在桌面应用程序中对齐

Dr.*_*len 6 c# listbox alignment winforms

请告诉我如何将中心与桌面应用程序中的ListBox文本对齐.
我在Visual Studio 2005中使用C#.Net.

我正在使用Windows表单.

C.E*_*uis 8

您可以将DrawModeListBox 的属性设置为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)); 
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 是.但是请使用TextRenderer.DrawText. (2认同)