Vil*_*nen 3 delphi vcl delphi-xe
我正在使用Delphi XE,我想制作一个按钮,只显示提供的PNG图像,透明背景,没有任何额外的边距.
我尝试用TButton做这个,但是我得到了一个带有bsPushButton风格的丑陋灰色背景.如果我使用bsCommandLink样式,则有一个10像素的上边距,尽管我的所有ImageMargins设置都设置为0.
实现这一目标的最简单方法是什么?
编辑:它不必看起来像一个按钮.我只需要它看起来与它所分配的图像完全一样.优选地,它应该能够是制表位并具有各种状态(启用,禁用,悬停......),因此我可以为每个状态分配适当的图像.
你想要的是一个透明的控件,它继承TWinControl自你希望它能够检索焦点,这从来都不是一件容易的事.然而,由于Embarcadero最近的版本提供了一个提供此功能的控件.这TCustomTransparentControl是一个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;
Run Code Online (Sandbox Code Playgroud)
通过这种方法,您应该能够获得您正在寻找的透明度和半透明度.但是,您仍然需要处理按钮被禁用,按下等的情况.