【问题标题】:Getting the MaxLen parameter to use with MinimizeName获取 MaxLen 参数以与 MinimizeName 一起使用
【发布时间】:2013-04-30 02:36:03
【问题描述】:

我正在尝试使用 Vcl.FileCtrl 单元中的 MinimizeName 函数在 TLabel 上放置一个很长的文件名,但我不知道如何获取该函数使用的 MaxLen 参数 如果我硬编码一个值,我可以看到一个有效的结果。但由于表单可以调整大小,我希望它是动态的 = 在调整大小事件时发生变化。

我尝试过的一些事情是 lblLicenseFile.Width // 字符串太长 lblLicenseFile.Width - 10 //字符串太长 Trunc(lblLicenseFile.Width / lblLicenseFile.Font.Size) // 字符串很短

必须有某种方法来计算这个像素数

MinimizeName(const Filename: TFileName; Canvas: TCanvas; MaxLen: Integer): TFileName; MaxLen 是可用于在画布上绘制文件名的长度(以像素为单位)。

【问题讨论】:

  • 我不明白这个问题。为什么不直接从表单的OnResize 事件中调用MinimizeName
  • 如果我没有为 MaxLen 添加正确的值,则文本看起来不正常 - 要么长标签,要么短标签。
  • 标签 autosize 设置为 false,我当前的代码如下所示 - 不起作用。标签文件名的字符串太长:= MinimizeName(Settings.License.LicenseFile, lblLicenseFile.Canvas, lblLicenseFile.Width); lblLicenseFile.Caption := 文件名;

标签: delphi canvas tlabel


【解决方案1】:

要让标签控件自动缩短路径,如果您使用的是最新版本的 Delphi,可以将 AutoSize 属性设置为 False,将 EllipsisPosition 属性设置为 epPathEllipsis

【讨论】:

  • 我没有想到 - 但它成功了,我摆脱了 Vcl.FileCtrl 单元 - 谢谢
【解决方案2】:

为了摆脱表单大小调整的依赖关系,如果您使用例如,调整大小也可能发生。拆分器,您可以覆盖 CanResize 事件以调整您的标题。

例如:

unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TLabel = Class(StdCtrls.TLabel)
  private
    FFullCaption: String;
    procedure SetFullname(const Value: String);
  published
    function CanResize(var NewWidth, NewHeight: Integer): Boolean; override;
    property FullCaption: String read FFullCaption Write SetFullname;
  End;

  TForm3 = class(TForm)
    FileNameLabel: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form3: TForm3;

implementation

uses FileCtrl;
{$R *.dfm}

procedure TForm3.Button1Click(Sender: TObject);
begin
  FileNameLabel.FullCaption := 'C:\ADirectory\ASubDirectory\ASubSubDirectory\AFileN.ame'
end;

{ TLabel }

function TLabel.CanResize(var NewWidth, NewHeight: Integer): Boolean;
begin
  inherited;
  if Assigned(Parent) then
    Caption := MinimizeName(FFullCaption, Canvas, NewWidth)
end;

procedure TLabel.SetFullname(const Value: String);
begin
  FFullCaption := Value;
  Caption := MinimizeName(FFullCaption, Canvas, Width)
end;

end.

【讨论】:

    猜你喜欢
    • 2018-10-11
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 2017-06-28
    相关资源
    最近更新 更多