Stangeness of OnPaint

D

dominique

Hi,

In windows forms (vb.net), i use my own controls subclassed from base
controls and i override the Onxxx methods.

for example:
Public Class MyBouton
Inherits System.Windows.Forms.Button
..
Protected Overrides Sub OnPaint(ByVal pe As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pe)
..
Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
Mybase.OnClick(e)
..

I saw that :
- for some controls (ex: combobox), i can't access to OnPaint but i can
access to OnClick ..
- for these controls, if i had in the constructor :
Me.SetStyle(ControlStyles.UserPaint, true)
i can now access to OnPaint (then i must draw the control even if i put
MyBase.OnPaint(pe))
but if i put : Me.SetStyle(ControlStyles.UserPaint, false)
i can't access to none Onxxx

- in some controls (ex: Button) without Me.SetStyle, it is ok, i can
access to OnPaint

I saw also if i intercept a windows message, it is ok :

Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
Select Case m.Msg
Case &HF ' =15 =Paint

Can anybody explain this stangeness ?
Is it a known bug ?

Is it better to use always windows message instead of OnPaint ?

Thanks for advice.

Dominique Gratpain
 
H

Herfried K. Wagner [MVP]

dominique said:
In windows forms (vb.net), i use my own controls subclassed
from base controls and i override the Onxxx methods.

If you don't get an answer here, consider posting to the Windows Forms
group:

microsoft.public.dotnet.framework.windowsforms
microsoft.public.dotnet.framework.windowsforms.controls
 
J

Jay B. Harlow [MVP - Outlook]

dominique,
- for some controls (ex: combobox), i can't access to OnPaint but i can
access to OnClick ..
ComboBox's support something called Owner Draw, you need to handle the
OnDrawItem event instead of OnPaint.
- in some controls (ex: Button) without Me.SetStyle, it is ok, i can
access to OnPaint
That's only true if you leave ButtonBase.FlatStyle at Standard, Flat or
Popup, if you change the ButtonBase.FlatStyle to System (to gain Win XP
theme support) then you cannot access OnPaint.

NOTE: I'm curious if this holds true for VS.NET 2005 (currently in beta, due
out next year) as they have improved the theming support.
Can anybody explain this stangeness ?
Charles Petzold's book "Programming Microsoft Windows with Microsoft Visual
Basic .NET - Core Reference" from MS Press covers almost all the Windows
Forms controls and how to do custom painting in each.
Is it a known bug ?
I would say a limitation of Windows itself, which unfortunately is carried
into the framework, as the Framework is a wrapper around the Win32 controls.
Is it better to use always windows message instead of OnPaint ?
I would favor OnPaint & OnDrawItem over WndProc & dealing with the windows
message itself, as OnPaint & OnDrawItem already take care of all the interop
stuff, they give me a Graphics object, instead of needing to create one from
a Win32 handle...

In addition to

microsoft.public.dotnet.framework.windowsforms
microsoft.public.dotnet.framework.windowsforms.controls

You might also want to ask in:
microsoft.public.dotnet.framework.drawing

Hope this helps
Jay
 
D

dominique

Thanks Jay and Herfried for yours answers.

It is strange that the IDE suggest the OnPaint event for the ComboBox
subclass even thought it doesn't work .. but perharps it is because the
class was an user control before to become a combobox.

I am going to post this question in the three newsgroups you suggest.

Dominique
 
J

Jay B. Harlow [MVP - Outlook]

Dominique,
ComboBox inherits from Control (not UserControl).

Seeing as OnPaint is a member of Control, ComboBox has an OnPaint method.

Hope this helps
Jay
 

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