change the color of a disabled TEdit?
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?的更多相关文章
- javafx ComboBox Event and change cell color
public class EffectTest extends Application { public static void main(String[] args) { launch(args); ...
- highcharts dynamic change line color
mouseOut: function(){ this.series.graph.attr({"stroke","#ccc"}) }
- 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 ...
- Change the color of a link in an NSMutableAttributedString
Swift Updated for Swift 3 Use with a textView.linkTextAttributes = [NSForegroundColorAttributeName: ...
- 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 ...
- 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 ...
- 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 ...
- Entity Framework Code First - Change Tracking
In this post we will be discussing about change tracking feature of Entity Framework Code First. Cha ...
- 快速 图片颜色转换迁移 Color Transfer Opencv + Python
Super fast color transfer between images About a month ago, I spent a morning down at the beach, w ...
随机推荐
- 自动化软件部署的shell脚本
在实际项目中,我们经常用到需要自动化部署软件的情况,这种情况下,我们就不能人为地在命令行下敲击命令了,一切都需要通过shell脚本自动化完成.所谓自动化完成,其实也就是通过写shell程序,模拟人为敲 ...
- C++Primer学习笔记《2》
数组是一种复合类型,由类型名+数组名+维度组成. 数组定义中的类型能够是C++基本内置类型.也能够是类类型的.数组元素的类型能够是除了引用类型以外的其它不论什么类型.没有全部的元素都是引用的数组. 数 ...
- 快速排序的c++实现 和 python 实现
最近在学python,其中有个要求实现快速排序的练习,就顺便复习了c++的快速排序实现. 快速排序的基本思想是,通过一轮的排序将序列分割成独立的两部分,其中一部分序列的关键字(这里主要用值来表示)均比 ...
- windows静态库的使用
最近在学Zeromq(像框架一样的一个socket library)的使用,里面有关于库的使用问题.今天就来复习下静态库的使用: 使用静态库是重用代码的一种绝佳方式.您不必在自己创建的每个程序中重新实 ...
- JS 怎么刷新当前页面
reload 方法,该方法强迫浏览器刷新当前页面. 语法:location.reload([bForceGet]) 参数: bForceGet, 可选参数, 默认为 false,从客户端缓存里取当前页 ...
- Collection 和 Collections的区别?
Collection 和 Collections的区别? 解答:Collection是java.util下的接口,它是各种集合的父接口,继承于它的接口主要有Set 和List:Collections是 ...
- Windows编程中回调函数的使用心得(MFC篇)
回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数.回调函数不是由该函数的实现方直接调用,而是在特定 ...
- 安装tomcat出现failed to install tomcat6 service错误及解决方法(转载)
安装安装版tomcat会出现failed to install tomcat6 service ,check your setting and permissio的概率是非常低的,但是最近楼主就老出现 ...
- mysql分表技术
一般来说,当我们的数据库的数据超过了100w记录的时候就应该考虑分表或者分区了,这次我来详细说说分表的一些方法. 目前我所知道的方法都是MYISAM的,INNODB如何做分表并且保留事务和外键,我还不 ...
- Linux动态库搜索路径的技巧
众所周知,Linux动态库的默认搜索路径是/lib和/usr/lib.动态库被创建后,一般都复制到这两个目录中.当程序执行时需要某动态库,并且该动态库还未加载到内存中,则系统会自动到这两个默认搜索路径 ...