|
VB .NET Techniques and References
|
|
|
Graphics
|
|
Table of Contents
General Graphics Information
All the features described on this page use the Drawing2D package of .NET.
Therefore, classes which use these features must import this package with the following statement at the beginning of the class file:
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.
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
Shaping Controls
All objects have a
Region Property
which desctibes the region of that control where painting is allowed and where mouse events are detected.
By default, this region is rectangular of size defined in the Size property of the control, and encompasses the entire control.
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
- Create a GraphicsPath for that control (e.g. mypath) and shape it as a series of lines, curves, polygons, etc. depending on what
you want the shape to be.
- Create a new Region (e.g. myregion for that control based on that path:
myregion = New Region(mypath)
- Set the control's region to be that region:
Me.Region = myregion
If you are defining a user defined control such that all the instances of that control have the same size and shape,
then it makes sense to define the region as a class variable once (with the qualifier Shared in the definition
and have the Region of all the instances point to that class variable.
Copying Images
Another way to create some graphics in a control is to copy a whole or part of an image into this control.
In this case you would
- Create the Graphics object from scratch as a bitmap
as explained here
- Use the
Graphics.DrawImage methods
to draw into into it.
Note that this method is overloaded with many options depending on what you want to do.
Colours
.NET has a
Color class
which is used to work with colours.
This page is maintained by
Sophie Quigley
(cps613@cs.torontomu.ca)
Last modified
Thursday, 20-Oct-2022 20:25:15 EDT