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

Class herlper and inheritance

443 views
Skip to first unread message

Jens Borrisholt

unread,
Nov 26, 2008, 8:29:05 AM11/26/08
to
Hi Experts

I would like to override the WndProc in a class helper. I have tried a lot
of things but it all boils down to this little example :

type
TCustomEditHelper = class helper for TCustomEdit
protected
procedure WndProc(var Message: TMessage); override;
end;

Then I get the error :
[DCC Error] Unit34.pas(12): E2137 Method 'WndProc' not found in base class

But WndProc are declared in TWinControl, under protected.

So can anyone help me :

I just want to override the WinProc in a Calss helper

Jens Borrisholt


Rob Kennedy

unread,
Nov 26, 2008, 8:47:22 AM11/26/08
to
Jens Borrisholt wrote:
> I would like to override the WndProc in a class helper. I have tried a lot
> of things but it all boils down to this little example :
>
> type
> TCustomEditHelper = class helper for TCustomEdit
> protected
> procedure WndProc(var Message: TMessage); override;
> end;
>
> Then I get the error :
> [DCC Error] Unit34.pas(12): E2137 Method 'WndProc' not found in base class
>
> But WndProc are declared in TWinControl, under protected.

TWinControl is not the base class of TCustomEditHelper; it is the base
class of TCustomEdit.

> So can anyone help me :
>
> I just want to override the WinProc in a Calss helper

I don't think you can do such a thing. A class helper add new methods to
a class when the class helper is in scope. It does not modify the
original class in any way, and it especially does not modify any
already-compiled code that uses the original class.

What is it you're trying to do?

If you're trying to affect the behavior of all the TEdit controls in
your program, when you may have to resort to patching the function in
memory to jump to your new function first. MadCollection and the JCL
have units that can help with that.

--
Rob

Jens Borrisholt

unread,
Nov 26, 2008, 9:16:07 AM11/26/08
to
On 26 Nov., 14:47, Rob Kennedy <m...@privacy.net> wrote:
> Jens Borrisholt wrote:
> > I would like tooverridethe WndProc in aclass helper. I have tried a lot

> > of things but it all boils down to this little example :
>
> > type
> >   TCustomEditHelper =class helperfor TCustomEdit

> >   protected
> >     procedure WndProc(var Message: TMessage);override;
> >   end;
>
> > Then I get the error :
> >   [DCC Error] Unit34.pas(12): E2137 Method 'WndProc' not found in base class
>
> > But WndProc are declared in TWinControl, under protected.
>
> TWinControl is not the base class of TCustomEditHelper; it is the base
> class of TCustomEdit.
>
> > So can anyone help me :
>
> > I just want tooverridethe WinProc in a Calss helper
>
> I don't think you can do such a thing. Aclass helperadd new methods to
> a class when theclass helperis in scope. It does not modify the

> original class in any way, and it especially does not modify any
> already-compiled code that uses the original class.
>
> What is it you're trying to do?
>
> If you're trying to affect the behavior of all the TEdit controls in
> your program, when you may have to resort to patching the function in
> memory to jump to your new function first. MadCollection and the JCL
> have units that can help with that.
>
> --
> Rob

Hi

I have a TrichEdit control on wich I have implemented a ViewOnly
property, meaning I looks like a normally enabled richedit, but it has
no cursor, view only ..


I'm trying to implement that on all TCustomEdit decentant, Tmemo,
TEdit, TRichEdit etc.

So bacilly I would like to hook whe winproc on all TCustomEdit
decentant, and add a property viewOnly

Jens Borrisholt

Maarten Wiltink

unread,
Nov 26, 2008, 10:07:22 AM11/26/08
to
"Jens Borrisholt" <Je...@Borrisholt.com> wrote in message
news:1b29f365-504d-445d...@k19g2000yqg.googlegroups.com...
[...]

> I have a TrichEdit control on wich I have implemented a ViewOnly
> property, meaning I looks like a normally enabled richedit, but it has
> no cursor, view only ..

