Scrollbars and controls in general

J

Jon Slaughter

I'm trying to wrap all the basic controls(Button, scrollbar, etc..) to allow
for skinning. The problem is that many of the controls seem so different in
the way they work. For example, right now I'm trying to wrap teh scrollbar
class but when I override the OnPaint and OnPaintBackground prevent painting
the scrollbar is still painted. How the heck am I suppose to know how to do
these things if I can't find the information about how they work? I would
expect when I override these functions I would prevent the scrollbar from
drawing itself but this doesn't seem to be the case ;/ (while it is the case
for the button)

Ok, so maybe the scrollbar paints in a different way? Maybe the scrollbar
uses teh Controls property to hold the buttons and stuff but still, how does
it paint itself then? When I override the paint methods shouldn't I expect
that no painting should be performed?

It looks like I will have to just implement a new scrollbar class but then
it will be difficult to support the themes of the os? I figured that it
would be very easy just to override the painting and hit tests along with a
few others if I a flag was set and draw a better looking scrollbar that
functions the same way as the original but can easily be turned off. One
could easily refactor it into a program without making any programmatic
changest to support the new look too. But again, I'm running into trouble
trying to figure out what MS did. MSDN doesn't help as it just gives me a
list of properties and methods for the class but not how the class actually
works. (like if they forgot to make the OnPaint virtual so I could override
it)

any ideas? Anywhere I can find some info on this topic that will help clear
up some of my confusion?

Thanks,
Jon
 
D

Dave Sexton

Hi Jon,
I'm trying to wrap all the basic controls(Button, scrollbar, etc..) to allow
for skinning. The problem is that many of the controls seem so different in
the way they work. For example, right now I'm trying to wrap teh scrollbar
class but when I override the OnPaint and OnPaintBackground prevent painting
the scrollbar is still painted. How the heck am I suppose to know how to do
these things if I can't find the information about how they work? I would
expect when I override these functions I would prevent the scrollbar from
drawing itself but this doesn't seem to be the case ;/ (while it is the case
for the button)

Try calling the following protected method in your derived class:

SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint,
true);

Then, you should only have to override OnPaint. I haven't tried this myself,
so please let me know if it works for you.

It looks like I will have to just implement a new scrollbar class but then
it will be difficult to support the themes of the os? I figured that it
would be very easy just to override the painting and hit tests along with a
few others if I a flag was set and draw a better looking scrollbar that
functions the same way as the original but can easily be turned off. One
could easily refactor it into a program without making any programmatic
changest to support the new look too. But again, I'm running into trouble
trying to figure out what MS did. MSDN doesn't help as it just gives me a
list of properties and methods for the class but not how the class actually
works. (like if they forgot to make the OnPaint virtual so I could override
it)

You can download Rotor, Microsoft's open source CLR, or use .NET Reflector to
go inside and take a look.
any ideas? Anywhere I can find some info on this topic that will help clear
up some of my confusion?

If you are trying to rebuild the Windows controls suite via custom painting
then I'm not sure that deriving from the classes themselves are going to be
useful to you. At this point, you're probably better off creating your own
custom control suite from scratch since the Windows controls were not designed
for managed skinning, per se, although in the 2.0 framework there is new
support for drawing XP styles using the *Renderer objects of related controls.
e.g., ButtonRenderer, CheckBoxRenderer, ScrollBarRenderer, etc.

The closest you'll get to skinning a managed control now is with the
BackgroundImage, BackColor and BorderStyle properties so why not just roll
your own controls or buy a third-party suite? ;)
 
J

Jon Slaughter

Dave Sexton said:
Hi Jon,


Try calling the following protected method in your derived class:

SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint,
true);

Yes, this did the trick. I suppose I have to spend some more time
investigating the nuances that exist in the controls class. Just hard to
find any good information on it and I guess I'm being a little lazy in
expecting everything to be laid out for me ;/
Then, you should only have to override OnPaint. I haven't tried this
myself, so please let me know if it works for you.



You can download Rotor, Microsoft's open source CLR, or use .NET Reflector
to go inside and take a look.

ok, I'll check it out. Thanks.
If you are trying to rebuild the Windows controls suite via custom
painting then I'm not sure that deriving from the classes themselves are
going to be useful to you. At this point, you're probably better off
creating your own custom control suite from scratch since the Windows
controls were not designed for managed skinning, per se, although in the
2.0 framework there is new support for drawing XP styles using the
*Renderer objects of related controls. e.g., ButtonRenderer,
CheckBoxRenderer, ScrollBarRenderer, etc.

The closest you'll get to skinning a managed control now is with the
BackgroundImage, BackColor and BorderStyle properties so why not just roll
your own controls or buy a third-party suite? ;)

But I'd like to support the themes for the OS. I was just going to implement
the scrollbar as a control and do everything myself but I figured that its
no more or less complicated than just deriving from the Scrollbar class
instead of the control class. I'd automatically get the ability to use the
OS's themes by doing this. (just a tad bit more code but nothing
complicated)

Thanks,
Jon
 
D

Dave Sexton

Hi Jon,

But I'd like to support the themes for the OS. I was just going to implement
the scrollbar as a control and do everything myself but I figured that its
no more or less complicated than just deriving from the Scrollbar class
instead of the control class. I'd automatically get the ability to use the
OS's themes by doing this. (just a tad bit more code but nothing
complicated)

The *Renderer classes in 2.0 allow you to do just that - apply the current
visual styles of the OS to your controls:

ScrollBarRenderer on MSDN:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.scrollbarrenderer.aspx

So, you don't even have to derive from the Windows controls unless you simply
want to retain their public properties and designer support. But you can
always write the properties that you need yourself and use the BCL's
IDesigners on your own controls as well. And using the Windows controls will
be restrictive because you might want to override some properties that aren't
virtual, or you might want to modify some functionality that's internal to the
Control itself. Your best bet is to roll your own, IMO. That way you can use
a skinning architecture that is appropriate for your controls, such as an
ISkin interface and a ButtonSkin implementation, for example, that is assigned
to your SkinableControls.Button class via a VisualSkin property, or something
like that. The hardest controls to rewrite are going to be those controls
that accept text input from an end-user, however you can always just wrap
those controls. ;)
 
J

Jon Slaughter

Dave Sexton said:
Hi Jon,



The *Renderer classes in 2.0 allow you to do just that - apply the current
visual styles of the OS to your controls:

ScrollBarRenderer on MSDN:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.scrollbarrenderer.aspx

So, you don't even have to derive from the Windows controls unless you
simply want to retain their public properties and designer support. But
you can always write the properties that you need yourself and use the
BCL's IDesigners on your own controls as well. And using the Windows
controls will be restrictive because you might want to override some
properties that aren't virtual, or you might want to modify some
functionality that's internal to the Control itself. Your best bet is to
roll your own, IMO. That way you can use a skinning architecture that is
appropriate for your controls, such as an ISkin interface and a ButtonSkin
implementation, for example, that is assigned to your
SkinableControls.Button class via a VisualSkin property, or something like
that. The hardest controls to rewrite are going to be those controls that
accept text input from an end-user, however you can always just wrap those
controls. ;)

Ok, This looks interesting. Unfortunately, for some reason, the scrollbar
renderer does work for non-visual styles so I'd still have to emulate the
look and feel of windows if I wanted to(although it doesn't seem to hard if
I can easily get the metrics for it(which I kidna done already)). But this
does seem to work.

Thanks for your time. I think I might be on the right track now.

Thanks again,
Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top