Click here to Skip to main content
14,668,073 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello everyone,

I am looking for a method to check if my cursor is currently over a specific draw so it can be highlighted.

Let's say this is may shape:
 /          /

As you can obviously see, there's a bit of spare space (buttons are usually straight boxes).
How can I exclude the spare space?

Just so you know, I draw the lines using a GraphicsPath.

Rate this:
Please Sign up or sign in to vote.

Solution 1

Check if the cursors coordinates (as a Point type) lie within in that region.
Pont point = ...; //Set mouse coordinates here.
GraphicsPath path = ...; // Create your path here.
if(path.IsVisible(point)) ... // Check if mouse coordinates are within path


#realJSOP 20-Apr-11 8:14am
5 - Proposed as answer
Kim Togo 20-Apr-11 8:35am
My 5. OP is using GraphicsPath to draw the lines and IsVisible returns true or false that indicates whether the specified point is contained within this GraphicsPath.
Sergey Alexandrovich Kryukov 20-Apr-11 19:05pm
Not 100% what is needed. It's more simple, too.
Please see my answer.
Rate this:
Please Sign up or sign in to vote.

Solution 3

As Manfred has said, convert your Path to a Region and use the Region.IsVisible(Point)[^] method.
Manfred Rudolf Bihy 20-Apr-11 8:25am
I changed my mind when I found out that GraphicsPath also has the IsVisible(Point point) method. Anyways my 5+!
Henry Minute 20-Apr-11 8:28am
Good spot! +5
BobJanova 20-Apr-11 9:51am
You can also assign this region to the Control.Region property so that mouse events are only picked up for the control within the region, painting is clipped to it etc.
Groulien 20-Apr-11 17:50pm
Worked, thanks a lot!
Sergey Alexandrovich Kryukov 20-Apr-11 19:04pm
Yes, but not 100% what you want.
Please see my answer.
murathankocan 30-Jan-13 15:06pm
there is a better one at
Rate this:
Please Sign up or sign in to vote.

Solution 4

All other answers are not quite correct. They only help to define visible area or process mouse events in a special way. The ultimate solution is this:

Use Windows API SetWindowRgn on a button HWND. It will shape a buttons Windows to a region in all aspects, not just visual, but the mouse event will be automatically routed only if they happen within the specified region. See[^].

Rate this:
Please Sign up or sign in to vote.

Solution 2

Perhaps make a Panel, do your own drawing there and then listen for event Control.MouseEnter[^]

In the MouseEnter event, use Cursor.Position[^] to validate if the cursor is inside your own drawing. If it is, change the color.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100