// 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);
}
}
}
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);
}
}
5条答案
按热度按时间gywdnpxw1#
本文将帮助您:http://blog.michaelgillson.org/2010/05/18/left-right-center-where-do-you-align/
诀窍是将ComboBox的
DrawMode
-Property设置为OwnerDrawFixed
,并订阅其事件DrawItem
。您的事件应包含以下代码:
要右对齐项目,只需将
StringAlignment.Center
替换为StringAlignment.Far
。8ljdwjyq2#
ComboBox不支持这个功能。确切的原因在时间的迷雾中消失了,ComboBox从90年代早期就已经存在了,但肯定与让文本框部分的文本与下拉列表中的文本对齐的尴尬有关。使用
DrawItem
的自定义绘图也不能解决这个问题;这只会影响下拉项的外观。作为一种可行的解决方法,你可以做一些奇怪的事情,比如用空格填充项字符串,这样它们看起来就在中间了。你需要
TextRenderer.MeasureText()
来计算为每个项添加多少空格。你所说的“边框”不是边框,它是焦点框。你也不能摆脱它。Windows拒绝让你创建一个不显示焦点控件的UI。喜欢键盘而不是鼠标的用户关心这一点。没有解决方案。
9udxz4iz3#
将
RightToLeft
属性设置为true
。它不会反转字符的顺序,只会右对齐。
k3fezbri4#
这篇文章有点旧,但仍然值得一提:
对于Windows窗体ComboBox,这两种要求都是可能的:
*文本居中对齐(文本区域和下拉框)
Edit
控件并设置该控件的ES_CENTER
样式。*去掉焦点框
示例
rjzwgtxy5#
您可以通过在查询中的Display成员之前添加空格来执行类似的操作
例如: