我正在使用 Delphi XE,我想制作一个按钮,仅显示提供的具有透明背景的 PNG 图像,并且没有任何类型的附加边距。
我尝试使用 TButton 执行此操作,但我得到了 bsPushButton 样式的难看的灰色背景。如果我使用 bsCommandLink 样式,则尽管我所有的 ImageMargins 设置都设置为 0,但上边距为 10 像素。
实现这一目标的最简单方法是什么?
EDIT:它不必看起来像一个按钮。我只需要它看起来与分配的图像一模一样。最好它应该能够成为制表位并具有各种状态(启用、禁用、悬停...),以便我可以为每个状态分配适当的图像。
你想要的是一个继承自的透明控件TWinControl http://docwiki.embarcadero.com/VCL/en/Controls.TWinControl因为你希望它能够重新获得焦点,所以这从来就不是一件容易的事。然而,从最近的版本开始,Embarcadero 提供了一个可以实现这一点的控件。这TCustomTransparentControl http://docwiki.embarcadero.com/VCL/en/Controls.TCustomTransparentControl is a TWinControl
后代,使任务对你来说更容易一些。
所以,我要做的就是创建一个新组件,并继承它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;
通过这种方法,您应该能够获得所需的透明度和半透明度。然而,您仍然需要处理按钮被禁用、按下等情况。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)