CPS613 |
VB .NET Techniques and References | |
Graphics |
Imports System.Drawing.Drawing2D
General information about how to change the look of a control can be found at the MSDN
Custom Control Painting and Rendering page.
Drawing Lines
The simplest way to draw on a control is in the
OnPaint method
for that control. This is because this method will be automatically called whenever the control is refreshed, which can happen
automatically for a variety of reasons but can also be made to happen programmatically by calling the
Refresh Method.
The OnPaint method is passed a parameter e, of type PaintEventArgs which has a Graphics property containing a Graphics object which is used to draw or paint onto the control. Therefore the simplest approach to drawing on a control is to draw on the Graphics object which is passed to the control in its OnPaint method, as shown here.
To draw on the graphics object, simply use one of its methods. Note that some of these methods, such as DrawCurve, DrawLines, and DrawPolygon, require an array of points from which to draw. If you are defining this array dynamically, you will need to change the size of the array dynamically. If you decide to use the ReDim statement, you should be aware that it is somewhat counterintuitive because the parameters represent the last index of the array and not the size:
ReDim arrayname(-1) as Integer 'arrayname now has 0 elements ReDim arrayname(0) as Integer 'arrayname now has 1 elements and the last index is 0 ReDim arrayname(10) as Integer 'arrayname now has 11 elements and the last index is 10
The shape (and therefore effective size) of a control can be changed by changing that property which is of type Region As you can see, one of the Region constructors can construct a region out of a GraphicsPath which is a series of connected graphs and lines. The add methods of the GraphicsPath class can be used to add shapes such as ellipses, rectangles and polygons to a GraphicsPath.
Therefore to change the shape of a control to be anything other than a rectangle, you must
myregion = New Region(mypath)
Me.Region = myregion