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

ComboBox中加载图标,该如何解决

2012-03-12 
ComboBox中加载图标在style里设置csOwnerDrawFixed后,再下面代码加载图标...可这样ComboBox1.Items.Add就

ComboBox中加载图标
在style里设置csOwnerDrawFixed后,再下面代码加载图标...

可这样ComboBox1.Items.Add就起不了作用,编辑不了内容!   请问怎样才可以用Add添加内容   ?

procedure   TForm1.ComboBox1DrawItem(Control:   TWinControl;   Index:   Integer;
    Rect:   TRect;   State:   TOwnerDrawState);
var
    lBitmap:   TBitmap;
begin
    with   (Control   as   TCombobox).Canvas   do
    begin
        FillRect(Rect);
        lBitmap   :=TBitmap.Create;
        if   imagelist1.GetBitmap(index,lBitmap)   then
            BrushCopy(Bounds(Rect.Left   +   2,Rect.Top,lBitmap.Width,lBitmap.Height),lBitmap,Bounds(0,0,lBitmap.Width,lBitmap.Height),clRed);
    end;
end;

procedure   TForm1.FormActivate(Sender:   TObject);
begin
    if   ComboBox1.Items.Count   <>   ImageList1.Count   then
  begin
        ComboBox1.Items.Text   :=   StringOfChar(#13,   ImageList1.Count);
        Combobox1.ItemIndex   :=0;
  end;
end;



[解决办法]
没有画文字?

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
var
lBitmap: TBitmap;
begin
with (Control as TCombobox).Canvas do
begin
FillRect(Rect);
lBitmap := TBitmap.Create;
if imagelist1.GetBitmap(index, lBitmap) then
BrushCopy(Bounds(Rect.Left + 2, Rect.Top, lBitmap.Width, lBitmap.Height),
lBitmap, Bounds(0, 0, lBitmap.Width, lBitmap.Height), clRed);
//位置再自己调一下。
TextOut(Rect.Left,Rect.Top,(Control as TComboBox).Items[index]);
end;
end;

热点排行