MouseEnter Control
-
I'm building a custom control and I want it to change borders when a MouseEnter events happens. a) Can I simply paint the border in the MouseEvent method? OR b) Must I set a flag and call Invalidate(), and wire OnPaint to draw the new/old border appropriately? If a) is ok, then how do I get a graphics instance in the MouseEnter event. Or, is b) the preferred method to do this? Thanks, -Luther
-
I'm building a custom control and I want it to change borders when a MouseEnter events happens. a) Can I simply paint the border in the MouseEvent method? OR b) Must I set a flag and call Invalidate(), and wire OnPaint to draw the new/old border appropriately? If a) is ok, then how do I get a graphics instance in the MouseEnter event. Or, is b) the preferred method to do this? Thanks, -Luther
Because there are several things that can happen while your mouse is moving or resting comfortably in the bounds of your control, it'd probably be best to set a flag (ex: set to
true
) and callInvalidate()
in the handler forMouseEnter
event, and to clear the flag (ex: set tofalse
) in the handler for theMouseLeave
event. Of course, your override forOnPaint
(note, this is an override, not an event handler, which are typically wired for other controls - use overrides when possible to override functionality from the base class) should draw the border appropriately depending on the flag. The first method is possible (seeControl.CreateGraphics
) but is too problematic sinceMouseEnter
only fires once. Your border would get painted, sure, but then any successive painting required won't repaint the border.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Because there are several things that can happen while your mouse is moving or resting comfortably in the bounds of your control, it'd probably be best to set a flag (ex: set to
true
) and callInvalidate()
in the handler forMouseEnter
event, and to clear the flag (ex: set tofalse
) in the handler for theMouseLeave
event. Of course, your override forOnPaint
(note, this is an override, not an event handler, which are typically wired for other controls - use overrides when possible to override functionality from the base class) should draw the border appropriately depending on the flag. The first method is possible (seeControl.CreateGraphics
) but is too problematic sinceMouseEnter
only fires once. Your border would get painted, sure, but then any successive painting required won't repaint the border.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
The flag method works cleanly - I typically try to avoid saving and polling state like that ... but practically speaking, I assume it makes sense in this stateful, event driven world. I also appreciate your comment regarding overrides vs handlers. It wasn't concretely clear to me when to use which. I see that for every child control I add to a parent, I could add a handler. Self-documenting and a bit easier to organize. Whereas, the control's own painting naturally belongs in the override. Thanks! -Luther