Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# GDI+
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 20-Apr-11 2:53am
Groulien2.9K
Rate this: bad
good
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
 
Regards,
 
-MRB
  Permalink  
v3
Comments
   
5 - Proposed as answer
Kim Togo at 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.
SAKryukov at 20-Apr-11 19:05pm
   
Not 100% what is needed. It's more simple, too.
Please see my answer.
--SA
Rate this: bad
good
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.
  Permalink  
Comments
Manfred R. Bihy at 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 at 20-Apr-11 8:28am
   
Good spot! +5
BobJanova at 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.
nbgangsta at 20-Apr-11 17:50pm
   
Worked, thanks a lot!
SAKryukov at 20-Apr-11 19:04pm
   
Yes, but not 100% what you want.
Please see my answer.
--SA
murathankocan at 30-Jan-13 15:06pm
   
there is a better one at
http://www.youtube.com/watch?v=K_JzL4kzCoE
Rate this: bad
good
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 http://msdn.microsoft.com/en-us/library/dd145102(v=vs.85).aspx[^].
 
—SA
  Permalink  
v2
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 290
1 Jochen Arndt 165
2 DamithSL 125
3 PIEBALDconsult 110
4 Garth J Lancaster 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 20 Apr 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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