知识大全 创建除三角形外的各种形状窗体

Posted

篇首语:不怕百事不利,就怕灰心丧气。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 创建除三角形外的各种形状窗体相关的知识,希望对你有一定的参考价值。

  Is it possible to create forms with shapes other than the standard rectangular shape in Windows?   Sometimes it s just not enough to write applications that have the same boring rectangular forms over and over again Sometimes you need a change How about an elliptical form? Or maybe even a triangular form? Sound intriguing? It s not that hard to do

  New in Win is something called a region The Win API Programmer s Reference defines a region as follows:

   a rectangle polygon or ellipse (or a bination of o or more of these shapes) that can be filled painted inverted framed and used to perform hit testing (testing for the cursor location)

  From the definition the most notable thing about a region is that it can be manipulated in a variety of ways For our purposes we want to define a region to create a specific shape

  I should point out that a region can be defined for just about any inControl descendant (not just forms) meaning you can apply a region to a TPanel or even a TEdit (though I strongly remend against it) But to alter the shape of a inControl descendant all you need to provide is a handle and employ some handy dandy shape change functions

  To get a control to change its shape follow this o step process:

  * Define the boundaries of the region that represent a particular shape   * Apply the boundaries you ve defined to a window   This is pretty simple However it s very important to refer to the help file and to have the source at hand I wouldn t be able to acplish many of my projects let alone write many of the articles I write here without those o resources at my disposal Especially with the Windows API calls having access to the Window PAS file is essential so I know what to pass into the functions Remember the WinAPI calls are really wrapper calls into the appropriate Windows DLLs and of course the help file is essential to getting background information on the topic you re interested in   With respect to this article look up the SetWindowRgn topic in Win Developer s Help and have it handy while you re putting together your program Pay particular attention to the Group hyperlink because it will give you a run down of all the procedures related to the region topic Let s move on!   Defining a Region s Boundary  The first step to creating a form of a different shape is to define the shape itself For our discussion we ll use three WinAPI calls:

  * CreateEllipticRgn   This function will create an elliptically shaped region   * CreateRoundRectRgn   This will create a rectangular region with rounded corners   * CreatePolygonRgn   This will create just about any multi sided shape as long as the lines form a closed solid

  These functions return a HRGN type which will then be used by a function called SetWindowRgn whose sole purpose in life it is to set the parameters defined by a particular region variable I ve encapsulated these functions in methods that are part of a demonstration form The functions are coded as follows:   Notice that all the functions are used in an assignment   operation to a variable called rgn This is a   private var that I declared for the form The private var is   accessible to all functions; I did this so that I could change the shape of   the form or a control on the form and use the same region

procedure TForm DrawEllipticRegion(wnd : HWND; rect : TRect);begin  rgn := CreateEllipticRgn(rect left rect top rect right rect bottom);  SetWindowRgn(wnd rgn TRUE);end;

procedure TForm DrawRndRectRegion(wnd : HWND; rect : TRect);begin  rgn := CreateRoundRectRgn(rect left rect top rect right rect bottom );  SetWindowRgn(wnd rgn TRUE);end;

procedure TForm DrawPolygonRegion(wnd : HWND; rect : TRect; NumPoints : Integer; DoStarShape : Boolean);const  RadConvert = PI/ ;  Degrees    = ;  MaxLines   = ;var  x y   xCenter   yCenter   radius   pts   I       : Integer;  angle   rotation: Extended;  arPts   : Array[ MaxLines] of TPoint;begin

  xCenter := (rect Right rect Left) div ;  yCenter := (rect Bottom rect Top) div ;  if DoStarShape then    begin      rotation := Degrees/( *NumPoints);      pts := * NumPoints;    end  else    begin      rotation := Degrees/NumPoints;             //get number of degrees to turn per point      pts := NumPoints    end;  radius := yCenter;

