Finding out if your mouse is withing a GDI+ Shape on a Control

Denaes

Well-known member
Joined
Jun 10, 2003
Messages
956
Im drawing shapes on a picturebox.

I want to know when Im inside of a shape (and also outside of a shape).

This is really easy for a rectangle, you just check the X & Y & width & height to see if the mouse is inside.

Like say I have a wedge (using a piefill method to draw). The Rect covers the whole circle, but the Wedge is drawn with the Rect, a starting angle and an ending angle. Im just not sure how to get at those points in the angle...

Im not sure if there is a method for the premade drawing methods that does this or if I have to use the easy methods and then do it all myself to find the points anyways (I guess by figuring out the two lines and the arc)
 
I dont know of any built in methods for checking collision detection of non rectangular shapes; however, assuming you have a Right Triangle, and you know the length of the legs (height , and width) you can calculate the hypotenuse using the Pythagorean Theorem, and thus figure out if the mouse position is inside the area of the triangle or not.
 
GraphicsPath object

If your piefill method can be adapted to create a GraphicsPath object instead of, or in addition to, the rendering, then you can use the IsVisible method to test whether a point (such as the cursor) lies within the segment. A GraphicsPath object can be created by adding the lines and curves that make up the pie segment.

This approach will work with any kind of shape which can be represented as a GraphicsPath.

Good luck :)
 
Unfortunately not right triangles.

Searching around Ive seen mention of creating objects for each shape and then looping through the shapes to see if the point is in/out of bounds... that does simplify things a little, but you still have to compare the points to the shape/location of the shape you drew.

A few tests rely on making a bitmap of the drawing (I think of the whole drawing screen because I dont think you can make irregular sided bitmaps), but those do a check for the color of the pixel at the point given, which works for color coded shapes.
 
Hmm, the graphicsPath seems like a good place to store the image. Just have to see if there is an easy way to do it (ie, the .draw<shape> methods) or if you have to draw the shape by hand.

Edit:

GraphicsPath.AddPie works the same as Graphics.DrawPie (or so it seems) so Ill create an object for each of the shapes that will handle their graphicspath and checking mouse and drawing the shape to the screen. Ill post here later with an example if I get it working as I hope for future people with a similar problem
 
Last edited by a moderator:
Okay, I really went an easy route as I already had much of the pertinent info for shapes stored in objects already and everything had to be recalculated with redraws because I change sizes along with form resizes.

So here is how it works:

Code:
            PiePath = New Drawing2D.GraphicsPath
            PiePath.AddPie(CircleRect, StartAngle, SweepAngle)
            If PiePath.IsVisible(MouseLoc) Then
                 Mouse is over wedge, so highlight the wedge and display its info
                g.FillPie(Brushes.Gray, CircleRect, StartAngle, SweepAngle)
            End If

            g.DrawPie(PiePen, CircleRect, StartAngle, SweepAngle)

This checks each wedge prior to it being drawn and if the mouse is in its shape, I use the FillPie to give it a color (kinda like highlighting it).

This ended up being super easy with the GraphicsPath object, thanks :)
 
Back
Top