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

How can I subclass a control on a Formview

10 views
Skip to first unread message

Cameron_C

unread,
Nov 23, 2009, 10:54:01 AM11/23/09
to
Hello everyone,
I have a simple coloured button control I derived from a CButton.
When I drag a Button control from the toolbox onto my Formview, I do not get
an opportunity to override the OnCreate message.
I added an OnPreSubclassWindow, but it does not appear to be called either.
I want to save the User selected colours in the registry. Then when the
application starts the next time, the colour can be pulled from the registry,
and used to set the values for the buttons.
I have done a work around by invoking the colour load routine from the
FormView OnInitialUpdate.
But I believe I should be able to do this from the derived class.

Any suggestions?

Thanks,

AliR

unread,
Nov 23, 2009, 11:16:44 AM11/23/09
to
I'm not sure what OnPreSubclassWindow is, but did you try overriding the
virtual method PreSubclassWindow in your button control?

AliR.


"Cameron_C" <Came...@discussions.microsoft.com> wrote in message
news:1C036565-C14D-46B4...@microsoft.com...

David Wilkinson

unread,
Nov 23, 2009, 11:53:34 AM11/23/09
to

Cameron:

OnCreate() is not called for controls placed on a dialog template.

I personally would not have a control access the registry, but if you want to do
it why don't you do it in the constructor? The control does not have to be live
in order to cache the color values in member variables of the control.

--
David Wilkinson
Visual C++ MVP

Cameron_C

unread,
Nov 23, 2009, 12:13:02 PM11/23/09
to
Ah geeze,
Don't I feel foolish!!!
I can't believe I spent a weekend staring at OnPreSubclassWindow and reading
PreSubclassWindow.

Thank you!

"AliR" wrote:

> .
>

Joseph M. Newcomer

unread,
Nov 23, 2009, 12:57:49 PM11/23/09
to
That is correct. It is meaningless to override OnCreate, since OnCreate can never occur.
In fact, you would NEVER want to put an OnCreate handler in a custom child control!

PreSubclassWIndow is called only if you have a control variable bound to the control; I
have never seen this fail. If you are using GetDlgItem and don't have a control variable,
that would be your problem.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Joseph M. Newcomer

unread,
Nov 23, 2009, 1:04:05 PM11/23/09
to
Note that for the control to access the Registry, it has to know the name of the app and
the vendor of the app; it certainly would make no sense to store this state globally; in
fact, it would be a key of the form

HKEY_CURRENT_USER\
Software\
VendorName\
ProductName\
DialogName\
ButtonColors\
ButtonID\
ColorName= RGBvalue

or
ColorName\
R=DWORD
G = DWORD
B = DWORD

so

HKCU\JMNCO\MyTestProgram\Settings\ButtonColors\101\Enabled=0xFF0000
HKCU\JMNCO\MyTestProgram\Settings\ButtonColors\101\Disabled=0x000000

Now the question is, how do you get the vendor name, product name, and the name of the
dialog ("Settings")? You have to somehow get that loaded first, and consequently, you are
back to having to set something in the button in the OnInitDialog handler (where
GetWindowText would give the name of the dialog) or in the OnInitialUpdate handler (where
you would need to figure out what the ID of the class was, e.g., the string value of
CYourClassName::IDD). So I don't see that just setting the colors at that point is a
problem.
joe

Cameron_C

unread,
Nov 23, 2009, 2:02:01 PM11/23/09
to
Hello Joe,
Yes I read through your article on PreSubclassWindow, and I just could not
make sense of why it simply never was invoked in my class.
I have about fifteen buttons on one page of my application as drop targets
for various things and a User suggested that different colours would be
helpful.
So, I decided to implemet cutomizing the colours .
Anyway to make a long story short, I spent the weekend staring at
OnPreSubclassWindow but somehow reading PreSubclassWindow, and I just never
lined up the mistake until AliR pointed it out. And yes I am a bit red faced
now.

My next effort will be to implementing something to allow the Users to move
the buttons around on their screen.

Thanks for your help.


"Joseph M. Newcomer" wrote:

> .
>

0 new messages