仅用图像制作按钮的最简单方法

发布于 2024-10-21 02:13:13 字数 327 浏览 1 评论 0原文

我正在使用 Delphi XE,我想制作一个按钮,仅显示提供的具有透明背景的 PNG 图像,并且没有任何类型的附加边距。

我尝试使用 TButton 执行此操作,但我得到了 bsPushButton 样式的难看的灰色背景。如果我使用 bsCommandLink 样式,则尽管我所有的 ImageMargins 设置都设置为 0,但上边距为 10 像素。

实现此目的的最简单方法是什么?

编辑:它不必看起来像一个按钮。我只需要它看起来与分配的图像一模一样。最好它应该能够成为制表位并具有各种状态(启用、禁用、悬停...),以便我可以为每个状态分配适当的图像。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长途伴 2024-10-28 02:13:13

您想要的是一个继承自 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;

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

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.

卖梦商人 2024-10-28 02:13:13

您可以使用 TImage 并分配 OnClick 事件来模拟按钮。这取决于您是否需要获得焦点。

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

独﹏钓一江月 2024-10-28 02:13:13

您可以使用 TPanel 并分配 OnClick 事件来模拟按钮。将面板的边框设置为“平坦”,使其看起来像没有面板。

它与stukelly提出的解决方案类似,但更容易实现启用和悬停功能。例如,悬停时,您可以使面板看起来 3D。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文