Strangeness in 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 strangeness ?
Is it a known bug ?

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

Thanks for advice.

Dominique Gratpain
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi dominique,

What do you mean you by 'can access and can't access'?
Is it that you method is not called by the framework or something else.

Please, give us more details. One small sample demonstating the problem
would help us.
 
D

dominique

Hi Stoitcho,

Thanks for your answer.

For exemple, when i use :
Public Class MyCombobox
Inherits System.Windows.Forms.ComboBox
..
Protected Overrides Sub OnPaint(ByVal pe As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(pe)

never i go on the OnPaint method, this method is not called by the
framework.

I must use win32 message :
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
Select Case m.Msg
Case &HF ' =15 =Paint

Try it.

I posted this question also in two other newsgroups of microsoft :

microsoft.public.dotnet.languages.vb
look at : http://www.developersdex.com/vb/message.asp?p=1121&r=4621554

microsoft.public.dotnet.framework.drawing
look at : http://www.developersdex.com/vb/message.asp?p=2919&r=4624287T

Bob and Jay said that it is the fault of Windows.
I think that it is not normal and i hope it will be better with VS 2005
.. but there is always Windows.

There are no many articles on that.

Dominique
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Dominique,

As a windows control combobox is a rather complex control not to speak that
WindowsForms' ComboBox t has a lot of bugs. When I say complex I mean that
it is combination of edit control and list control. Both parts are different
windows with different hwnds and paint surfaces.
It is not easy to hadle painting on the control in a single event hadnler.
Further more combobox needs to track selection, hailighting and so forth,
which will be imposible if the control doesn't know the geometry of the
combobox items.

That's why to paint comboboxes one needs to use different techniques.

To paint the combobox first you need to set its DrawMode to
DrawMode.OwnerDrawXXX style. Then hook on MeasureItem and DrawItem events
 

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