Then derive a class (which you've already done) and override WndProc in
that. It's what virtual methods are for. Class helpers aren't.

Groetjes,
Maarten Wiltink

Jens Borrisholt

unread,
Nov 27, 2008, 1:48:04 AM11/27/08
to
On 26 Nov., 16:07, "Maarten Wiltink" <maar...@kittensandcats.net>
wrote:
> "Jens Borrisholt" <J...@Borrisholt.com> wrote in message
>
> news:1b29f365-504d-445d...@k19g2000yqg.googlegroups.com...
> [...]
>
> > IhaveaTrichEditcontrolonwichIhaveimplemented a ViewOnly

> > property, meaning I looks like a normally enabled richedit, but it has
> > no cursor, view only ..
>
> Then derive a class (which you've already done) and override WndProc in
> that. It's what virtual methods are for. Class helpers aren't.
>
> Groetjes,
> Maarten Wiltink

The problem is no to get it to work in a derived class, I would rather
like to extend ALL TCustomEdit desandt with tihs new feature ..

Maarten Wiltink

unread,
Nov 27, 2008, 3:23:33 AM11/27/08
to
"Jens Borrisholt" <Je...@Borrisholt.com> wrote in message
news:e6b3d858-5821-40e9...@x8g2000yqk.googlegroups.com...

> On 26 Nov., 16:07, "Maarten Wiltink" <maar...@kittensandcats.net>
> wrote:
>> "Jens Borrisholt" <J...@Borrisholt.com> wrote in message
>> news:1b29f365-504d-445d...@k19g2000yqg.googlegroups.com...

>>> I have a TrichEditcontrol on wich I have implemented a ViewOnly


>>> property, meaning I looks like a normally enabled richedit, but it
>>> has no cursor, view only ..
>>
>> Then derive a class (which you've already done) and override WndProc
>> in that. It's what virtual methods are for. Class helpers aren't.

> The problem is no to get it to work in a derived class, I would rather


> like to extend ALL TCustomEdit desandt with tihs new feature ..

Aha. I see.

You can't.

In Delphi. At least one langage exists where this _is_ possible to the
best of my knowledge. Once, when the world was young (something like
twenty years ago), I read an article which mentioned such a feature in
discussing a virus scanner for OS/2.

The shell program, written in REXX, apparently had a class hierarchy
for its Explorer, with subdirectories and drives derived from a general
container class. The context menu for nodes in the tree was constructed
through an RTTI mechanism and there was a feature by which the virus
scanner could derive a class from TSubdirectory, add a method
ScanForViruses to it, and _push the derived class up a level_. This made
the new method available in TSubdirectory.

It could then push the new class up another level, and the new context
menu item then became available not only to TSubdirectory but to all its
sibling classes (derived from the same parent class).

This would let you do exactly what you want. I thought it was a very
exciting feature (and so did the writer of the review), but I've never
seen it anywhere else since.

I have thought for years that the more static, compiled nature of Delphi
would make this trick impossible, but thinking of it again I can't
really come up with a good reason why it would be. One glaring difference
is that the REXX system did it at _runtime_, where in Delphi I would
expect it to be done at compile time. This may be a programmer's bias,
however; for extending a shell doing it at runtime would suffice nicely.
Not for adding a programmable property to TCustomEdit though.

Groetjes,
Maarten Wiltink

Hans-Peter Diettrich

unread,
Nov 27, 2008, 5:16:19 AM11/27/08
to
Jens Borrisholt schrieb:

> The problem is no to get it to work in a derived class, I would rather
> like to extend ALL TCustomEdit desandt with tihs new feature ..

The usual way of doing this is creating a new version (branch) of the
library. The new functionality should be patched into the source code of
the original library, so that the patch can be applied to later versions
of that library. For compatibility reasons the new feature can be
enabled by some flag, so that the new library can be used in
"compatibility" mode and in "improved" mode.

DoDi

Rob Kennedy

unread,
Dec 1, 2008, 1:00:29 AM12/1/08
to
Jens Borrisholt wrote:
> On 26 Nov., 16:07, "Maarten Wiltink" <maar...@kittensandcats.net>
> wrote:
>> "Jens Borrisholt" <J...@Borrisholt.com> wrote in message
>> news:1b29f365-504d-445d...@k19g2000yqg.googlegroups.com...
>>> IhaveaTrichEditcontrolonwichIhaveimplemented a ViewOnly
>>> property, meaning I looks like a normally enabled richedit, but it has
>>> no cursor, view only ..
>>
>> Then derive a class (which you've already done) and override WndProc in
>> that. It's what virtual methods are for. Class helpers aren't.
>
> The problem is no to get it to work in a derived class, I would rather
> like to extend ALL TCustomEdit desandt with tihs new feature ..

Declare a class that has this interface:

type
TViewOnlyEditSubclasser = class
private
FEdit: TCustomEdit;
FOldProc: TWndMethod;
procedure WndProc(var Message: TMessage);
public
constructor Create(const AEdit: TCustomEdit);
destructor Destroy; override;
end;

Implement WndProc however you need. You should call FOldProc for any
messages that your method doesn't handle itself. Implement the
constructor and destructor like this:

constructor TViewOnlyEditSubclasser.Create;
begin
inherited Create;
FEdit := AEdit;
FOldProc := FEdit.WindowProc;
FEdit.WindowProc := WndProc;
end;

destructor TViewOnlyEditSubclasser.Destroy;
begin
FEdit.WindowProc := FOldProc;
inherited;
end;

Create an instance of this class for whichever edit controls you want to
make read-only.

--
Rob

Jens Borrisholt

unread,
Dec 3, 2008, 3:39:02 AM12/3/08
to
On 1 Dec., 07:00, Rob Kennedy <m...@privacy.net> wrote:
> JensBorrisholtwrote:

> > On 26 Nov., 16:07, "Maarten Wiltink" <maar...@kittensandcats.net>
> > wrote:
> >> "JensBorrisholt" <J...@Borrisholt.com> wrote in message

Hi

I found a solution. Baclly I attatch a new component to the
TCustomEdti wich I want to subclass. the soultion can be found on
delphi.about.com here : http://delphi.about.com/od/vclusing/a/viewonly-edit.htm,
I just today (3. December 2008) posted an update :D

Please enjoy.

Jens Borrisholt

0 new messages