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. vim中ctags应用

    ctags(Generate tag files for source code)是vim下方便代码阅读的工具.尽管ctags也可以支持其它编辑器,但是它正式支持的只有VIM.并且VIM中已经默认安装 ...

  2. 利用docker搭建测试环境--安装

    软件测试过程中,总会碰到测试环境不够用的尴尬情况.即时有了机器还要经历装系统,配置环境,调试等一系列繁琐的问题.虽然市面上也有一些批处理话的工具(如salt,fabric等),但是还是需要实体机器作为 ...

  3. C++ 4种强制类型转换

    C++的四种强制类型转换为:static_cast.const_cast.reinterpret_cast和dynamic_cast 类型转换的一般形式:cast-name(expression); ...

  4. hdu6069 Counting Divisors 晒区间素数

    /** 题目:hdu6069 Counting Divisors 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6069 题意:求[l,r]内所有数的k次方 ...

  5. shell 颜色

    PS1='\[\e[33;1m\][\u@\h \W]\\$ \[\e[m\]' echo -e "\033[30m 黑色字oldboy trainning \033[0m" ec ...

  6. 【Raspberry pi】GPIO注意事项

    1.GPIO编码的方法 第三列是树莓派板子上的自然编号(左边引脚为1-15,右边引脚为2-26),RPi.GPIO.setmode(GPIO.BOARD)采用这列编号 树莓派主芯片提供商Broadco ...

  7. uva 10494 - If We Were a Child Again 大数除法和取余

    uva 10494 - If We Were a Child Again If We Were a Child Again Input: standard inputOutput: standard ...

  8. js转译html标签

    api返回的接口数据是这样的"<div>测试数据</div>" js拼接html时会过滤掉<div></div> 所以需要对< ...

  9. poj 3308(最小点权覆盖、最小割)

    题目链接:http://poj.org/problem?id=3308 思路:裸的最小点权覆盖,建立超级源点和超级汇点,将源点与行相连,容量为这行消灭敌人的代价,将列与汇点相连,容量为这列消灭敌人的代 ...

  10. Laravel5.1 登录和注册

    关于登录和注册 Laravel自带了一套组件实现了这一功能,我们只需要实现简单的视图即可. AuthController是专门管理用户注册和登录的. PassWordController是重置密码用的 ...