XE 画矩形实现拖拉改变大小(属性)

实现原理:

1.拖一个rectangle控件,Align属性为TOP

2.拖一个Split控件,Align为Top

3.拖Rectangle,Align为Bottom

4.拖Split,Align为Bottom

5.左边、右边依次类推,

6.最后拖一个Rectangle,Align为Client,Stroke属性中的Color为red。

具体代码:

unit UnitCheckIPN;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
  FMX.Objects, FMX.Controls.Presentation;

type
  TForm2 = class(TForm)
    RT: TRectangle;  //RectTop
    ST: TSplitter;
    RL: TRectangle;
    SL: TSplitter;
    RB: TRectangle;
    SB: TSplitter;
    RR: TRectangle;
    SR: TSplitter;
    Rect: TRectangle;
    Image1: TImage;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
    procedure SetRect(s:String);
    function GetRC: String;                   // 获取   获取当前矩形的位置
    procedure SetRC(const Value: String);     // 设置   给定x,y,w,h   矩形自动改变位置
  public
    { Public declarations }
    property RC: String read GetRC write SetRC;  // 将矩形的值设为属性,读为获取rect的(x,y,width, height)   写时为设置矩形的x,y,w,h
  end;

var
  Form2: TForm2;

implementation

{$R *.fmx}

procedure TForm2.Button1Click(Sender: TObject);
begin
  RC:='103,128,301,203';      //
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
  ShowMessage(RC);            //
end;

procedure TForm2.Button3Click(Sender: TObject);
begin
  RC:='103,128,394,203';
end;

function TForm2.GetRC: String;
var
  x,y,w,h : Single;
begin
  result:='';
  y:=RT.Height+ST.Height;
  h:=Image1.Height - RT.Height - ST.Height - RB.Height - SB.Height;

  x:=RL.Width+SL.Width;
  w:=Image1.Width-RL.Width-SL.Width-RR.Width-SR.Width;

  result:=x.ToString()+','+y.ToString()+','+w.ToString()+','+h.ToString();
end;

procedure TForm2.SetRC(const Value: String);
var
  ss: TStringList;
  x,y,w,h: Integer;
begin
  ss:=TStringList.Create;
  ss.CommaText:=Value;
  x:=ss[0].ToInteger();
  y:=ss[1].ToInteger();
  w:=ss[2].ToInteger();
  h:=ss[3].ToInteger();
  ss.Free;

  RT.Height:=y-ST.Height;
  RB.Height:=Image1.Height-(RT.Height + ST.Height + h + SB.Height);

  RL.Width:=x-SL.Width;
  RR.Width:=Image1.Width-(RL.Width+SL.Width+w+SR.Width);
end;

procedure TForm2.SetRect(s: String);
var
  ss: TStringList;
  x,y,w,h: Integer;
begin
  ss:=TStringList.Create;
  ss.CommaText:=s;
  x:=ss[0].ToInteger();
  y:=ss[1].ToInteger();
  w:=ss[2].ToInteger();
  h:=ss[3].ToInteger();
  ss.Free;

  RT.Height:=y-ST.Height;
  RB.Height:=Image1.Height-(RT.Height + ST.Height + h + SB.Height);

  RL.Width:=x-SL.Width;
  RR.Width:=Image1.Width-(RL.Width+SL.Width+w+SR.Width);
end;

end.

 这里充分理解了属性,见证了属性的强大,好好学习,再接再厉。

 

posted @ 2015-11-02 16:15  王云盼  阅读(299)  评论(0编辑  收藏  举报