This loop defines the Cartesian points of the shape Notice I ve added degrees to the rotation angle This is so that shapes will stand up; otherwise they ll lie on their sides I had to brush up on my trigonometry to acplish this (fot all those sin and cos thingies) Many thanks to Terry Smithwick and David Ullrich for their assistance on CompuServe!   for I := to pts do begin    if DoStarShape then      if (I mod ) = then //which means that        radius := Round(radius/ )      else        radius := yCenter;

    angle := ((I * rotation) + ) * RadConvert;    x := xCenter + Round(cos(angle) * radius);    y := yCenter Round(sin(angle) * radius);    arPts[I] X := x;    arPts[I] Y := y;  end;

  rgn := CreatePolygonRgn(arPts pts WINDING);  SetWindowRgn(wnd rgn TRUE);end;

  The first o functions are pretty simple just o liners All that s needed to create the appropriate shapes is a handle and a TRect structure For forms that structure would be taken from the ClientRect property; for other controls use the BoundsRect property

  The DrawPolygonRegion method however is much more plex This is due in part to the fact that CreatePolygonRgn requires the vertices of the corners of the polygon to be passed as an array of TPoints and partly because I wanted to draw equilateral polygons based off points rotated around a mon center point For that I had to use some trigonometry

  I wanted to not only draw polygon regions but stars as well Using rotational trig allowed me to do it The way the function works if the DrawStarShape parameter is set to True is that for every even value of I in the loop the radius of the circle is set to half its length and to maintain the number of points of the polygon I want to draw I double the number of points to acodate the contraction of the radius

  At the very end of each function is a call to SetWindowRgn This function takes as parameters a window handle a rgn var and a Boolean value that specifies whether the window should be re drawn In all cases if you want to see the shape you ve made this must be always be set to True

  Below is the listing for the entire source code of my test form On the form I ve dropped four TButtons (one for each of the shapes: ellipse round rectangle polygon and star); a TPanel to demonstrate the ability to set regions for inControl descendants other than TForm; and a SpinEdit used in conjunction with the Polygon and Star region buttons to define the number of points that ll be defining the shape Here s the code: unit regmain;

interface

uses  Windows Messages SysUtils Classes Graphics Controls Forms Dialogs   ExtCtrls StdCtrls Spin;

type  TForm = class(TForm)    Button : TButton;    Button : TButton;    Button : TButton;    SpinEdit : TSpinEdit;    Button : TButton;    Panel : TPanel;    Edit : TEdit;    procedure DrawRndRectRegion(wnd : HWND; rect : TRect);    procedure DrawEllipticRegion(wnd : HWND; rect : TRect);    procedure DrawPolygonRegion(wnd : HWND; rect : TRect; NumPoints : Integer; DoStarShape : Boolean);    procedure Button Click(Sender: TObject);    procedure Button Click(Sender: TObject);    procedure Button Click(Sender: TObject);    procedure Button Click(Sender: TObject);  private    Private declarations     rgn : HRGN;    rect : TRect;  public    Public declarations   end;

var  Form : TForm ;

implementation

$R * DFM

procedure TForm DrawRndRectRegion(wnd : HWND; rect : TRect);begin  rgn := CreateRoundRectRgn(rect left rect top rect right rect bottom );  SetWindowRgn(wnd rgn TRUE);end;

procedure TForm DrawEllipticRegion(wnd : HWND; rect : TRect);begin  rgn := CreateEllipticRgn(rect left rect top rect right rect bottom);  SetWindowRgn(wnd rgn TRUE);end;

procedure TForm DrawPolygonRegion(wnd : HWND; rect : TRect; NumPoints : Integer; DoStarShape : Boolean);const  RadConvert = PI/ ;  Degrees    = ;  MaxLines   = ;var  x y   xCenter   yCenter   radius   pts   I       : Integer;  angle   rotation: Extended;  arPts   : Array[ MaxLines] of TPoint;begin

  xCenter := (rect Right rect Left) div ;  yCenter := (rect Bottom rect Top) div ;  if DoStarShape then    begin      rotation := Degrees/( *NumPoints);      pts := * NumPoints;    end  else    begin      rotation := Degrees/NumPoints;             //get number of degrees to turn per point      pts := NumPoints    end;  radius := yCenter;

