Easiest way to make a button with just a image

筅森魡賤 提交于 2019-12-04 13:48:30
Tommy Andersen

What you want is a transparent control that inherits from TWinControl since you want it to be able to retrieve focus, this has never been an easy task. However since recent versions Embarcadero has provided a control that provides this. The TCustomTransparentControl is a TWinControl descendent that makes the task a bit easier for you.

So, what I would do is to create a new component, and inherit it from TCustomTransparentControl, then what I would do is to overwrite the Paint method like this:

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;

By this approach you should be able to get the transparency and translucency you are looking for. However you still need to handle the situation where the button is disabled, pressed, etc.

You could use TImage and assign the OnClick event to mimic a button. It depends if you need to receive focus or not.

WeGoToMars

You could use TPanel and assign the OnClick event to mimic a button. Set the borders of the panel to 'flat' to make it look like there is no panel.

It is similar to the solution proposed by stukelly but it is easier to implement the enabled and hover features. For example, on hover, you can make the panel look 3D.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!