| Back to logs list
136990 2009 年 07 月 26 日 08:57 Reading (loading. ..) Comments (1) Category: DELPHI program
achieve Shaped FORM not a difficult task, this article will show you how to use API functions to achieve rounded rectangle and ellipse FORM,
new balance 574, and on this basis of the descendants of the class to achieve TWINcontrol shaped implementation.
To change the shape of
FORM,
new balance outlet, which is the realization of regional (region) of the control. Program in the Win32 API reference manual on regional (region) is defined as described: it can be a rectangle, polygon, oval (or both combined,
Marriage Made In Heaven, or more shapes), which can be filled drawing, flip, structured and can be the focus of implementation.
concluded that by definition: regional (region) can be changed and manipulated according to our needs define our region and create the desired shape.
should be noted that the regional (region) but also the descendants of any TWINcontrol class definition and control (not just FORMS), that is, to the regional (region) of the definition applied to the Tpanel or TEdit such objects. Descendants of the class in changing TWINcontrol shape control, the need to provide a handle and create some changes in the shape of the function.
concrete implementation is generally divided into two steps:
1. Define the shape of the area required for the boundary shape (such as: oval-shaped).
2. The shape of the defined regional boundaries applied to the window.
Here, we will call the Windows API function to complete these two steps, following the application of the specific function be described:
achieve the first step: the definition of regional boundaries.
here will call the three WinAPI, these three functions are:
CreateEllipticRgn () function is generated ellipse;
CreateRoundRectRgn () function is to generate rounded rectangle;
CreatePolygonRgn () function is to generate polygon region, Windows automatically to ensure that its vertices connected to form a closed area.
these three functions identified by the returned pointer variable regions generated by the second step will be applied. These functions in the function declaration in Delphi and parameters described as follows:
(1) ellipse generating function:
function prototype: HRGN CreateEllipticRgn (int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
meaning of the parameters:
nLeftRect, nTopRect: the upper-left coordinates;
nRightRect, nBottomRect: lower right corner of the region coordinates;
(2) rounded rectangular area generating function:
function prototype: HRGN CreateRoundRectRgn (int nLeftRect,
BMW ad - Qzone log, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
meaning of the parameters:
nLeftRect, nTopRect: the upper-left coordinates;
nRightRect,
new balance running shoes, nBottomRect: lower right corner of the region coordinates;
nWidthEllipse, nHeightEllipse: fillet width and height;
(3) polygon area generating function:
function prototype: HRGN CreatePolygonRgn (CONST POINT * lppt, int cPoints, int fnPolyFillMode);
meaning of the parameters:
Lppt: point to a POINT type of array defined polygon vertices;
CPoints: definition of the array of vertices;
FnPolyFillMode: define fill patterns, optional value ALTERNATE or WINDING.
to achieve the second step: the return of the HRGN types of regional value is set to the window area function call.
settings window area function:
function prototype: int SetWindowRgn (HWND hWnd, HRGN hRgn, BOOL bRedraw);
Parameter Description:
hWnd: pointing to the operating handle to the window;
hRgn: The handle to the region;
bRedraw: whether to display the window redraw flag.
the end of each function needs to call SetWindowRgn function, and then by the Windows operating system, the various forms of regional and display settings.
FORM
following will test the entire source code listed in the FORM added four buttons on the control implementation: oval, rounded rectangle, equilateral polygon and star; a Tpanel control descendants of the class to demonstrate TWINcontrol definition and control of the region; a SpinEdit control polygon and star of the vertex defined number of connections.
source:
unit form_statue;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Spin;
type
TForm1 = class (TForm)
Button1: TButton;
SpinEdit1: TSpinEdit;
Panel1: TPanel;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure DrawRndRectRegion (wnd: HWND; rect: TRect);
procedure DrawEllipticRegion (wnd: HWND; rect: TRect);
procedure DrawPolygonRegion (wnd: HWND; rect: TRect; NumPoints: Integer; DoStarShape: Boolean);
procedure Button1Click (Sender: TObject);
procedure Button4Click (Sender: TObject);
procedure Button3Click (Sender: TObject);
procedure Button2Click (Sender: TObject);
private
{Private declarations}
rgn: HRGN;
rect: TRect;
public
{Public declarations}
end;
var
Form1: TForm1;
implementation
{$ R *. DFM}
procedure TForm1.DrawRndRectRegion (wnd: HWND; rect: TRect);
begin
rgn: = CreateRoundRectRgn (rect.left, rect.top, rect.right, rect.bottom, 30, 30);
SetWindowRgn (wnd, rgn, TRUE);
end;
procedure TForm1.DrawEllipticRegion (wnd: HWND; rect: TRect);
begin
rgn: = CreateEllipticRgn (rect.left, rect.top, rect.right, rect.bottom);
SetWindowRgn (wnd,
new balance shoes, rgn, TRUE);
end;
procedure TForm1.DrawPolygonRegion (wnd: HWND; rect: TRect; NumPoints: Integer; DoStarShape: Boolean);
const
RadConvert = PI/180;
Degrees = 360;
MaxLines = 100;
var
x, y,
Humor jokes - Qzone log,
xCenter,
yCenter,
radius,
new new balance shoes,
pts,
I: Integer;
angle,
rotation: Extended;
arPts: Array [0 .. MaxLines] of TPoint;
begin
xCenter: = (rect.Right - rect.Left) div 2;
yCenter: = (rect.Bottom - rect.Top) div 2;
if DoStarShape then
begin
rotation: = Degrees / (2 * NumPoints);
pts: = 2 * NumPoints;
end
else
begin
rotation: = Degrees / NumPoints; / / get the degree of each vertex
pts: = NumPoints;
end;
radius: = yCenter;
for I: = 0 to pts - 1 do begin
if DoStarShape then
if (I mod 2) = 0 then
radius: = Round (radius / 2)
else
radius: = yCenter;
angle: = ((I * rotation) + 90) * RadConvert;
x: = xCenter + Round (cos (angle) * radius);
y: = yCenter - Round (sin (angle) * radius);
arPts.X: = x;
arPts.Y: = y;
end;
rgn: = CreatePolygonRgn (arPts, pts, WINDING);
SetWindowRgn (wnd, rgn, TRUE);
end;
procedure TForm1.Button1Click (Sender: TObject);
begin
DrawRndRectRegion (Form1.Handle, Form1.ClientRect);
end;
procedure TForm1.Button4Click (Sender: TObject);
begin
DrawPolygonRegion (Panel1.Handle, Panel1.BoundsRect, SpinEdit1.Value, True);
end;
procedure TForm1.Button3Click (Sender: TObject);
begin
DrawEllipticRegion (Form1.Handle, Form1.ClientRect);
end;
procedure TForm1.Button2Click (Sender: TObject);
begin
DrawPolygonRegion (Panel1.Handle,
new balance mens, Panel1.BoundsRect, SpinEdit1.Value, False);
end;
end.