This loop defines the Cartesian points of the shape Again I ve added degrees to the rotation angle so the shapes will stand up rather than lie on their sides Thanks again to Terry Smithwick and David Ullrich for their trig help on CompuServe   for I := to pts do begin    if DoStarShape then      if (I mod ) = then //which means that        radius := Round(radius/ )      else        radius := yCenter;

    angle := ((I * rotation) + ) * RadConvert;    x := xCenter + Round(cos(angle) * radius);    y := yCenter Round(sin(angle) * radius);    arPts[I] X := x;    arPts[I] Y := y;  end;

  rgn := CreatePolygonRgn(arPts pts WINDING);  SetWindowRgn(wnd rgn TRUE);end;

procedure TForm Button Click(Sender: TObject);begin  DrawEllipticRegion(Form Handle Form ClientRect);end;

procedure TForm Button Click(Sender: TObject);begin  DrawPolygonRegion(Panel Handle Panel BoundsRect SpinEdit Value False);end;

procedure TForm Button Click(Sender: TObject);begin  DrawRndRectRegion(Form Handle Form ClientRect);end;

procedure TForm Button Click(Sender: TObject);begin  DrawPolygonRegion(Panel Handle Panel BoundsRect SpinEdit Value True);end;

end

cha138/Article/program/Delphi/201311/25060

相关参考

知识大全 用VB.NET设计各种形状的窗体界面二

用VB.NET设计各种形状的窗体界面二  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!    结合上

知识大全 如图,两个形状大小相同的三角形,可以拼成各种不同的图形,下面已画出一个三角板, 请你分

如图,两个形状大小相同的三角形,可以拼成各种不同的图形,下面已画出一个三角板,请你分解:可以任一顶点为对称中心,图“略”。用两个大小及形状相同的直角三角形可以拼成多少种不同的图形?6种,等长边拼合,有

知识大全 现代智力玩具,有个三角形的底版,里边有个凹槽,里边有12个各种形状的塑料板,拼对着往里放

现代智力玩具,有个三角形的底版,里边有个凹槽,里边有12个各种形状的塑料板,拼对着往里放也是七巧板一个长方形的长是3宽是2,里边有一个三角形三角形的高是0.8求长方形减去三角形的面积跟长同底的话:3*

知识大全 我的左胳膊上长了三颗痣,并且组成了三角形的形状,这是代表什么意思啊

我的左胳膊上长了三颗痣,并且组成了三角形的形状,这是代表什么意思啊?综合来讲:三角形是稳定的代表,管辖在于权也,痣长在左臂上,男左女右,你是男的,则说明你以后的权力掌握在你手里,你是女的话则说明你的权

知识大全 经典案例 财务管理系统(11)[1]

  ()新建一个窗体将窗体的Name属性设置为DocuInput选择Project|Options菜单命令将DocuInp窗体从自动创建的窗体列表中删除将窗体保存为DocuInputFormpas该窗

除设备外的融资租赁是否适用以差额纳营业税?

现有营业税税目中关于融资租赁的规定是关于设备的,那么除设备外的融资租赁(例如不动产)是否适用金融业税目中的融资租赁以差额纳营业税呢?应该如何缴纳营业税?  根据《国家税务总局关于印发〈营业税税目注释〉

知识大全 经典案例 财务管理系统(14)[1]

  完成凭证输入功能  ()新建一个窗体将窗体的Name属性设置为DocuInput选择Project|Options菜单命令将DocuInp窗体从自动创建的窗体列表中删除将窗体保存为DocuInpu

阴道外的接触性皮炎各种禁忌

女性的外生殖器,也就是外阴,要是患有接触性皮炎,跟避孕套有关系,也有的是跟内裤等有关,原因是很多的。阴道外的接触性皮炎各种禁忌,是需要广大的女性,不仅仅是已经患有阴道外的接触性皮炎的患者,才需要提防的

阴道外的接触性皮炎各种禁忌

女性的外生殖器,也就是外阴,要是患有接触性皮炎,跟避孕套有关系,也有的是跟内裤等有关,原因是很多的。阴道外的接触性皮炎各种禁忌,是需要广大的女性,不仅仅是已经患有阴道外的接触性皮炎的患者,才需要提防的

知识大全 怎么查百度文库创建时间,就是这个文档什么时后上传的

怎么查百度文库创建时间,就是这个文档什么时后上传的点击右边的文档信息.然后就可以看到了.你可以找个来试下,文档信息旁边有个黑色三角形,你点击它就可以看到下面有个贡献时间.百度文库上传的文档什么时候能成