winforms 对齐组合框中的文本

ztigrdn8  于 2023-04-21  发布在  其他
关注(0)|答案(5)|浏览(164)

我想在组合框中对齐我的文本,以便它会显示在组合框的中心告诉我如何做到这一点,你也可以看到有一个默认的边界周围的组合框时,它是在重点如何我可以删除该边界也亲切地解决我的两个问题谢谢

gywdnpxw

gywdnpxw1#

本文将帮助您:http://blog.michaelgillson.org/2010/05/18/left-right-center-where-do-you-align/
诀窍是将ComboBox的DrawMode-Property设置为OwnerDrawFixed,并订阅其事件DrawItem
您的事件应包含以下代码:

// Allow Combo Box to center aligned
private void cbxDesign_DrawItem(object sender, DrawItemEventArgs e)
{
  // By using Sender, one method could handle multiple ComboBoxes
  ComboBox cbx = sender as ComboBox;
  if (cbx != null)
  {
    // Always draw the background
    e.DrawBackground();

    // Drawing one of the items?
    if (e.Index >= 0)
    {
      // Set the string alignment.  Choices are Center, Near and Far
      StringFormat sf = new StringFormat();
      sf.LineAlignment = StringAlignment.Center;
      sf.Alignment = StringAlignment.Center;

      // Set the Brush to ComboBox ForeColor to maintain any ComboBox color settings
      // Assumes Brush is solid
      Brush brush = new SolidBrush(cbx.ForeColor);

      // If drawing highlighted selection, change brush
      if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
        brush = SystemBrushes.HighlightText;

      // Draw the string
      e.Graphics.DrawString(cbx.Items[e.Index].ToString(), cbx.Font, brush, e.Bounds, sf);
    }
  }
}

要右对齐项目,只需将StringAlignment.Center替换为StringAlignment.Far

8ljdwjyq

8ljdwjyq2#

ComboBox不支持这个功能。确切的原因在时间的迷雾中消失了,ComboBox从90年代早期就已经存在了,但肯定与让文本框部分的文本与下拉列表中的文本对齐的尴尬有关。使用DrawItem的自定义绘图也不能解决这个问题;这只会影响下拉项的外观。
作为一种可行的解决方法,你可以做一些奇怪的事情,比如用空格填充项字符串,这样它们看起来就在中间了。你需要TextRenderer.MeasureText()来计算为每个项添加多少空格。
你所说的“边框”不是边框,它是焦点框。你也不能摆脱它。Windows拒绝让你创建一个不显示焦点控件的UI。喜欢键盘而不是鼠标的用户关心这一点。没有解决方案。

9udxz4iz

9udxz4iz3#

RightToLeft属性设置为true
它不会反转字符的顺序,只会右对齐。

k3fezbri

k3fezbri4#

这篇文章有点旧,但仍然值得一提:
对于Windows窗体ComboBox,这两种要求都是可能的:

*文本居中对齐(文本区域和下拉框)

  • 对于文本区域,找到Edit控件并设置该控件的ES_CENTER样式。
  • 对于下拉项或下拉模式中的选定项,要将文本居中对齐,只需使控件为所有者绘制并将文本绘制在中心即可。
    *去掉焦点框
  • 使控件为所有者绘制,但不绘制焦点矩形。
    示例
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class MyComboBox : ComboBox
{
    public MyComboBox()
    {
        DrawMode = DrawMode.OwnerDrawFixed;
    }

    [DllImport("user32.dll")]
    static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    const int GWL_STYLE = -16;
    const int ES_LEFT = 0x0000;
    const int ES_CENTER = 0x0001;
    const int ES_RIGHT = 0x0002;
    [StructLayout(LayoutKind.Sequential)]
    public struct RECT
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
        public int Width { get { return Right - Left; } }
        public int Height { get { return Bottom - Top; } }
    }
    [DllImport("user32.dll")]
    public static extern bool GetComboBoxInfo(IntPtr hWnd, ref COMBOBOXINFO pcbi);

    [StructLayout(LayoutKind.Sequential)]
    public struct COMBOBOXINFO
    {
        public int cbSize;
        public RECT rcItem;
        public RECT rcButton;
        public int stateButton;
        public IntPtr hwndCombo;
        public IntPtr hwndEdit;
        public IntPtr hwndList;
    }
    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        SetupEdit();
    }
    private int buttonWidth = SystemInformation.HorizontalScrollBarArrowWidth;
    private void SetupEdit()
    {
        var info = new COMBOBOXINFO();
        info.cbSize = Marshal.SizeOf(info);
        GetComboBoxInfo(this.Handle, ref info);
        var style = GetWindowLong(info.hwndEdit, GWL_STYLE);
        style |= 1;
        SetWindowLong(info.hwndEdit, GWL_STYLE, style);
    }
    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        base.OnDrawItem(e);
        e.DrawBackground();
        var txt = "";
        if (e.Index >= 0)
            txt = GetItemText(Items[e.Index]);
        TextRenderer.DrawText(e.Graphics, txt, Font, e.Bounds,
            ForeColor, TextFormatFlags.Left | TextFormatFlags.HorizontalCenter);
    }
}
rjzwgtxy

rjzwgtxy5#

您可以通过在查询中的Display成员之前添加空格来执行类似的操作
例如:

combobox1.DataSource = Query(Select col1 , ('   '+col2) as Col2 from tableName) 
combobox1.DisplayMember = "Col2";
combobox1.ValueMember = "col1";

相关问题