Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Custom colors in Delphi 7

1,027 views
Skip to first unread message

Jim Fleming

unread,
Jan 16, 2006, 6:53:09 AM1/16/06
to
Hi,
Could someone post here the complete code for establishing an additional custom colour constant (eg
clMyColour) that should then be available for selection in all Color properties in the Object
Inspector

Thanks,

Jim Fleming


Remy Lebeau (TeamB)

unread,
Jan 17, 2006, 5:18:16 PM1/17/06
to

"Jim Fleming" <jfle...@ingeteam.es> wrote in message
news:43cb8952$1...@newsgroups.borland.com...

> Could someone post here the complete code for establishing an
> additional custom colour constant (eg clMyColour) that should
> then be available for selection in all Color properties in the
> Object Inspector

For what you ask, you will have to your own property editor. You cannot add
new values to the default Color property editor.

Derive a new class from TColorProperty and then register it via
RegisterPropertyEditor(). For example:

--- MyColorEditor.pas ---

unit MyColorEditor;

interface

// you did not say which version of Delphi you are using. Different
versions
// use different unit names for the design-time support classes ...
uses DsgnIntf;

type
TMyColorEditor = class(TPropertyEditor)
public
function GetValue: String; override;
procedure GetValues(Proc: TGetStrProc); override;
procedure SetValue(const Value: String); override;
end;

procedure Register;

implementation

uses Graphics;

const
clMyColour: TColor = $12345; // use your custom color's actual value

function TMyColorEditor.GetValue: String;
begin
if TColor(GetOrdValue) = clMyColour then
Result := 'MyColour'
else
Result := inherited GetValue;
end;

procedure TMyColorEditor.GetValues(Proc: TGetStrProc);
begin
Graphics.GetColorValues(Proc);
Proc('MyColour');
end;

procedure TMyColorEditor.SetValue(const Value: String);
begin
if AnsiSameText(Value, 'MyColour') then
SetOrdValue(clMyColour)
else
inherited SetValue(Value);
end;

procedure Register;
begin
RegisterPropertyEditor(Typeinfo(TColor), nil, '', TMyColorProperty);
end;

end.

If you want your custom color to be presented visually in the drop-down
list, then you will also have to override the ListDrawValue() and
PropDrawValue() methods as well. In D6+, you will also have to derive your
class from the ICustomPropertyDrawing and ICustomPropertyListDrawing
interfaces.


Gambit


Jim Fleming

unread,
Jan 18, 2006, 8:51:22 AM1/18/06
to
Remy,

Thanks very much for your reply. It is much appreciated, as I am not an expert in component writing
/ property modification, etc, etc.

// you did not say which version of Delphi you are using. Different versions
// use different unit names for the design-time support classes ...
uses DsgnIntf;

We use Delphi 7. Is DsgnIntf then the correct interface unit ??


The remaining code you give is clear, having followed other posts on this subject.

However, just one last question, which you probably saw coming anyway (<g>):

> If you want your custom color to be presented visually in the drop-down
> list, then you will also have to override the ListDrawValue() and
> PropDrawValue() methods as well.

Could you post an example overriding ListDrawValue and PropDrawValue ???


Thanks for your time,

Jim Fleming


Remy Lebeau (TeamB)

unread,
Jan 18, 2006, 3:41:40 PM1/18/06
to

"Jim Fleming" <jfle...@ingeteam.es> wrote in message
news:43ce...@newsgroups.borland.com...

> We use Delphi 7.

Sorry, I did not see that in the original posting.

> Is DsgnIntf then the correct interface unit ??

No, it is not. TPropertyEditor is declared in the and DesignEditors unit,
and the drawing interfaces are declared in the VCLEditors unit.

> Could you post an example overriding ListDrawValue and PropDrawValue ???

Search the newsgroup archives at http://www.deja.com.


Gambit


Jim Fleming

unread,
Jan 19, 2006, 5:29:48 AM1/19/06
to
Remy,

From RxColors unit I have extracted the following code:

procedure TRxColorProperty.ListDrawValue (const Value: string; ACanvas: TCanvas;

const ARect: TRect; ASelected: Boolean);

function ColorToBorderColor (AColor: TColor): TColor;

type

TColorQuad = record

Red, Green, Blue, Alpha: Byte;

end;

begin

if ((TColorQuad(AColor).Red > 192) or (TColorQuad(AColor).Green > 192) or

(TColorQuad(AColor).Blue > 192)) then

Result := clBlack

else

if (ASelected) then

Result := clWhite

else

Result := AColor;

end;

var

vRight: Integer;

vOldPenColor, vOldBrushColor: TColor;

begin

vRight := (ARect.Bottom - ARect.Top) + ARect.Left;

with ACanvas do

try

vOldPenColor := Pen.Color;

vOldBrushColor := Brush.Color;

Pen.Color := Brush.Color;

Rectangle(ARect.Left, ARect.Top, vRight, ARect.Bottom);

Brush.Color := RxStringToColor(Value);

Pen.Color := ColorToBorderColor(ColorToRGB(Brush.Color));

Rectangle(ARect.Left + 1, ARect.Top + 1, vRight - 1, ARect.Bottom - 1);

Brush.Color := vOldBrushColor;

Pen.Color := vOldPenColor;

finally

ACanvas.TextRect(Rect(vRight, ARect.Top, ARect.Right, ARect.Bottom),

vRight + 1, ARect.Top + 1, Value);

end;

end;

This looks to me, a neophyte in these matters, to be correct. Am I right ?

Searching Borland newsgroups, I could not find a PropDrawValue routine for a colour property.

Could you, or someone else, please post such code, if you have it ?? I'd be very grateful.

Thanks,

Jim Fleming


0 new messages