tk_
tk_

Reputation: 570

Delphi access private function in VCL unit

How do I access private function in a VCL unit?

I need to call the function ColorToPrettyName from ExtCtrls.pas.

Now I copied this to my source but IMO nicer would be to use that function instead.

Some more details: The function is used in TColorBox, but I only need the pretty name. I tried to instantiate TColorBox and get the pretty name from it, but this is only possible when I have some TWinControl to assign to its Parent. But that TWinControl I don't have in the place where I want to use the pretty name and I don't want to do any hacks.

Upvotes: 0

Views: 592

Answers (3)

David Heffernan
David Heffernan

Reputation: 613262

You cannot readily call this function from outside the unit since it is not exposed.

You could create an instance of the control that calls the function, and persuade it to do the dirty work. That's perfectly feasible as another another answer demonstrates.

You could use a disassembler to find the address of the function and call it using a procedural variable. The madExcept source code is a great source of examples of that technique.

On balance, copying the source to your code is, in my view, the best option. All of your available options have drawbacks and this seems the simplest.

Upvotes: 2

Blurry Sterk
Blurry Sterk

Reputation: 1607

Like David said; copying the source to your own unit will be the best.

I believe ColorToPrettyName will not be changed that often but if you are worried that it will and your copied code will be different upon an upgrade of Delphi then you can add a compiler directive to your code that checks the version and warns you about it. Then you can update your code and wait till the next time you upgrade your Delphi. Easy.

Upvotes: 1

quasoft
quasoft

Reputation: 5438

You will find a sample how to access the ColorToPrettyName here: https://github.com/project-jedi/jvcl/blob/master/donations/Colors/JvFullColorSpaces.pas

// (outchy) Hook of TColorBox to have access to color's pretty names.
// Shame on me but that's the only way to access ColorToPrettyName array in the
// ExtCtrls unit. Thanks Borland.

{$IFDEF COMPILER6_UP}
type
  TJvHookColorBox = class (TCustomColorBox)
  protected
    function GetItemsClass: TCustomComboBoxStringsClass; override;
    procedure DestroyWindowHandle; override;
  public
    constructor Create; reintroduce;
    procedure CreateWnd; override;
    procedure DestroyWnd; override;
  end;
  ........

This is what you asked, though this is definitely not good practice.

A better solution would be to use the same function from JEDI (JvJCLUtils.pas), although this adds a dependency.

You will find JEDI here: http://jvcl.delphi-jedi.org/

It contains many more useful utilities and components.

Upvotes: 2

Related Questions