How to override the Form.OnPaint method


T

Tom P.

I am trying to take over the painting of a windows form but I can't
seems to override the OnPaint method... or the OnPaintBackground
method... or even the OnNotifyMessage method if there is a control
covering the client area. None of these are getting called in any way.
I've tried using SetStyle(ControlStyles.AllPaintingInWmPaint, true);
and SetStyle(ControlStyles.UserPaint, true); but nothing works. If
there is no client area the OnPaint is never getting called. Is there
a different method for painting the actual Form? How do I repaint the
titlebar and menus and everything?

How do I actually override the entire OnPaint method for the form?

Tom P.
 
Ad

Advertisements

R

RobinS

How about if you bring up the form in design mode. Go to the form properties
window, click on the lightning bolt to bring up the available events, find
Paint. Double-click on the blank space next to it. This will add an
eventhandler to your code-behind for the painting of the form.

Will it completely override? I don't nkow. Try it and see.

RobinS.
GoldMail.com
 
P

Peter Duniho

How about if you bring up the form in design mode. Go to the form
properties window, click on the lightning bolt to bring up the available
events, find Paint. Double-click on the blank space next to it. This
will add an eventhandler to your code-behind for the painting of the
form.

Will it completely override? I don't nkow. Try it and see.

It won't. It will handle the Paint event, not override OnPaint().

But Tom's question isn't really about overriding OnPaint(). Overriding
that method is easy. You just add a new OnPaint() method in your subclass
with the keyword "override". That's not what he's asking about though (in
spite of this inaccurate subject line).

He really wants to override the non-client rendering of the window. As
far as I know, there's no way to do this directly from .NET. I think it's
possible that if he overrides the WndProc() method and handles the
NC_PAINT messages himself it _might_ work. But I don't really know enough
about how .NET and the underlying unmanaged window class interact to know
for sure.

Pete
 
Ad

Advertisements

T

Tom P.

It won't. It will handle the Paint event, not override OnPaint().

But Tom's question isn't really about overriding OnPaint(). Overriding
that method is easy. You just add a new OnPaint() method in your subclass
with the keyword "override". That's not what he's asking about though (in
spite of this inaccurate subject line).

He really wants to override the non-client rendering of the window. As
far as I know, there's no way to do this directly from .NET. I think it's
possible that if he overrides the WndProc() method and handles the
NC_PAINT messages himself it _might_ work. But I don't really know enough
about how .NET and the underlying unmanaged window class interact to know
for sure.

Pete

Thanks again Pete,

I should have known you'd be the one to point me in the right
direction. WM_NCPAINT is the message I was supposed to capture but I
still wasn't able to paint in the Non-client area. After a bit more
poking I discovered that Vista does some special stuff during the non-
client paint and it's best to just leave it alone. I'll have to
subclass some of the other controls in my client area (like the
menubar and toolstrip) to do the skinning I want but the window frame
and NC area are out of my league.

Now on to why Vista doesn't handle string trimming correctly.

Tom P.
 

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