Use API functions to implement "atypical" forms in Delphi

Source: Internet
Author: User
Tags textout

Use API functions to implement "atypical" forms in DELPHI
Some of the current shared software, especially some multimedia player software, pay great attention to the design of the program interface to attract users. In fact, we can use API functions to achieve nice and alternative effects.
1. Special form
Can the form be square only? No, there can be other shapes. This requires two Win32 API functions. First, use the CreateRoundRectRgn () function to define an elliptical area in the form. The region referred to here is a special API object. We can perform operations such as filling and cropping within the region to define the external features of the form. Call the setjavaswrgn () function to draw a graph. You can also use the CombineRgn () function to merge multiple regions. For example, add the following code to the unit file of Delphi:
Procedure TForm1.FormCreate (Sender: TObject );
Var
FRegion1: THandle;
FRegion2: THandle;
Begin
FRegion1: = CreateRoundRectRgn (20, 20, 200,200,300,300); // defines an elliptical Region
FRegion2: = CreateRectRgn (170,170,400,400); // defines a rectangular area
CombineRgn (FRegion1, FRegion1, FRegion2, RGN_OR); // connect the two regions
Setjavaswrgn (handle, FRegion1, True); // draw the connected area
End;
The CreateRoundRectRgn () function in the program section is used to create a rounded rectangle. Its prototype is:
HRGN CreateRoundRectRgn (
Int nLeftRect, // X coordinate in the upper left corner
Int nTopRect, // Y coordinate in the upper left corner
Int nRightRect, // X coordinate in the lower right corner
Int nBottomRect, // Y coordinate in the lower right corner
Int nWidthEllipse, // The width of the elliptic where the rounded corner is located
Int nHeightEllipse // The height of the elliptical corner
);
Other images, such as polygon and elliptical, have corresponding API functions. Their prototype is as follows:
HRGN CreateEllipticRgn (int nLeftRect, int nTopRect, int nRightRect, int nBottomRect)
Hrgn createellipticrgnindirect (const rect * LPRC)
Hrgn createpolygonrgn (const point * lppt, int cpoints, int fnpolyfillmode)
Hrgn createpolypolygonrgn (const point * lppt, const int * lppolycounts, int ncount, int fnpolyfillmode)
Hrgn createrectrgn (INT nleftrect, int ntoprect, int nrightrect, int nbottomrect)
Hrgn createrectrgnindirect (const rect * LPRC)
The parameters of the above functions are well understood and used to describe the coordinates of the built-in rectangle in the graph or point to the rectangle. Let's take a look at the other two functions:
Function prototype: setjavaswrgn (
Hwnd, // handle of the current form
Hrgn, // handle of the current region
BOOL bRedraw, // redraw the flag
)
Function: This function passes the created region handle as a parameter to the current form handle, and draws the form within the region range;
Function prototype: CombineRgn (
HRGN hrgnDest, // The handle connecting the destination region
HRGN hrgnSrc1, // handle of the first connection Source Region
HRGN hrgnSrc2, // handle of the second connection Source Region
Int fnCombineMode // connection mode
)
Function: This function combines two areas into a new area. The connection mode can be set to the following values:
Parameter Value
Rgn_and creates a new region from the intersection of Region 1 and Region 2.
Rgn_copy creates a copy of Region 1 as the new region
The rgn_diff connection belongs to Region 1 but does not belong to Region 2.
Rgn_or connects all the parts of Area 1 and Area 2 to the Union.
Rgn_xor. Connect all parts of Area 1 and Area 2 and remove the common parts.
2. hollow form
This kind of form is characterized by a form on the good end, which is partially dug in the middle. For example, add the following code to the unit file of Delphi:
Procedure tform1.formcreate (Sender: tobject );
VaR
FRegion3: THandle;
Begin
Canvas. Font. Name: = 文; // set the Font
Canvas. Font. Size: = 100; // set the Font Size.
BeginPath (Canvas. Handle); // obtain the outline drawn on vanvas.
TextOut (form1.Canvas. Handle, program spring and autumn, 8); // drop the area occupied by the word "program Spring and Autumn"
EndPath (Canvas. Handle );
FRegion3: = PathToRegion (Canvas. Handle); // assign the preceding area to the form.
Setjavaswrgn (Handle, FRegion3, True); // start painting
End;
Three API functions are used here:
Function prototype: BOOL BeginPath (HDC hdc // handle of the device environment)
Function: starts to receive path tracks of the current device environment;
Function prototype: BOOL EndPath (HDC hdc // handle of the device environment)
Function: stop receiving and assign the received Path to the handle of the current device environment.
Function prototype BOOL TextOut (
HDC hdc, // handle of device context
Int nXStart, // X coordinate of the start position
Int nYStart, // Y coordinate of the start position
Lptstr lpString, // string address
Int cbString // The number of characters contained in the string (note that one Chinese Character occupies two characters)
)
Function: draws a given string at a specified position.
Conclusion: It is an important skill to learn to use APIs for programming, and sometimes it can achieve unexpected results. The above skills are intended to inspire others. We hope that you can make full use of all the skills, give full play to your imagination, design a more dazzling form, and beautify your programs.

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.