最简单的方法来制作一个只有一个图像的按钮 [英] Easiest way to make a button with just a image

查看:204
本文介绍了最简单的方法来制作一个只有一个图像的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Delphi XE,我想制作一个按钮,它只显示提供的透明背景的PNG图像,没有任何额外的边距。



我试图用TButton做这个,但是我得到一个丑陋的灰色背景与bsPushButton风格。如果我使用bsCommandLink风格,那么有一个10像素的顶部边距,尽管我所有的ImageMargins设置都设置为0。



最简单的方法是使它发生什么? p>

编辑:不必像按钮一样。我只需要它看起来就像它分配的图像。优选地,它应该是一个制表符停止并具有各种状态(启用,禁用,悬停,...),所以我可以为每个状态分配适当的图像。

解决方案

你想要的是一个透明的控件,它继承自 TWinControl ,因为您希望它能够检索焦点,这从来不是一件容易的事情。但是,由于最近版本Embarcadero已经提供了一个提供此控件的控件。 TCustomTransparentControl 是一个

所以,我会做的是创建一个新组件,并从 TCustomTransparentControl 继承它,那么我将要做的是覆盖 Paint 方法,如下所示: / p>

 程序TMyTransparentButton.Paint; 
var
rc:TRect;
begin
如果没有(csDestroying在ComponentState中)然后
begin
//指定图像的大小和位置。
rc:= Rect(0,0,pngImage.Width,pngImage.Height);

//在画布上绘制图像。
pngImage.Draw(Canvas,rc);
结束
结束

通过这种方法,您应该能够获得所需的透明度和半透明度。然而,您仍然需要处理按钮被禁用,按下等的情况。


I'm using Delphi XE and I would like to make a button which shows just the provided PNG image with transparent background and no additional margins of any kind.

I tried to do this with TButton but I get an ugly gray background with bsPushButton style. If I use bsCommandLink style there is a 10 pixel top margin although all my ImageMargins settings are set to 0.

What would be the easiest way to make this happen?

EDIT: It doesn't have to look like a button. I just need it to look exactly like the image it is assigned with. Preferably it should be able to be a tab stop and have various states (enabled, disabled, hover, ...) so I could assign appropriate image to each state.

解决方案

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.

这篇关于最简单的方法来制作一个只有一个图像的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