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.

时间: 2024-10-23 08:01:43

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

Change column color for columnChart in extjs

var store = Ext.create('Ext.data.JsonStore', { fields: ['name', 'data'], data: [ { 'name': 'metric one',   'data':10 }, { 'name': 'metric two',   'data': 7 }, { 'name': 'metric three', 'data': 5 }, { 'name': 'metric four',  'data': 2 }, { 'name': 'me

javafx ComboBox Event and change cell color

public class EffectTest extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { stage.setTitle("ComboBoxSample"); Scene scene = new Scene(new Group(), 450, 250); ComboBox emailCom

Change the color of a link in an NSMutableAttributedString

Swift Updated for Swift 3 Use with a textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.green] And in context: let attributedString = NSMutableAttributedString(string: "This is an example by @marcelofabri_") let linkRange = (

Entity Framework Code First - Change Tracking

In this post we will be discussing about change tracking feature of Entity Framework Code First. Change tracking allows Entity framework to keep track of all the changes in entities' data. It might involve adding new entities to entities collection o

更改bootstrap-datetimepicker弹出日期选择框只能在下方的限制

城市监控的项目,网页调用监控摄像头,并需要replay的日期和时间选择calendar.找了半天,找到一个好东东~~~~bootstrap-datetimepicker, UI也很有型的范,就用它吧~~, 一切顺利,就是这个calendar的弹出框只能在下方,而我需要嵌套在摄像头监控屏幕下的工具条上,如果弹出框在下方就太离谱了,最后决定改,哈就成就今天这个calendar啦,自我感觉效果不错,晒出来装一下B~~~~哈 更改的bootstrap-datetimepicker.js /** * @l

iBus input method in Linux

The following packages should be installed: python-ibus ibus ibus-clutter ibus-gtk ibus-m17n: input many non-latin characters ibus-pinyin ibus-qt4 ibus-table ibus-table-wubi python-xlib: will be used by Emacs ibus.el ibus-m17n: input many non-latin c

webParts与Web部件

web部件是ASP.NET WebForm里面的服务器控件,它涵盖的内容比较多,鉴于这种状况的话鄙人不打算深究下去了,只是局限于了解web.config配置里面的配置内容则可. 那么也得稍微说说啥是Web部件.引用MSDN的话:ASP.NET Web 部件是一组集成控件,用于创建网站使最终用户可以直接从浏览器修改网页的内容.外观和行为.这些修改可以应用于网站上的所有用户或个别用户.还有引用它上面的插图 看了这个之后我就感觉就类似于QQ个人空间上的各个面板或者OA系统上的面板,可以按照每个用户的个

积累的VC编程小技巧之打印相关

1.修改打印预览的ToolBar 为AFX_IDD_PREVIEW_TOOLBAR这个ID创建一个DialogBar.则系统就会用新创建的DialogBar代替系统默认的那个 2.关于打印 1.要打印哪个视就 ((CMainFrame*)AfxGetMainWnd())->m_wndSplitter.SetActivePane(...) //要打印的那个视对应的Pane 2.有一个单文档工程,文档窗口被切分:左视图由CTreeView 的派生类管理,右视图由CListView 的派生类CMyLi

Bootstrap学习笔记(七)其它内置组件

缩略图(一) 缩略图在网站中最常用的地方就是产品列表页面,一行显示几张图片,有的在图片底下(左侧或右侧)带有标题.描述等信息.Bootstrap框架将这一部独立成一个模块组件.并通过"thumbnail"样式配合bootstrap的网格系统来实现.可以将产品列表页变得更好看. 源码文件: ?  LESS版本:对应文件thumbnails.less ?  Sass版本:对应文件_thumbnails.scss ? 编译后版本:bootstrap.css文件第4402行-第4426行 使用