写了一个控件
unit PicCombo;
interface
uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs,
StdCtrls, ImgList;
type
TPicCombo = class(TCustomComboBox)
private
FImages: TCustomImageList;
FImageChangeLink: TChangeLink;
procedure SetImages(const Value: TCustomImageList);
procedure ImageListChange(Sender: TObject);
protected
procedure DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState); override;
procedure MeasureItem(Index: Integer; var Height: Integer); override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure CalcSize;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Images: TCustomImageList read FImages write SetImages;
property Anchors;
property Color;
property Constraints;
property Ctl3D;
property DragCursor;
property DragKind;
property DragMode;
property DropDownCount;
property Enabled;
property ImeMode;
property ImeName;
property ParentColor;
property ParentCtl3D;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnChange;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnDropDown;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnStartDock;
property OnStartDrag;
end;
procedure Register;
implementation
uses
CommCtrl;
procedure Register;
begin
RegisterComponents('Samples', [TPicCombo]);
end;
{ TPicCombo }
procedure TPicCombo.CalcSize;
var
i: Integer;
begin
if Assigned(FImages) then begin
Height := FImages.Height + 4;
ItemHeight := FImages.Height;
Width := GetSystemMetrics(SM_CXHSCROLL) + 4 + FImages.Width;
Items.Clear;
for i := 0 to FImages.Count - 1 do
Items.Add('');
end;
end;
constructor TPicCombo.Create(AOwner: TComponent);
begin
inherited;
Style := csOwnerDrawFixed;
FImageChangeLink := TChangeLink.Create;
FImageChangeLink.OnChange := ImageListChange;
end;
destructor TPicCombo.Destroy;
begin
FImageChangeLink.Free;
inherited;
end;
procedure TPicCombo.DrawItem(Index: Integer; Rect: TRect;
State: TOwnerDrawState);
begin
if not Assigned(FImages) then Exit;
if [odSelected, odFocused] * State <> [] then
Canvas.Brush.Color := clHighlight
else Canvas.Brush.Color := Color;
Canvas.Brush.Style := bsSolid;
Canvas.FillRect(Rect);
ImageList_Draw(FImages.Handle, Index, Canvas.Handle,
Rect.Left, Rect.Top, ILD_NORMAL or ILD_TRANSPARENT);
end;
procedure TPicCombo.ImageListChange(Sender: TObject);
begin
if HandleAllocated and (Sender = Images) then CalcSize;
end;
procedure TPicCombo.MeasureItem(Index: Integer; var Height: Integer);
begin
//do nothing
end;
procedure TPicCombo.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited;
if (AComponent = FImages) and (Operation = opRemove) then
Images := nil;
end;
procedure TPicCombo.SetImages(const Value: TCustomImageList);
begin
if Assigned(FImages) then
FImages.UnRegisterChanges(FImageChangeLink);
FImages := Value;
if Assigned(FImages) then begin
FImages.RegisterChanges(FImageChangeLink);
FImages.FreeNotification(Self);
end;
CalcSize;
end;
end.