Create irregular forms using VB. NET

Source: Internet
Author: User

Create irregular forms using VB. NetGenerally, the form of an application is a regular, that is, a rectangular form. Sometimes for some special purpose, we want to change the shape of the application form, such as creating a personalized mp3 player and a small clock. This requires "Customization" of our application. In addition, forms with special shapes can sometimes attract user attention, so that they can pay special attention to your program.

In VB6, we generally use the following code to create irregular forms.
Private Declare Function setjavaswrgn Lib "user32" (ByVal hWnd As Long, _ ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, _ ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

Private Sub Form_Activate ()
Dim hndRgn As Long
HndRgn = CreateEllipticRgn (0, 0,175,175)
Call setmediawrgn (Me. hWnd, hndRgn, True)
Call DeleteObject (hndRgn)
End Sub
First, use Win32 API CreateEllipticRgn to create a circular area, and then set the Form area to the custom Region. In this way, we can get the following circular Form to be more beautiful, we can set the BorderStyle of Form to None, and then draw a TitileBar by ourselves to make it a lively application.

How to achieve the above effect in VB. Net:
VB. NET is a cross-platform language that makes better use of the object-oriented mechanism. Its object-oriented capability extends the path of the language itself: Everything is an object. This means that you have gained more internal functions than in earlier VB versions, and you will rarely be forced to use Windows APIs. Therefore, in VB. Net, we had to give up the concept of using APIs in VB6 and use the powerful object mechanism of VB. Net to elaborate on the above topics.
In VB. Net, Form has a Reigin attribute. By creating a custom Reigin and specifying the Form Reigin, we can get an irregular Form. In addition, the Reigin object in VB. Net has powerful functions and far exceeds the limitations of VB. Therefore, we can make many beautiful interfaces.
Regin object
An object of System. Drawing that indicates the interior of a rectangle and a graph shape composed of a path. Because the coordinates of a region are specified in the global coordinates, You can scale it. However, on the drawing surface, its interior depends on the size and shape of its pixels. Applications can use several areas to stack the output of drawing operations. The Window Manager uses the area to define the drawing area of the window. These areas are called editing areas. Applications can also use a region in hit detection operations, such as checking whether a vertex or rectangle is intersecting with a certain region. Applications can fill an area by using a Brush object.
GraphicsPath represents a series of interconnected straight lines and curves. The application uses paths to draw shape outlines, fill shapes inside and create clip areas. The graphics engine maintains geometric coordinates in the path in the global coordinate space. A path can be composed of any number of images (sub-paths. Each graph is composed of a series of connected straight lines and curves or geometric elements. The starting point of a graph is the first point in a series of straight lines and curves that are connected to each other. The endpoint is the last point in the sequence. The starting and ending points of a geometric primitive are defined by the primitive specification.
Let's take a look at two specific instances.
1. Add the following code to the Form Load event in text format:
Dim text_path As GraphicsPath
Dim text_region As Region
Me. BackColor = Color. Red
Me. Width = 600
'Create the text path.
Text_path = New GraphicsPath (Drawing. Drawing2D. FillMode. Alternate)
Text_path.AddString ("CSDN", New FontFamily ("Times New Roman"), FontStyle. Bold, 200, New Point (10, 10 ),
StringFormat. GenericDefault)
'Create a Region from the path.
Text_region = New Region (text_path)
'Strain the form to the region.
Me. Region = text_region run the following form. Remember to press Shift + F5 to stop the program.
2. elliptical form:
Add the following code to obtain the form Me. Width = 300.
Me. Height = 220
Me. BackColor = Color. RoyalBlue
Dim m_path As GraphicsPath
M_path = new graphicspath (fillmode. Winding)
M_path.addellipse (1, 1,200,200)
Dim m_region as new region (m_path)
Me. region = m_region the above are just two simple examples. By using region and graphicspath objects, you will get more interesting results.

 

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.