将readonly设置为true时如何自动更改TcustomEdit的字体颜色

时间:2019-02-13 15:21:37

标签: delphi

当所有编辑设置为只读时,我想更新所有字体的字体颜色。为此,我像这样更新TCustomEdit:

function submitTable(){
    var rows_ids = [];
    $('#content-table .new-value').each(function(){
        var old_value = parseInt($(this).parent().find('.old-value').text());
        var new_value = parseInt($(this).find('input.desired-input').val());
        if(old_value !== new_value){
            rows_ids.push($(this).parent().attr('id'));
            // use this as you want, it containes the id for the changed rows.
            // you have the changes value in new_value, so you also can use it.
        }
    });
    console.log(rows_ids);
}

但是我不明白为什么这行不通:( 理想情况下,我希望我的只读 编辑设计与已禁用 Tedit

相同的设计

1 个答案:

答案 0 :(得分:4)

好吧,对于初学者来说,您的代码是不完整的,应该看起来像这样:

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;

这么说,为了在运行时正确运行,您需要确保所有Edit对象实际上都在使用此类,而不是标准的TEdit类。如果要在设计时执行此操作,则必须将该类放入包中,然后将其安装到IDE中。

一种更简单的方法是将其变成中介层:

type
  TEdit = class(Vcl.StdCtls.TEdit)
    ... same code as above ...
  end;

将此类声明放置在您的TForm类声明的上方。或在uses单元后{em>之后中包含在单独的单元中。无论哪种方式,DFM流系统都会对Vcl.StdCtrls中的所有TEdit对象使用TEdit last 声明的定义。无需安装IDE。