About Paint Event

K

Kürþat

Hi all,

I do some drawing in a form's paint event handler and I have a button on
that form. Whenever the mouse enters or leaves the button Form's paint event
occurs. Isn't that a strange behavior? Is it possible to prevent that?

Thanks.
 
M

Morten Wennevik

Hi,

Are you doing something in the Enter/Leave events?

The mouse cursor itself does not trigger paint events as Windows keeps
track of whatever the cursor covers and paints it back when the cursor
moves.
 
K

Kürsat

Hi,
No, Enter/Leave events aren't handled. Button itself triggers paint event.
Tested on different systems. Strange but true! (Button is XP style)
 
M

Morten Wennevik

Ah, the Button's Paint event.

I think because the Button can change characteristics based when a cursor
moves over it, it receives a Paint when the mouse enters/leaves.

You can prevent this by created your own button and disable the default
OnMouseEnter and OnMouseLeave events

public class MyButton : Button
{
protected override void OnMouseEnter(EventArgs e)
{
//base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
//base.OnMouseLeave(e);
}
}


Hi,
No, Enter/Leave events aren't handled. Button itself triggers paint
event.
Tested on different systems. Strange but true! (Button is XP style)
 
S

Stoitcho Goutsev \(100\)

Morten,

The paint event is treiggered by a WM_PRINTCLIENT messages receieved by the
form.
I believe this has something to do with VisualStyles, but the interesting
part is that this message is send only by the push button. Check boxes for
example doesn't have this effect. Sop what I did is to handle that message
in the WndProc. Doing this I managed to suppress the strange paint event and
everything with the XP styles looks fine. However I think this is send by
the button for a reason and I cannot guarantee that there won't be any
problems beside the fact that the control won't respond to the
WM_PRINTCLIENT message correctly :).

Override the form's WndProc method and add the following code

const int WM_PRINTCLIENT = 0x0318;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_PRINTCLIENT)
{
return;
}
base.WndProc(ref m);
}
}



--
HTH
Stoitcho Goutsev (100)

Kürsat said:
Hi,
No, Enter/Leave events aren't handled. Button itself triggers paint event.
Tested on different systems. Strange but true! (Button is XP style)
 

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