Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 当我们将readonly设置为true时,如何自动更改TcustomEdit的字体颜色_Delphi - Fatal编程技术网

Delphi 当我们将readonly设置为true时,如何自动更改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;

我想更新所有编辑设置为只读时的字体颜色。为此,我更新了TCustomEdit,如下所示:

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()
方法。