Come near VB.net (vii) drawing on a form
If you read some articles you must know that drawing on a form to use the System.Drawing class, there is no line and label control in the vb.net, you need to write some code,
First, write an event procedure OnPaint. Display a graphic while displaying a form
Protected Overrides Sub OnPaint (ByVal e as PaintEventArgs)
' TODO: Please add the drawing code here
End Sub
ByVal e As PaintEventArgs is a parameter class, which is somewhat like a custom type used in VB6, with multiple member variables in a set, (reference
"VB." NET core definition of the collection/member). You can see the relevant parameters by following the E. Obtaining x,y coordinates in events such as MOUSE_DOWN,MOUSE_UP is used
E.x and E.y. Some of the events on the form need to be added manually, the previous chapters have the relevant content, and now we're going to use E. Graphics parameters,
Please add the following code:
Dim gp as Graphics = E.graphics
Of course you have to remember to add an imports System.Drawing to the beginning of the program code so system.drawing.graphics can be written as graphics, and System.Drawing.Pen
Can be written as a pen
And we're going to create a brush object, which is just like in reality, the brush is an object, and we use the brush to draw.
Dim Newpen as New Pen (color.black,5)
You can even write this:
Dim Newpen as New Pen (color. FromArgb (22, 23, 24), 5)
Pen is an overloaded function, that is, there are several formats, after the comma appears after the hint can see a downward key and number, click the arrow can
View different parameters used by different versions of the function. This uses a FromArgb function of the System.draw.color class, and since he is also an overloaded grandchild, you can write
A,r,g,b Four parameters can also be written to r,g,b three parameters, a total of four versions for you to use, as long as the comma after the occurrence of the hint can see a downward key head and number can be.
You can use only one color parameter, or add an integer to the back, define the size of the brush, that is, how wide the line should be drawn, remember color is also system.drawing
A class such as: Dim Newpen as New Pen (Color.Black)
Now use the DrawLine method to draw the lines you want, as follows:
Gp. Drawline (Newpen, 10, 20, 70, 80)
DrawLine is also an overloaded method (Overloads public Sub DrawLine) that typically uses 5 parameters, and the following four can be either integer or single
A floating-point type. This is an integer.
The first is to represent the brush, followed by x1,y1 (start coordinates), X2,y2 (endpoint coordinates)
Draw a rectangle as follows:
Protected Overrides Sub OnPaint (ByVal e as PaintEventArgs)
Dim gp as Graphics = E.graphics
Dim Newpen as New Pen (Color.Black, 5)
Gp. DrawRectangle (Newpen, 10, 20, 70, 80)
End Sub
Drawing a square requires the last two parameters to be the same:
Protected Overrides Sub OnPaint (ByVal e as PaintEventArgs)
Dim gp as Graphics = E.graphics
Dim Newpen as New Pen (Color.Black, 5)
Gp. DrawRectangle (Newpen, 10, 20, 80, 80)
End Sub
Draw a parabola as follows:
Protected Overrides Sub OnPaint (ByVal e as PaintEventArgs)
Dim gp as Graphics = E.graphics
Dim Newpen as New Pen (Color.Black, 5)
Gp.drawarc (Newpen, 10, 20, 70, 80, 123, 233)
End Sub
The Bezier curves are as follows:
Protected Overrides Sub OnPaint (ByVal e as PaintEventArgs)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.