Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted

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


Regards,

-MRB
 
Share this answer
 
v3
Comments
#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.
--SA
As Manfred has said, convert your Path to a Region and use the Region.IsVisible(Point)[^] method.
 
Share this answer
 
Comments
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.
--SA
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 http://msdn.microsoft.com/en-us/library/dd145102(v=vs.85).aspx[^].

—SA
 
Share this answer
 
v2
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900