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. 【批量加入】-拼接sql字符串

    如今做的一个项目须要用到批量加入,可是封装的底层没有这种方法,所以自食其力,自己来写.我们用的是拼接sql字符串的方法来实现功能. 详细实现流程:首先将须要的数据存储到实体的list中,然后将这个li ...

  2. 在使用add()方法添加组件到容器时,必须指定将其放置在哪个区域中

    BorderLayout是Window.Frame和Dialog的默认布局管理器,其将容器分成North.South.East.West和Center 5个区域,每个区域只能放置一个组件. 在使用ad ...

  3. Android基础新手教程——3.1 基于监听的事件处理机制

    Android基础新手教程--3.1.1 基于监听的事件处理机制 标签(空格分隔): Android基础新手教程 本节引言: 第二章我们学习的是Android的UI控件,我们能够利用这些控件构成一个精 ...

  4. 主干(trunk)、分支(branch )、标记(tag) 用法示例 + 图解

    以svn为例,git的master相当于trunk,dev分支相当于branches --------------------------------------------------------- ...

  5. CGContextRef用法

    本文转载至 http://blog.csdn.net/perfect_promise/article/details/7660220 quartz 是主要的描画接口,支持基于路径的描画. 抗锯齿渲染. ...

  6. 基于注解的Spring AOP拦截含有泛型的DAO

    出错场景 1.抽象类BaseDao public abstract class BaseDao<T> { public BaseDao() { entityClass = (Class&l ...

  7. PhoneGap 获得设备属性Demo

    <!DOCTYPE html> <html> <head> <title>设备属性Demo</title> <script type= ...

  8. bnuoj 34990(后缀数组 或 hash+二分)

    后缀数组倍增算法超时,听说用3DC可以勉强过,不愿写了,直接用hash+二分求出log(n)的时间查询两个字符串之间的任意两个位置的最长前缀. 我自己在想hash的时候一直在考虑hash成数值时MOD ...

  9. poj2046

    Gap Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1829   Accepted: 829 Description Le ...

  10. ES6入门概览二--数组

    一 数组 1. Array.from() 将两类对象转为真的数组 : 类似数组的对象(伪数组,如arguments.document.getElementsByTagNames等)和可遍历对象(包括E ...