Delphi 图像按钮,无标题,无边框,带制表位

问题描述 投票:0回答:1

我想使用一个图像作为按钮,没有标题,没有背景,没有带制表位的边框。

我已经尝试了带有 flat 属性集的 speedbutton,除了没有制表位外,它是完美的。

我试过 bitbtn,它也接受图像并有一个制表位,但无法删除按钮边框。我在 StyleElements 中未选中 seBorder,它什么都不做,也没有平面选项,所以无法删除边框。

image delphi button borderless tabstop
1个回答
0
投票

每个人都有自己的风格和喜好,但在UI设计上,坚持平台标准很重要。并且请注意,您的 UI 将来可能会变得无法使用而令人头疼。诸如 Aero 界面的引入和高密度显示器的使用之类的事情很容易弄乱您的个性化 UI。

话虽如此,

TSpeedButton
TBitBtn
是实现您想要的内容的两个选项。根据设计,Delphi 有两种类型的
TControl
。一种是
TGraphicControl
,像
TSpeedButton
,收不到焦点。另一个是
TWinControl
,像
TBitBtn
,可以专注。因此,一种方法是将
TSpeedButton
放置在可聚焦容器上,通过覆盖
CreateParam
使容器透明,并处理其按键/按键事件。这并不容易。或者,您可以子类
TBitBtn
并通过处理
CN_DRAWITEM
消息覆盖其绘图。

下面是一个最小的工作示例,在 Delphi 2009 和 10.4 上测试。因为你只想要图像,所以我跳过了,没有处理主题。我也没有处理按钮按下、启用和悬停。阅读源代码 (

Buttons.pas
) 了解如何处理它们。

在新表单中添加一个

TButton
和一个
TbitBtn
并尝试此代码

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Buttons, StdCtrls;

type
  // This changes all TBitBtn controls in this form, including the one from the plaette
  TBitBtn = class(Buttons.TBitBtn)
  private
    procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
    procedure DrawItem(const DrawItemStruct: TDrawItemStruct);
  end;

  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    Button1: TButton;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TBitBtn }

procedure TBitBtn.CNDrawItem(var Message: TWMDrawItem);
begin
  DrawItem(Message.DrawItemStruct^);
end;

procedure TBitBtn.DrawItem(const DrawItemStruct: TDrawItemStruct);
var
  R: TRect;
  FCanvas: TCanvas;
begin
  FCanvas := TCanvas.Create;
  try
    FCanvas.Handle := DrawItemStruct.hDC;
    R := ClientRect;
    if Focused then
      FCanvas.DrawFocusRect(R);
    Glyph.Transparent := True;
    FCanvas.Draw((R.Left + R.Right - GlyPh.Width) div 2,
                 (R.Top + R.Bottom - GlyPh.Height) div 2,
                 GlyPh);
  finally
    FCanvas.Free;
  end;
end;

end.

编辑: 2023 年 3 月 26 日:TBitBtn 的字形是用透明背景绘制的,尽管其透明属性为 false。上面添加了一条线使绘图透明,就像 TBitBtn 一样。

© www.soinside.com 2019 - 2024. All rights reserved.