10.2. Perform Hit Testing with Shapes

Problem

You need to detect whether a user clicks inside a shape.

Solution

Test the point where the user clicked with methods such as Rectangle.Contains and Region.IsVisible (in the System.Drawing namespace) or GraphicsPath.IsVisible (in the System.Drawing.Drawing2D namespace), depending on the type of shape.

How It Works

Often, if you use GDI+ to draw shapes on a form, you need to be able to determine when a user clicks in a given shape. You can determine this using a Rectangle and a Point. A Rectangle is defined by its height, width, and upper-left coordinates, which are reflected by the Height, Width, X, and Y properties. A Point, which is an X and Y coordinate, represents a specific location on the screen. ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.