Create Visual Basic. NET control from scratch (5)

Source: Internet
Author: User
Step 2: draw the widget's appearance
To make the control have a visible appearance, we need to place the logic in the Paint event. Then, the logic is run every time the control needs to refresh its visual appearance.
The Paint logic in Windows Forms uses the class in the. net gdi + section. These classes basically include the Windows API graphics function. Because it is suitable for. NET, it is easier to use than API. However, you need to understand the following points about how they work.
In Windows APIs, graphical operations require a window handle, sometimes called hWnd. In GDI +, it is replaced by a Graphics object, which not only represents the drawing area, but also provides operations (methods) performed in the area ).
For example, a Graphics object has the following methods to draw various screen elements:
DrawCurve
DrawEllipse
DrawLine
DrawPolygon
DrawRectangle
DrawString
FillEllipse
FillPolygon
These are easy to understand, just examples of available methods. Some more complex methods also allow rotation of objects. We will use the DrawRectangle method to draw borders and the FillEllipse method to draw colored circles.
Most plotting methods require Pen or Brush objects. The Pen object is used to draw a straight line and determine the color and width of the line. The Brush object is used to fill the area, determine the color used for the filled area, and some special effects (for example, fill the area with a bitmap ). We will use a special Brush effect to darken the color of a light that is not currently on.
The following code processes the Paint event of the control:
Protected Overrides Sub OnPaint (ByVal pe _
System. Windows. Forms. PaintEventArgs)
MyBase. OnPaint (pe)
Dim grfGraphics As System. Drawing. Graphics
GrfGraphics = pe. Graphics
'First, draw three circles that represent the lamps.
'One is on, and the other two are off.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.