2006. január 19., csütörtök

Drawing components' bitmaps appeared in Delphi palette


Problem/Question/Abstract:

How to draw components' bitmaps appeared in Delphi palette?

Answer:

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;

Nincsenek megjegyzések:

Megjegyzés küldése