(*//
标题:颜色列表转换到图形列表
说明:用于利用菜单选择颜色
设计:Zswang
日期:2002-03-31
支持:wjhu111@21cn.com
//*)
///////Begin Source
function ColorsToImageList(mColors: array of TColor;
mImageList: TImageList; mAppend: Boolean = False): Boolean;
{ 将颜色列表转换到图形列表中 }
var
I: Integer;
vBitmap: TBitmap;
begin
Result := False;
if not Assigned(mImageList) then Exit;
if not mAppend then mImageList.Clear;
vBitmap := TBitmap.Create;
try
vBitmap.Height := mImageList.Height;
vBitmap.Width := mImageList.Width;
for I := Low(mColors) to High(mColors) do begin
vBitmap.Canvas.Pen.Color := mColors[I];
vBitmap.Canvas.Brush.Color := mColors[I];
vBitmap.Canvas.FillRect(Rect(0, 0, vBitmap.Width, vBitmap.Height));
mImageList.Add(vBitmap, nil);
end;
finally
vBitmap.Free;
end;
Result := True;
end; { ColorsToImageList }
///////End Source
///////Begin Demo
procedure TForm1.FormCreate(Sender: TObject);
var
vMenuItem: TMenuItem;
vColor: TColor;
I: Integer;
begin
Randomize;
PopupMenu := PopupMenu1;
PopupMenu1.Images := ImageList1;
for I := 1 to 10 do begin
vColor := RGB(Random(256), Random(256), Random(256));
vMenuItem := TMenuItem.Create(Self);
vMenuItem.Caption := ColorToString(vColor);
vMenuItem.ImageIndex := ImageList1.Count;
vMenuItem.OnClick := MenuItemClick;
ColorsToImageList([vColor], ImageList1, True);
PopupMenu1.Items.Add(vMenuItem);
end;
end;
procedure TForm1.MenuItemClick(Sender: TObject);
begin
Color := StringToColor(StripHotkey(TMenuItem(Sender).Caption));
end;
///////End Demo