Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2005.09.11;
Скачать: CL | DM;

Вниз

Как поменять Font.Color TEdit a (при Enabled = False)?   Найти похожие ветки 

 
vigo   (2005-08-19 16:05) [0]

Подскажите, можно ли поменять цвет текста TEdit если свойство Enabled = False?


 
Kazakh   (2005-08-19 16:14) [1]

Изменить цвет неактивного TEdit
-------------------------------------------------------------

{Question:
How can I change the color of a disabled (Edit1.Enabled := false;) control?
I do not want the normal grey color.

Answer:
Two options:

1) place the control on a panel and disable the panel instead of the control.
This way the color stays to whatever you set it.

2) make a descendent and take over the painting when it is disabled.

Here is an example:}

unit PBExEdit;

interface

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

type
  TPBExEdit = class(TEdit)
  private
    { Private declarations }
    FDisabledColor: TColor;
    FDisabledTextColor: TColor;
    procedure WMPaint(var msg: TWMPaint); message WM_PAINT;
    procedure WMEraseBkGnd(var msg: TWMEraseBkGnd); message WM_ERASEBKGND;
    procedure SetDisabledColor(const Value: TColor); virtual;
    procedure SetDisabledTextColor(const Value: TColor); virtual;
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(aOwner: TComponent); override;
  published
    { Published declarations }
    property DisabledTextColor: TColor read FDisabledTextColor
      write SetDisabledTextColor default clGrayText;
    property DisabledColor: TColor read FDisabledColor
      write SetDisabledColor default clWindow;
  end;

procedure Register;

 implementation

procedure Register;
begin
  RegisterComponents("PBGoodies", [TPBExEdit]);
end;

constructor TPBExEdit.Create(aOwner: TComponent);
begin
  inherited;
  FDisabledColor := clWindow;
  FDisabledTextColor := clGrayText;
end;

procedure TPBExEdit.SetDisabledColor(const Value: TColor);
begin
  if FDisabledColor <> Value then
  begin
    FDisabledColor := Value;
    if not Enabled then
      Invalidate;
  end;
end;

procedure TPBExEdit.SetDisabledTextColor(const Value: TColor);
begin
  if FDisabledTextColor <> Value then
  begin
    FDisabledTextColor := Value;
    if not Enabled then
      Invalidate;
  end;
end;

procedure TPBExEdit.WMEraseBkGnd(var msg: TWMEraseBkGnd);
var
  Canvas: TCanvas;
begin
  if Enabled then
    inherited
  else
  begin
    Canvas:= TCanvas.Create;
    try
      Canvas.Handle := msg.DC;
      SaveDC(msg.DC);
      try
        canvas.Brush.Color := FDisabledColor;
        canvas.Brush.Style := bsSolid;
        canvas.Fillrect(clientrect);
        msg.Result := 1;
      finally
        RestoreDC(msg.DC, - 1);
      end;
    finally
      canvas.free
    end;
  end;
end;

procedure TPBExEdit.WMPaint(var msg: TWMPaint);
var
  Canvas: TCanvas;
  ps: TPaintStruct;
  CallEndPaint: Boolean;
begin
  if Enabled then
    inherited
  else
  begin
    CallEndPaint := False;
    Canvas:= TCanvas.Create;
    try
      if msg.DC <> 0 then
      begin
        Canvas.Handle := msg.DC;
        ps.fErase := true;
      end
      else
      begin
        BeginPaint(Handle, ps);
        CallEndPaint:= True;
        Canvas.handle := ps.hdc;
      end;
      if ps.fErase then
        Perform(WM_ERASEBKGND, Canvas.Handle, 0);
      SaveDC(canvas.handle);
      try
        Canvas.Brush.Style := bsClear;
        Canvas.Font := Font;
        Canvas.Font.Color := FDisabledTextColor;
        Canvas.TextOut(1, 1, Text);
      finally
        RestoreDC(Canvas.Handle, - 1);
      end;
    finally
      if CallEndPaint then
        EndPaint(handle, ps);
      Canvas.Free
    end;
  end;
end;

end.


 
Leonid Troyanovsky ©   (2005-08-19 16:20) [2]


> Kazakh   (19.08.05 16:14) [1]
> Изменить цвет неактивного TEdit


Это ж Peter Below (Teamb).
Дельферы должны знать своих героев.

--
Regards, LVT.


 
Турист   (2005-08-19 21:08) [3]

>vigo   (19.08.05 16:05)  

procedure TForm1.Button1Click(Sender: TObject);
begin
 Edit1.Enabled := False;
 Edit1.Color := clRed
end;



 
Турист   (2005-08-19 21:09) [4]

>Турист   (19.08.05 21:08) [3]
Неверно, вопрос был про Font.Color



Страницы: 1 вся ветка

Текущий архив: 2005.09.11;
Скачать: CL | DM;

Наверх




Память: 0.48 MB
Время: 0.026 c
1-1123735709
afanasic
2005-08-11 08:48
2005.09.11
Освобождение памяти при Destroy


1-1124191188
Bombila
2005-08-16 15:19
2005.09.11
Как рисовать иконки с alpha-blend(ом)?


14-1124266807
Андрей Жук
2005-08-17 12:20
2005.09.11
Проблемы с Dual Channel DDR


4-1122289098
Maverick
2005-07-25 14:58
2005.09.11
Перехват действия программы


6-1116865895
ATarget
2005-05-23 20:31
2005.09.11
PIPE каналы