随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253

首先: 这有现成的控件: TComboBoxEx, 没必要使用 TComboBox; 非要在 TComboBox 基础上添加, 也可以.

本例效果图:



代码文件:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ImgList, ComCtrls;

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    ImageList1: TImageList;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure FormDestroy(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  Bits: array[0..3] of TBitmap;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to Length(Bits)-1 do
  begin
    Bits[i] := TBitmap.Create;
    ImageList1.GetBitmap(i, Bits[i]);
    ComboBox1.Items.Add(Format('Item %d', [i]));
  end;

  ComboBox1.Style := csOwnerDrawVariable;
  ComboBox1.ItemHeight := Bits[0].Height + 4;
  ComboBox1.ItemIndex := 0;
end;

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  ComboBox1.Canvas.FillRect(Rect);

  ComboBox1.Canvas.BrushCopy(Bounds(Rect.Left+2, Rect.Top+2, Bits[Index].Width, Bits[Index].Height), 
                             Bits[Index],
                             Bounds(0, 0, Bits[Index].Width, Bits[Index].Height),
                             clBlack);
  ComboBox1.Canvas.TextOut(Rect.Left + 24, Rect.Top + 3, ComboBox1.Items[Index]);
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to Length(Bits)-1 do
    Bits[i].Free;
end;

end.

窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 149
  ClientWidth = 222
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object ComboBox1: TComboBox
    Left = 24
    Top = 8
    Width = 145
    Height = 21
    ItemHeight = 13
    TabOrder = 0
    OnDrawItem = ComboBox1DrawItem
  end
  object ImageList1: TImageList
    Left = 48
    Top = 48
    Bitmap = {
      494C010104000A00040010001000FFFFFFFFFF00FFFFFFFFFFFFFFFF424D3600
      0000000000003600000028000000400000002000000001002000000000000020
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      00000000000000000000000000000000000000000000AD9E9400AD9E9400AD9E
      9400AD9E9400AD9E9400AD9E9400AD9E9400AD9E9400AD9E9400AD9E9400AD9E
      9400AD9E9400AD9E9400AD9E9400000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      00000000000000000000000000000000000000000000A5968400E7DFD600E7D7
      D600DED7CE00DECFC600D6CFBD00D6C7BD00E7D7D6007B5131008C4918008C49
      1800C6BEB500CEBEAD00A5968400000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      000000000000000000000000000000000000000000000000000000000000EFF3
      F70094BE9C00297D31000069080000650800005D0000186121006B8E6B00D6DF
      D60000000000000000000000000000000000000000000000000000000000EFF3
      F70094BE9C00297D31000069080000650800005D0000186121006B8E6B00D6DF
      D6000000000000000000000000000000000000000000A5968400E7E7DE00E7D7
      D600DED7CE00DED7CE00D6CFC600D6CFBD00E7D7D6007B3818008C3800008C38
      0000C6BEB500CEBEAD00A5968400000000003130DE003130DE00A5A6FF00A5A6
      FF00A5A6FF00A5A6FF00A5A6FF00A5A6FF00A5A6FF00A5A6FF00A5A6FF00A5A6
      FF00A5A6FF00A5A6FF003130DE003130DE000000000000000000C6DBC600318A
      3100089A210010B6310008B6210008B2180000B2100000AE0800009208001065
      100094AE9C000000000000000000000000000000000000000000C6DBC600318A
      3100089A210010B6310008B6210008B2180000B2100000AE0800009208001065
      100094AE9C0000000000000000000000000000000000A5968400EFE7DE00E7E7
      DE00E7D7D600DED7CE00DED7CE00D6CFC600E7D7D60073412900943800008C38
      0000C6BEB500CEBEAD00A5968400000000003130DE003130DE00E7E7FF000000
      0000000000000000000000000000000000000000000000000000000000000000
      000000000000E7E7FF003130DE003130DE0000000000D6E3D600108A210018B6
      420018BA420018BA390010B6310039C3520018B6290008B2180000AE100000AE
      08000069000094AE9C00000000000000000000000000D6E3D600108A210018B6
      420018BA420018BA390010B6310039C3520018B6290008B2180000AE100000AE
      08000069000094AE9C00000000000000000000000000A5968400EFE7E700F7EF
      EF00F7EFEF00EFEFEF00EFEFEF00DECFCE00E7D7D60084A6A500944110009441
      0800C6BEB500CEBEAD00A5968400000000003130DE003130DE00A5A6FF00E7E7
      FF00000000000000000000000000000000000000000000000000000000000000
      0000E7E7FF00A5A6FF003130DE003130DE00EFF3F70042A2420021B6520021BE
      5A0021CB4A0018CF4A0073E39400E7FBE7006BE38C0021D3420018C7290008B2
      180000AE080010691000D6DFD60000000000EFF3F70042A2420021B6520021BE
      5A0021BE520018BA420073D38C0000000000B5EBBD0008B6210008B2180008B2
      180000AE080010691000D6DFD6000000000000000000A5968400EFEFE7000000
      00008C8684006359520052413900DED7D600E7DFD6009C968C00944918009449
      1800CEBEB500CEBEAD00A5968400000000003130DE003130DE00E7E7FF00A5A6
      FF00E7E7FF0000000000000000008486F7008486F7000000000000000000E7E7
      FF00A5A6FF00E7E7FF003130DE003130DE00B5DBB50018A6390029C36B0021CB
      520021D3520029CB6300A5E7BD00000000000000000073DF940021D34A0010C7
      290008B21800009610005A8A5A0000000000B5DBB50018A6390029C36B0029C3
      630021BE5A0073D7940000000000000000009CE3AD0010B6310010B6290008B2
      210008B21800009610005A8A5A000000000000000000A5968400EFEFE7000000
      00008C867B00A5968C0052414200E7D7D600E7DFD6007B5942008C5939009C49
      2100C6C7C600CEBEB500A5968400000000003130DE003130DE0000000000E7E7
      FF00A5A6FF00B5B6FF008486F7005A59EF005A59EF008486F700B5B6FF00A5A6
      FF00E7E7FF00000000003130DE003130DE006BBE6B0029BA5A0029C36B0021D3
      4A0029C7630029C36B0039C77300DEF7E700000000000000000073DF940018CF
      420010B6290008B2180021792100000000006BBE6B0029BA5A0029C36B0029C3
      6B007BDBA5000000000000000000ADE7BD0018BA4A0018BA420010BA390010B6
      310010B6290008B218002179210000000000A5AEAD009C968C00CEC7B5000000
      00006B6963005A494A0042383900E7DFD600E7DFDE005A4129006B4118006338
      2100CEC7BD00C6B6AD009C968400A5AEAD003130DE003130DE00000000000000
      0000B5B6FF008486F7005A59EF003130DE003130DE005A59EF008486F700B5B6
      FF0000000000000000003130DE003130DE0039AE390029C36B0029C36B007BE7
      8C0094E3B50094E3B50094E3B500A5E7BD000000000000000000000000006BDF
      840010B6310010B63100007910000000000039AE390029C36B0029C36B007BDB
      A5000000000000000000F7FBF70094DFAD0094DFAD008CDFA5008CDFA5008CDF
      9C0010B6310010B631000079100000000000C6513900F7D7CE0094968C00CEC7
      B500EFE7DE00EFE7E700EFE7E700E7E7DE00E7E7DE00DED7D600D6D7CE00D6CF
      C600C6B6AD009C968400E7D7D600C65139003130DE003130DE00000000000000
      00008486F7005A59EF003130DE00B5B6FF00B5B6FF003130DE005A59EF008486
      F70000000000000000003130DE003130DE0039B6390029C36B004ACB7B00EFFF
      EF0000000000000000000000000000000000000000000000000000000000EFFF
      EF0018BA420018BA3900088E18000000000039B6390029C36B004ACB7B000000
      0000000000000000000000000000000000000000000000000000000000000000
      000018BA420018BA3900088E180000000000A59E9C00C6513900F7D7CE009496
      8C00CEC7B500E7DFDE00EFE7E700EFE7DE00EFE7DE00E7D7D600D6CFC600C6BE
      AD009C968400EFD7D600C6513900ADA6A5003130DE003130DE00000000008486
      F7005A59EF003130DE00B5B6FF000000000000000000B5B6FF003130DE005A59
      EF008486F700000000003130DE003130DE0039BA390031C36B0029C36B0073E3
      84008CDFA5008CDFA50094DFAD0094DFAD00F7FBF70000000000000000006BDF
      8C0021BE520018BA4200088210000000000039BA390031C36B0029C36B007BDB
      A500000000000000000000000000A5E7BD0094E3B50094E3B50094E3B50094DF
      AD0021BE520018BA4200088210000000000000000000A59E9C00C6513900F7D7
      CE0094968C00CEC7B500E7DFDE00EFE7E700EFE7DE00DED7CE00C6BEB5009C96
      8400EFD7D600C6513900ADA6A500000000003130DE003130DE008486F7005A59
      EF003130DE00B5B6FF0000000000000000000000000000000000B5B6FF003130
      DE005A59EF008486F7003130DE003130DE007BD37B0042C7630042C7730010CB
      310010BE310018BA420018BA4A00ADE7BD00000000000000000073DF940021D3
      520021BE5A0021BE5A0031963900000000007BD37B0042C7630042C7730029C3
      6B007BDBA5000000000000000000DEF7E70039C7730029C36B0029C36B0029C3
      630021BE5A0021BE5A0031963900000000000000000000000000A5716300C651
      3900F7D7C600948E8C00CEC7B500E7D7D600DED7D600CEBEB5009C968400EFD7
      D600C6513900ADA6A50000000000000000003130DE003130DE003130DE003130
      DE00B5B6FF00000000000000000000000000000000000000000000000000B5B6
      FF003130DE003130DE003130DE003130DE00D6EBD60031BE390073D7940039CF
      5A0010C7290010BA29009CE3AD0000000000000000006BDB8C0018CF4A0029CB
      5A0029C36B0018A639008CBE8C0000000000D6EBD60031BE390073D7940052CF
      840031C773007BDBA5000000000000000000A5E7BD0029C36B0029C36B0029C3
      6B0029C36B0018A639008CBE8C00000000000000000000000000A5716300CE41
      2100C6513900FFD7C60094968C00CEC7B500CEC7B5009C968400EFD7D600C651
      3900ADA6A5000000000000000000000000003130DE003130DE003130DE008486
      F700A5A6FF00A5A6FF00A5A6FF00A5A6FF00A5A6FF00A5A6FF00A5A6FF00A5A6
      FF008486F7003130DE003130DE003130DE00F7F3F7006BD36B0073D7840084DB
      A50042D3630018CB310094EBA500EFFFEF005ADB730018CF390021CB520029C3
      6B0029BE630021962900E7EFE70000000000F7F3F7006BD36B0073D7840084DB
      A50063D38C0042CB7B0084DBA500F7FBF7008CDFAD0029C36B0029C36B0029C3
      6B0029BE630021962900E7EFE700000000000000000000000000C6301800AD18
      080063080000C6513900EFD7CE009496840094968C00E7D7D600C6513900ADA6
      A500000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      00000000000000000000000000000000000000000000E7F3E70039C739007BD7
      84009CE3AD0073D79C005ACF840039C7730029C36B0029C36B0029C36B0029BE
      5A00189E2100C6E3C600000000000000000000000000E7F3E70039C739007BD7
      84009CE3AD0073D79C005ACF840039C7730029C36B0029C36B0029C36B0029BE
      5A00189E2100C6E3C60000000000000000000000000000000000FF514A00DE41
      31009C080000AD9E9C00C6513900FFCFC600FFCFC600C6513900ADA6A5000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000E7F3E7006BD3
      6B004ACB4A0084DB94008CDFA5006BD394004ACB840029BE630018B2310042B2
      4200C6E3C6000000000000000000000000000000000000000000E7F3E7006BD3
      6B004ACB4A0084DB94008CDFA5006BD394004ACB840029BE630018B2310042B2
      4200C6E3C6000000000000000000000000000000000000000000DE412900CE38
      2100CE38210000000000AD9E9C00C6513900C6513900ADA6A500000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      000000000000000000000000000000000000000000000000000000000000F7F3
      F700D6EFD6007BD77B0039C7390039C3390039BE39006BCB6B00B5E3B500F7F3
      F70000000000000000000000000000000000000000000000000000000000F7F3
      F700D6EFD6007BD77B0039C7390039C3390039BE39006BCB6B00B5E3B500F7F3
      F70000000000000000000000000000000000424D3E000000000000003E000000
      2800000040000000200000000100010000000000000100000000000000000000
      000000000000000000000000FFFFFF0000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      0000000000000000000000000000000000000000000000000000000000000000
      000000000000000000000000000000008001FFFFFFFFFFFF8001FFFFE00FE00F
      80010000C007C00780011FF88003800380010FF0000101019001066001810301
      9001200400C106011000300C00E10C010000300C0FE11FF10000218400610E01
      800103C000C10601C00307E001810301C007000000010001C00FFFFF80038003
      C01FFFFFC007C007C43FFFFFE00FE00F}
  end
end

posted on 2008-09-08 11:43  万一  阅读(...)  评论(...编辑  收藏