首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

请教Combobox怎么设置文本垂直居中

2012-02-26 
请问Combobox如何设置文本垂直居中?是在DrawItem里写吗?但我不知怎么写,网上也找不到相关的,麻烦大家[解决

请问Combobox如何设置文本垂直居中?
是在DrawItem里写吗?但我不知怎么写,网上也找不到相关的,麻烦大家

[解决办法]
设ComboBox1的Style为csOwnerDrawVariable
然后在它的DrawItem程序中:
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
p:integer;
begin
p:=(combobox1.ItemHeight-combobox1.Canvas.TextHeight(combobox1.Items[Index])) div 2;
combobox1.Canvas.TextOut(Rect.Left,Rect.Top+p,combobox1.Items[Index]);
end;
由于每个字符的实际高度不一样(如a与b,后者要比前者实际高些:ab),所以结果可能不会完全居中(如全部是text为aaaa的)

热点排行