change the color of a disabled TEdit?

Author: P. Below

Category: VCL

{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.

change the color of a disabled TEdit?的更多相关文章

  1. javafx ComboBox Event and change cell color

    public class EffectTest extends Application { public static void main(String[] args) { launch(args); ...

  2. highcharts dynamic change line color

    mouseOut: function(){ this.series.graph.attr({"stroke","#ccc"}) }

  3. ImportError: No module named _curses;Color support is disabled, python-curses is not installed.解决办法

    linux系统默认安装了python2.6, 但是发现python2.7 import curses时 提示 找不到_curses 错误.  用pip(python2.7 )安装了curses-204 ...

  4. Change the color of a link in an NSMutableAttributedString

    Swift Updated for Swift 3 Use with a textView.linkTextAttributes = [NSForegroundColorAttributeName: ...

  5. mplayer-for-windows change color scheme in win 7

    Q: When I play movie on Windows7, always comes this message: The color scheme has been changed The f ...

  6. How to change the text color in the terminal

    You can open the file ~/.bashrc and then choose the force_color_prompt=yes otherwise, you can change ...

  7. Chrome DevTools: Color tricks in the Elements Panel

    shift + click to change the color format Tip one The Colour Platters are customeised for you .they s ...

  8. Entity Framework Code First - Change Tracking

    In this post we will be discussing about change tracking feature of Entity Framework Code First. Cha ...

  9. 快速 图片颜色转换迁移 Color Transfer Opencv + Python

      Super fast color transfer between images About a month ago, I spent a morning down at the beach, w ...

随机推荐

  1. 中小企业项目的痛VS感人IT团队

    早上,接到客户电话,dynamics CRM不能用了,此客户从开始安装程序开始二次开发期间,因电源问题导致服务器多次意外断电,至今也不加UPS电源.前几次,都不是很严重,服务器没有大量文件损坏,操作系 ...

  2. openresty安装

    cd /usr/local/mkdir datacd datawget http://openresty.org/download/openresty-1.9.15.1.tar.gz cd /usr/ ...

  3. The Ball Puzzle

    自己做的小游戏 google play store: https://play.google.com/store/apps/details?id=com.ffipp.theBall app store ...

  4. C. Beautiful Numbers

    C. Beautiful Numbers Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Oth ...

  5. [转]SpecFlow使用入门

    SpecFlow是一个BDD工具,在这里对BDD不多赘述,你可以阅读一下微软2010年十二月的一篇文章,此外如果你想要更多了解SpecFlow,可以参考我的另一篇翻译(当然,也可以直接进入官网) 一. ...

  6. Linux文件的打包与压缩

    打包命令: tar tar 的选项与参数非常的多!我们只讲几个常用的选项,更多选项您可以自行 man tar 查询罗! [root@www ~]# tar [-j|-z] [cv] [-f 创建的档名 ...

  7. 说说常见的几个js疑难点

    JavaScript match() 方法 定义和使用方法 match() 方法可在字符串内检索指定的值,或找到一个或多个正則表達式的匹配. 该方法类似 indexOf() 和 lastIndexOf ...

  8. delphi 快捷键的使用

    CTRL+SPACE 代码补全,很好用的(先改了输入法热键)CTRL+SHIFT+C 编写申明或者补上函数CTRL+SHIFT+↑(↓) 在过程.函数.事件内部, 可跳跃到相应的过程.函数.事件的定义 ...

  9. Windows(7)上那些好用的软件及优化技巧(原创)

    *本文为原创内容,转载请注明作者和出处:www.cnblogs.com/wang1024 软件篇 注:以下软件在百度直接搜索软件名均可找到官网,直接官网下载即可 大众的软件哪个好: 杀毒软件专题 基于 ...

  10. Flutter入门之有状态组件

    StatefulComponent使用方法入门 在上一篇Flutter入门之无状态组件中我们讲到了无状态组件,所谓的无状态组件指的就是其内部的状态是来自其父组件并使用final类型的变量来存储,当组件 ...