The easiest way to make a button with an image

I use Delphi XE, and I would like to make a button that shows only the provided PNG image with a transparent background and without additional fields of any type.

I tried to do this with TButton, but I got an ugly gray background with bsPushButton style. If I use the bsCommandLink style, there is a top edge of 10 pixels, although all of my ImageMargins settings are set to 0.

What would be the easiest way to do this?

EDIT: It should not look like a button. I just need it to look exactly like the image to which it is assigned. Preferably, it should be in a tab stop state and have different states (on, off, hover ...) so that I can assign an appropriate image for each state.

+3
source share
3 answers

What you want is a transparent control that inherits from TWinControl, since you want it to be able to get focus, it has never been an easy task. However, since recent versions of Embarcadero have provided a control that provides this. TCustomTransparentControl- This is a descendant TWinControlthat makes the task a little easier for you.

, , TCustomTransparentControl, , Paint :

procedure TMyTransparentButton.Paint;
var
  rc: TRect;
begin
  if not (csDestroying in ComponentState) then
  begin
    // Specify size and location of the image.
    rc := Rect(0, 0, pngImage.Width, pngImage.Height);

    // Draw the image on the canvas.
    pngImage.Draw(Canvas, rc);
  end;
end;

, . , , ..

+6
+5

TPanel OnClick, . "flat", , .

, stukelly, enabled hover. , 3D.

+1

All Articles