2009. március 21., szombat

Paint a TBitmap in disabled state


How can I make an image to appear enabled and disabled (i.e going from original to disabled-grey and back)? I know that TImage doesn't support this feature, so how would I implement this?


Everyone from you saw that standard TSpeedButton allow to show a loaded glyph in "disabled" state when your original glyph will be converted into gray-scheme.

Sometimes to create similar bitmap is useful not only for TSpeedButton.

You can use the next my CreateDisabledBitmap procedure where such "disabled" bitmap (Destination parameter) will be created from your original bitmap (Source).

procedure CreateDisabledBitmap(Source, Destination: TBitmap);
  ROP_DSPDxax = $00E20746;
  DDB, MonoBmp: TBitmap;
  IWidth, IHeight: Integer;
  IRect: TRect;
  IWidth := Source.Width;
  IHeight := Source.Height;
  Destination.Width := IWidth;
  Destination.Height := IHeight;
  IRect := Rect(0, 0, IWidth, IHeight);
  Destination.Canvas.Brush.Color := clBtnFace;
  Destination.Palette := CopyPalette(Source.Palette);
  MonoBmp := nil;
  DDB := nil;
    MonoBmp := TBitmap.Create;
    DDB := TBitmap.Create;
    DDB.HandleType := bmDDB;
    { Create a disabled version }
    with MonoBmp do
      HandleType := bmDDB;
      Canvas.Brush.Color := clBlack;
      Width := IWidth;
      if Monochrome then
        Canvas.Font.Color := clWhite;
        Monochrome := False;
        Canvas.Brush.Color := clWhite;
      Monochrome := True;
    with Destination.Canvas do
      Brush.Color := clBtnFace;
      Brush.Color := clBtnHighlight;
      SetTextColor(Handle, clBlack);
      SetBkColor(Handle, clWhite);
      BitBlt(Handle, 1, 1, IWidth, IHeight, MonoBmp.Canvas.Handle, 0, 0, ROP_DSPDxax);
      Brush.Color := clBtnShadow;
      SetTextColor(Handle, clBlack);
      SetBkColor(Handle, clWhite);
      BitBlt(Handle, 0, 0, IWidth, IHeight, MonoBmp.Canvas.Handle, 0, 0, ROP_DSPDxax);

Sample of use:

procedure TfrmMain.ButtonClick(Sender: TObject);
  Destination: TBitmap;
  Destination := TBitmap.Create;
    CreateDisabledBitmap(Image1.Picture.Bitmap, Destination);

where Image1 is TImage where you have an original bitmap and TImage2 will a container for created disabled bitmap.

Nincsenek megjegyzések:

Megjegyzés küldése