Use API functions to implement "atypical" forms in DELPHI
In order to attract users, some current shareware software, especially some multimedia player software, pays great attention to the design of the program interface. In fact, we can also use API functions to achieve those beautiful and alternative effects.
1. Special-shaped form
Can the form only be square? No, there are other shapes possible. This requires the use of two Win32 API functions. First, use the CreateRoundRectRgn() function to define an elliptical area within the form. The region referred to here is a special API object. We can perform operations such as filling and clipping inside the region to define the external characteristics of the form. Then call the SetWindowRgn() function to draw. Furthermore, you can use the CombineRgn() function to merge multiple areas. For example, add the following code to the Delphi unit file:
PRocedure TForm1.FormCreate(Sender: TObject);
var
FRegion1:THandle;
FRegion2:THandle;
begin
FRegion1:=CreateRoundRectRgn(20,20,200,200,300,300);//Delimit an elliptical area
FRegion2:=CreateRectRgn(170,170,400,400);//Delimit a rectangular area
CombineRgn(FRegion1,FRegion1,FRegion2,RGN_OR);//Connect two areas
SetwindowRgn(handle,FRegion1,True);//Draw the connected area
end;
The CreateRoundRectRgn() function in the program segment is used to create a rounded rectangular area, and its prototype is:
HRGN CreateRoundRectRgn(
int nLeftRect,//X coordinate of the upper left corner
int nTopRect,//Y coordinate of the upper left corner
int nRightRect,//X coordinate of the lower right corner
int nBottomRect,//Y coordinate of the lower right corner
int nWidthEllipse,//The width of the ellipse where the rounded corner is located
int nHeightEllipse //Height of the ellipse where the rounded corner is located
);
Other graphics such as polygons, ellipses, etc. have corresponding API functions. Their prototypes are 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 function are easy to understand and are used to indicate the coordinates of the built-in rectangle of the graphic or point to the rectangle. Let’s focus on the other two functions:
Function prototype: SetWindowRgn(
HWND hWnd, //handle of the current form
HRGN hRgn, //handle of the current region
BOOL bRedraw, //Redraw logo
)
Function: This function passes the handle of the created area as a parameter to the handle of the current form, and draws the form within the area;
Function prototype: CombineRgn(
HRGN hrgnDest, // handle to connect to the destination area
HRGN hrgnSrc1, //The first handle to connect to the source area
HRGN hrgnSrc2, // The handle of the second connection source area
int fnCombineMode // connection mode
)
Function: This function merges two areas into a new area, where the connection mode can take the following values:
Parameter value effect
RGN_AND creates a new region from the intersection of the common parts of region 1 and region 2
RGN_COPY creates a copy of region 1 as a new region
RGN_DIFF connects the parts that belong to area 1 but not area 2 to a new area
RGN_OR connects all parts of area 1 and area 2, that is, the union
RGN_XOR. Connect all parts of area 1 and area 2 and remove the common parts
2. Hollow form
The characteristic of this form is that it is a form with two ends, and a part is dug out in the middle. For example, add the following code to the Delphi unit file:
procedure TForm1.FormCreate(Sender: TObject);
var
FRegion3:THandle;
begin
Canvas.Font.Name:='Chinese Xingkai';//Set the font
Canvas.Font.Size:=100;//Set the font size
BeginPath(Canvas.Handle); //Get the outline drawn on vanvas
TextOut(form1.Canvas.Handle,0,20,'Program Spring and Autumn',8);//Cut out the area occupied by the four words "Program Spring and Autumn"
EndPath(Canvas.Handle);
FRegion3:=PathToRegion(Canvas.Handle);//Assign the above region to the form
SetwindowRgn(Handle,FRegion3,True);//Start drawing
end;
Three API functions are mainly used here:
Function prototype: BOOL BeginPath (HDC hdc //handle of the device environment)
Function: Start receiving the path trajectory of the current device environment;
Function prototype: BOOL EndPath(HDC hdc //handle of the device environment)
Function: Stop receiving and assign the received path trajectory to the handle of the current device environment
Function prototype BOOL TextOut(
HDC hdc,//handle of device context
int nXStart,//X coordinate of the starting position
int nYStart,//Y coordinate of the starting position
LPCTSTR lpString,//String address
int cbString //The number of characters contained in the string (note that one Chinese character occupies two characters)
)
Function: Draw the given string at the specified position.
Summary: Learning to use APIs for programming proficiently is a very important skill, and sometimes it can achieve unexpected results. The above techniques are intended to inspire others. I hope you can make full use of various techniques and use your imagination to design more dazzling forms and beautify your own programs.