全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:5854
推到 Plurk!
推到 Facebook!

How to draw components' bitmaps appeared in Delphi

 
axsoft
版主


發表:681
回覆:1056
積分:969
註冊:2002-03-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-02-11 13:32:08 IP:61.218.xxx.xxx 未訂閱
How to draw components' bitmaps appeared in Delphi palette?    資料來源: www.delphi3000.com    Example below demonstrates drawing components images in combo box items. Combo box is filled with names of all components placed on form. See comments in source for description of each action.     Source: 
// Add LibIntf unit into uses clause of your unit.     // Add this definition for Delphi version detection, just for better reading 
{$IFDEF VER90} 
  {$DEFINE DELPHI2} 
{$ENDIF}     {$IFDEF VER100} 
  {$DEFINE DELPHI3} 
{$ENDIF}     {$IFDEF VER120} 
  {$DEFINE DELPHI3} 
  {$DEFINE DELPHI4} 
{$ENDIF}     {$IFDEF VER130} 
  {$DEFINE DELPHI3} 
  {$DEFINE DELPHI4} 
{$ENDIF}     procedure TDlgForm.cbComponentsDrawItem(Control: TWinControl; 
Index: Integer; Rect: TRect; State: TOwnerDrawState); 
var 
ARect: TRect; 
{$IFDEF DELPHI3} 
PIt: LibIntf.TIPaletteItem; 
{$ENDIF} 
{$IFDEF DELPHI4} 
PIt: LibIntf.IPaletteItem; 
{$ENDIF} 
B, tmpBmp: TBitmap; 
{$IFDEF DELPHI2 } 
tmpImList: TImageList; 
{$ENDIF} 
SName: string; 
begin 
  with cbComponents.Canvas do begin         Brush.Color := cbComponents.Color; 
    Brush.Style := bsSolid; 
    FillRect(Rect); 
    tmpBmp := TBitmap.Create; 
    B := TBitmap.Create; 
    try 
      SName := cbComponents.Items[Index]; 
      B.Width:=24; B.Height:=24; 
      {$IFDEF DELPHI3}     
//Under Delphi 3 or later we should use routines from LibIntf unit 
      PIt:=LibIntf.DelphiIDE.GetPaletteItem(TComponentClass(GetClass(Comp.Owner.FindComponent(SName).ClassName))); 
// Drawing component image on our bitmap canvas 
      PIt.paint(B.Canvas,0,0); 
      tmpBmp.Assign(B); 
      {$ENDIF} 
      ARect := Bounds(0, 0, 24, 24); 
      {$IFDEF DELPHI2} 
// Detecting the class name of component 
      SName := TComponent(Comp.Owner.FindComponent(SName)).ClassName; 
// Loading bitmap image from resources since it is linked to cmplib32.dcl 
      B.LoadFromResourceName(hInstance, UpperCase(PChar(SName))); 
      tmpBmp.Width:=22; tmpBmp.Height:=22; 
      BitBlt(tmpBmp.Canvas.Handle, 0, 0, 22, 22,B.Canvas.Handle, 2, 2, SRCCOPY); 
      tmpImList := TImageList.CreateSize(22, 22); 
      try 
        tmpImList.AddMasked(tmpBmp, tmpBmp.TransparentColor); 
        tmpBmp.Canvas.Brush.Color := cbComponents.Color; 
        tmpBmp.Canvas.Brush.Style := bsSolid; 
        tmpBmp.Canvas.FillRect(ARect); 
        tmpImList.Draw(tmpBmp.Canvas, 0, 0, 0); 
      finally 
        tmpImList.Free; 
      end; 
    {$ENDIF} 
    {$IFDEF DELPHI3} 
    tmpBmp.Canvas.Brush.Color := cbComponents.Color; 
    tmpBmp.Canvas.FillRect(ARect); 
    tmpBmp.Width := 24; 
    tmpBmp.Height := 24; 
    BitBlt(tmpBmp.Canvas.Handle, 0, 0, 24, 24, B.Canvas.Handle, 4, 4, SRCCOPY); 
    {$ENDIF} 
// Drawing component image on the combo box canvas 
    Draw(Rect.Left   3, Rect.Top   2, tmpBmp); 
  finally 
    tmpBmp.Free; 
    B.Free; 
  End; 
// Drawing raised rectangle if item is selected 
  if odSelected in State then begin 
    ARect := Bounds(Rect.Left   1, Rect.Top   1, 23, 23); 
    Frame3D(cbComponents.Canvas, ARect, clBtnHighlight, clBtnShadow, 1); 
    Font.Color := clHighlightText; 
    Brush.Color := clHighlight; 
    ARect := Bounds(26, Rect.Top, Rect.Right - Rect.Left - 26, Rect.Bottom - Rect.Top); 
    FillRect(ARect); 
  end 
  else 
    Font.Color := clBlack; 
  Brush.Style := bsClear; 
  TextOut(Rect.Left   27, Rect.Top   4, cbComponents.Items[Index]);       end;     end; 
聯盟----Visita網站http://www.vista.org.tw ---[ 發問前請先找找舊文章 ]---
系統時間:2024-11-21 16:49:53
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!