我试图在winforms中绘制一个ComboBox,我想保持21的高度,但每当我打开userpaint时,ComboBox的高度增加到24,我无法改变它。
protected override void OnDropDownStyleChanged( EventArgs Args ) {
base.OnDropDownStyleChanged( Args );
SetStyle( ControlStyles.UserPaint, DropDownStyle == ComboBoxStyle.DropDownList );
}
注释掉SetStyle行将使高度表现得如此,但我的重写OnPaint方法将不适用。有没有办法在ComboBox上绘制,同时仍保持其原始高度为21?
(编辑)我修好了这个:
public CustomComboBox( ) {
DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
}
(EDIT2)现在,文字未显示在我的下拉列表中。
(EDIT3)再次修复:
在此处找到修复程序:OwnerDrawVariable Combobox DropDown Blank Space
[我的版本]
protected override void OnDrawItem( DrawItemEventArgs Args ) {
Args.DrawBackground( );
Int32 Index = Args.Index;
if ( Index < 0 || Index >= Items.Count ) {
return;
}
using ( Brush ItemBrush = new SolidBrush( Args.ForeColor ) ) {
Rectangle ItemRectangle = new Rectangle( );
ItemRectangle.X = Args.Bounds.Left;
ItemRectangle.Y = Args.Bounds.Top + ( ( Args.Bounds.Height - ItemHeight ) / 2 );
ItemRectangle.Width = Args.Bounds.Width;
ItemRectangle.Height = ItemHeight;
Args.Graphics.DrawString( Items[ Args.Index ].ToString( ), Args.Font, ItemBrush, ItemRectangle );
}
Args.DrawFocusRectangle( );
return;
}