Delphi 当我们将readonly设置为true时,如何自动更改TcustomEdit的字体颜色
我想更新所有编辑设置为只读时的字体颜色。为此,我更新了TCustomEdit,如下所示:Delphi 当我们将readonly设置为true时,如何自动更改TcustomEdit的字体颜色,delphi,Delphi,我想更新所有编辑设置为只读时的字体颜色。为此,我更新了TCustomEdit,如下所示: TMyCustomEdit = class(TCustomEdit) private procedure EMSetReadOnly (var Message: TMessage); message end; procedure TMyCustomEdit.EMSetReadOnly (var Message: TMessage); begin inherited;
TMyCustomEdit = class(TCustomEdit)
private
procedure EMSetReadOnly (var Message: TMessage); message
end;
procedure TMyCustomEdit.EMSetReadOnly (var Message: TMessage);
begin
inherited;
font.Color := clred;
end;
但我不明白这为什么不起作用:(
理想情况下,我希望我的只读编辑具有与禁用的Tedit相同的设计。首先,您的代码是不完整的,它应该更像这样:
type
TMyCustomEdit = class(TCustomEdit)
private
procedure EMSetReadOnly(var Message: TMessage); message EM_SETREADONLY;
procedure UpdateFont;
protected
procedure CreateWnd; override;
end;
procedure TMyCustomEdit.EMSetReadOnly(var Message: TMessage);
begin
inherited;
UpdateFont;
end;
procedure TMyCustomEdit.CreateWnd;
begin
inherited;
UpdateFont;
end;
procedure TMyCustomEdit.UpdateFont;
begin
if (GetWindowLongPtr(Handle, GWL_STYLE) and ES_READONLY) <> 0 then
Font.Color := clRed
else
Font.Color := clWindowText;
end;
type
TEdit = class(Vcl.StdCtls.TEdit)
... same code as above ...
end;
将此类声明放在TForm类声明之上。或者在
Vcl.StdCtrls
单元之后的uses
子句中包含的单独单元中。无论哪种方式,DFM流媒体系统都将对TForm
中的所有TEdit
对象使用上次声明的TEdit
定义。没有IDE i需要安装。首先,您的代码不完整,应该看起来像这样:
type
TMyCustomEdit = class(TCustomEdit)
private
procedure EMSetReadOnly(var Message: TMessage); message EM_SETREADONLY;
procedure UpdateFont;
protected
procedure CreateWnd; override;
end;
procedure TMyCustomEdit.EMSetReadOnly(var Message: TMessage);
begin
inherited;
UpdateFont;
end;
procedure TMyCustomEdit.CreateWnd;
begin
inherited;
UpdateFont;
end;
procedure TMyCustomEdit.UpdateFont;
begin
if (GetWindowLongPtr(Handle, GWL_STYLE) and ES_READONLY) <> 0 then
Font.Color := clRed
else
Font.Color := clWindowText;
end;
type
TEdit = class(Vcl.StdCtls.TEdit)
... same code as above ...
end;
将此类声明放在TForm类声明之上。或者在
Vcl.StdCtrls
单元之后的uses
子句中包含的单独单元中。无论哪种方式,DFM流媒体系统都将对TForm
中的所有TEdit
对象使用上次声明的TEdit
定义。没有IDE i需要安装。可能需要刷新编辑。如果您希望只读文本编辑看起来像禁用的文本编辑,为什么不禁用它?可能需要刷新编辑。如果您希望只读文本编辑看起来像禁用的文本编辑,为什么不禁用它?@SertacAkyuz我本来想禁用,但我决定确保在更改字体之前,ge实际上应用于HWND
。然后我想,如果ReadOnly
属性设置器不使用EM\u SETREADONLY
,如果它重新创建HWND
(VCL非常喜欢这样做)还有第三种可能性,如果属性使用SetWindowLong/Ptr(GWL_样式)
要直接设置ES_READONLY
,没有通知,所以我还没有说明。我目前无法访问Delphi来查看READONLY
是如何实际实现的。如果分配了句柄,它会发送消息……否则只设置FReadOnly。getter读取FReadOnly。@SertacAkyuz那么类就更有理由同时处理消息和CreateWnd()
method.@SertacAkyuz我本来打算这么做的,但我决定在更改字体之前确保将更改实际应用到HWND
。然后我想,如果ReadOnly
属性设置器不使用EM\u SETREADONLY
,而是重新创建HWND
(VCL非常喜欢这样做)还有第三种可能性,如果属性使用SetWindowLong/Ptr(GWL_样式)
要直接设置ES_READONLY
,没有通知,所以我还没有说明。我目前无法访问Delphi来查看READONLY
是如何实际实现的。如果分配了句柄,它会发送消息……否则只设置FReadOnly。getter读取FReadOnly。@SertacAkyuz那么类就更有理由同时处理消息和CreateWnd()
方法。