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. java 检测字符串中文乱码

    1.检测是否为乱码 public static boolean isMessyCode(String strName) { Pattern p = Pattern.compile("\\s* ...

  2. java 获取用户真实ip

    /** * 获取用户真实ip * @param request * @return */ public static String getIpAddr(HttpServletRequest reque ...

  3. quick-cocos2d-x的热更新机制实现

    quick-cocos2d-x的热更新机制实现 0 依赖 这里说的热更新,指的是客户端的更新. 大致的流程是,客户端在启动后访问更新api,根据更新api的反馈,下载更新资源,然后使用新的资源启动客户 ...

  4. android webview处理h5打开本地文件浏览器的功能

    这周遇到一个比较棘手的问题,需要在android上边集成h5页面,并且在h5页面上,需要用户能够上传android本地的照片,一开始我以为webview会自动处理掉的,因此没太留意,当真正集成时,才发 ...

  5. sklearn解决分类问题(KNN,线性判别函数,二次判别函数,KMeans,MLE,人工神经网络)

    代码:*******************加密中**************************************

  6. C#调用ActiveX

    ActiveX控件一般是用来在IE浏览器中配合使用的,有时也需要在例如WPF中调用,这样也是可以的. 一.引用-->右键-->添加引用 点击 COM,找到想要引用的类型库,名字不一定和IE ...

  7. 网络协议之rtp---rtp 传输视频及加密

    http://read.pudn.com/downloads170/sourcecode/windows/788977/es%20ParkertTS/ESToTS.cpp__.htm http://w ...

  8. 2018 CCPC 桂林游记

    TYPE: Onsite Contest NAME: 2018 - CCPC - Guilin PLAT: HUSTOJ TIME: 2018/10/28 09:00-14:00 CST LOCA: ...

  9. 【vijos】1892 树上的最大匹配(树形dp+计数)

    https://vijos.org/p/1892 这个必须得卡评测机+手动开栈才能卡过QAQ 手动开栈我百度的... int size=256<<20; //256MB char *p=( ...

  10. 【BZOJ】3401: [Usaco2009 Mar]Look Up 仰望(单调栈)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3401 还能更裸一些吗.. 维护一个递减的单调栈 #include <cstdio> #i ...