VB.NET creates an irregular form

zhaozj2021-02-17  72

Generally speaking, the form of the application is rule, that is, a rectangular form. Sometimes for a particular purpose, we want to change the shape of the application form, such as doing a full-time MP3 player, a small clock, etc., which requires "custom" our application. In addition, special shape forms can sometimes attract users' attention so that they pay particular attention to your procedure. In VB6, we generally create an irregular form by code. Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, _ ByVal hRgn As Long, ByVal bRedraw As Boolean) As LongPrivate Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, _ ByVal Y1 As Long, ByVal X2 As Long , ByVal Y2 As Long) As LongPrivate Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As LongPrivate Sub Form_Activate () Dim hndRgn As Long hndRgn = CreateEllipticRgn (0, 0, 175, 175) Call SetWindowRgn (Me.hWnd, HNDRGN (TRUE) CALL DELETEOBJECT (HNDRGN) End Sub First use Win32 API CreatellipticRGN to create a circular area, then set the Form's area as the User-defined Region, so we can get one round form below.

In order to be aesthetically pleasing, we can set the form's borderStyle as none, then draw a titlebar yourself, you can make a more lively application. How to implement the above effects in VB.NET: VB.NET is a cross-platform language, better use of object-oriented mechanisms. Its object-oriented ability extends the path of language itself: Everything is an object. This means that more intrinsic features are more inner features than in previous VB versions, you will be rarely forced to use the Windows API. So in VB.NET, we have to give up the concept of VB6 to use the API, and use VB.NET powerful object mechanisms to explain the above topics. In Vb.Net, the Form has a REIGIN attribute that we can get an irregular form by creating a custom REIGIN, then specifying the Form's REIGIN. And the REIGIN object in VB.NET is powerful, far exceeds the previous VB limit, so we can make a lot of beautiful interfaces.

An object of the Regin object system.drawing indicates the inside of a graphical shape made of a rectangle and a path composed of a path. Since the coordinates of the area are specified in the global coordinates, it can be scaled. However, on the drawing surface, its internal depends on the size and shape of its pixels. Applications can use several regions to stack the output of drawing operations. Window Manager uses the area to define the drawing area of ​​the window. These areas are called a clip area. The application can also use the area in the operation detected, such as checking if a point or rectangle intersects an area. The application can populate a zone by using a Brush object. GraphicsPath represents a range of interconnected straight lines and curves, the application uses the path to draw the shape of the contour, fill the shape inside and creates a clip area. The graphics engine maintains the coordinates of geometry within the path in the global coordinate space. The path can be composed of any number of graphics (sub-paths). Each graphic is composed of a series of interconnected straight lines and curves or geometric shaped primitives. The starting point of the graph is the first point in a series of straight lines and curves that are connected to each other. The end point is the last point in this sequence. The starting point and endpoint of the geometric shape primitive are defined by the primitive specification. Let's take two specific examples 1. Forms in text format Add: DIM text_path as region.backdim text_region as regionme.backcolor = color.redme.width = 600 'create the text path.text_path = new GraphicsPath (Drawing.Drawing2D.FillMode.Alternate) text_path.AddString ( "9CBS", 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) 'constrain the form to the region.me.region = text_region

Running will get the following shape, remember to press the SHIFT F5 abort process.

2. Elliptical form: Similarly to the following code, get the following form

Me.Width = 300Me.Height = 220Me.BackColor = Color.RoyalBlueDim m_path As GraphicsPathm_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 is just two simple examples. By using the Region and GraphicsPath objects, you will get more interesting results.

转载请注明原文地址:https://www.9cbs.com/read-30885.html

New Post(0)