☆TComboBoxExを使ってみる。
今まで、ComboBoxでインデントさせたり、イメージを表示するには、OnDrawItemで描画する必要がありましたが、 ComboBoxExを使うと簡単に表示できます。
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ImgList, StdCtrls, ComCtrls; type TForm1 = class(TForm) ComboBoxEx1: TComboBoxEx; ImageList1: TImageList; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} //テスト用アイコン作成 procedure MakeTestIcon(ImageList: TImageList); var Bitmap: TBitmap; begin Bitmap := TBitmap.Create; try Bitmap.Height := 16; Bitmap.Width := 16; Bitmap.Canvas.Brush.Color := clRed; Bitmap.Canvas.FillRect(Rect(2,2,14,14)); ImageList.Add(Bitmap,nil); Bitmap.Canvas.Brush.Color := clBlue; Bitmap.Canvas.FillRect(Rect(2,2,14,14)); ImageList.Add(Bitmap,nil); finally Bitmap.Free; end; end; function GetCategory(Index: Integer): String; begin case Index of 0: Result := 'Guitar'; 1: Result := 'Synthesizer'; else Result := 'Piano' end; end; procedure GetGuitarItem(ComboBoxEx: TComboBoxEx; Ident: Integer); var ComboExItem: TComboExItem; I: Integer; S: String; begin for I := 0 to 3 do begin case I of 0: S := 'Ibanez'; 1: S := 'Gibson'; 2: S := 'Yamaha'; else S := 'Fender'; end; ComboExItem := ComboBoxEx.ItemsEx.Add; ComboExItem.Caption := S; ComboExItem.Indent := Ident; ComboExItem.ImageIndex := 1; end; end; procedure GetSynthesizerItem(ComboBoxEx: TComboBoxEx; Ident: Integer); var ComboExItem: TComboExItem; I: Integer; S: String; begin for I := 0 to 2 do begin case I of 0: S := 'Roland'; 1: S := 'Yamaha'; else S := 'KORG'; end; ComboExItem := ComboBoxEx.ItemsEx.Add; ComboExItem.Caption := S; ComboExItem.Indent := Ident; ComboExItem.ImageIndex := 1; end; end; procedure GetPianoItem(ComboBoxEx: TComboBoxEx; Ident: Integer); var ComboExItem: TComboExItem; I: Integer; S: String; begin for I := 0 to 2 do begin case I of 0: S := 'Steinway & Sons'; 1: S := 'Yamaha'; else S := 'Kawai'; end; ComboExItem := ComboBoxEx.ItemsEx.Add; ComboExItem.Caption := S; ComboExItem.Indent := Ident; ComboExItem.ImageIndex := 1; end; end; procedure TForm1.FormCreate(Sender: TObject); var I: Integer; begin MakeTestIcon(ImageList1); ComboBoxEx1.Style := csExDropDownList; ComboBoxEx1.ItemsEx.Clear; ComboBoxEx1.ItemsEx.SortType := stNone; for I := 0 to 2 do begin ComboBoxEx1.ItemsEx.AddItem(GetCategory(I), 0, 0, -1, 0, nil); case I of 0: GetGuitarItem(ComboBoxEx1,1); 1: GetSynthesizerItem(ComboBoxEx1,1); else GetPianoItem(ComboBoxEx1,1); end; end; ComboBoxEx1.ItemIndex := 0; end; end.
実行するとこんな感じです。
参考
MSDN
ComboBoxEx Control Reference
| 固定リンク