【问题标题】:Delphi: How to know when a TEdit changes size?Delphi:如何知道 TEdit 何时更改大小?
【发布时间】:2009-09-14 19:21:38
【问题描述】:

当编辑框改变大小时,我需要更新它周围的项目。

TEdit 没有 OnResize 事件。

编辑框可以在不同时间调整大小,例如:

  • 在代码中更改宽度/高度
  • 为 DPI 缩放表单缩放
  • 字体已更改

我敢肯定还有其他我不知道的人。

我需要一个事件来知道编辑框何时改变了它的大小。是否有 Windows 消息我可以将编辑框子类化并抓取?

【问题讨论】:

  • 您想在更改代码时触发 OnResize?怎么样?
  • @Havenard:Ian 的意思是如果代码的大小在运行时发生变化,而不是如果他在设计时实际更改了代码。

标签: windows delphi events resize editbox


【解决方案1】:

OnResize 被声明为 TControl 的受保护属性。您可以使用所谓的“cracker”类公开它。不过,这有点骇人听闻。

type
  TControlCracker = class(TControl);

...

procedure TForm1.FormCreate(Sender: TObject);
begin
  TControlCracker(Edit1).OnResize := MyEditResize;
end;

procedure TForm1.MyEditResize(Sender: TObject);
begin
  Memo1.Lines.Add(IntToStr(Edit1.Width));
end;

【讨论】:

  • 最优雅,并展示了大多数 OO 库中的抽象漏洞。
【解决方案2】:

你有没有尝试过这样的事情:

unit _MM_Copy_Buffer_;

interface

type
  TMyEdit = class(TCustomEdit)
  protected
    procedure Resize; override;
  end;

implementation

procedure TMyEdit.Resize;
begin
  inherited;
  if not (csLoading in ComponentState) then
  begin
    // react on new size
  end;
end;

end.

或者这个:

unit _MM_Copy_Buffer_;

interface

type
  TCustomComboEdit = class(TCustomMaskEdit)
  private
    procedure WMSize(var Message: TWMSize); message WM_SIZE;
  end;

implementation

procedure TCustomComboEdit.WMSize(var Message: TWMSize);
begin
  inherited;
  if not (csLoading in ComponentState) then
  begin
    // react on new size
  end;
  UpdateBtnBounds;
end;

end.

【讨论】:

    【解决方案3】:

    处理wm_Size 消息。通过为控件的WindowProc 属性分配新值来子类化控件;请务必存储旧值,以便您可以在那里委派其他消息。

    另见:wm_WindowPosChanged

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      • 2015-09-07
      • 2020-04-20
      相关资源
      最近更新 更多