《GOF设计模式》—组合(COMPOSITE)—Delphi源码示例:绘图编辑器

示例:绘图编辑器

说明:

代码:

unit uGraphic;

 

interface

 

uses

    Windows,SysUtils,Classes,Graphics,Types;

 

type

    TGraphic = class

    protected

        FCanvas: TCanvas;

        function GetChilds(Index: integer): TGraphic; virtual;

        function GetCount: Integer; virtual;

    public

        constructor Create(ACanvas: TCanvas);

        //---

        procedure Draw; virtual; abstract;

        procedure Add(AGraphic: TGraphic); virtual;

        procedure Remove(AGraphic: TGraphic); virtual;

        //---

        property Childs[Index: integer]: TGraphic read GetChilds;

        property Count: Integer read GetCount;

    end;

    TPicture = class(TGraphic)

    private

        FList: TList;

        procedure Clear;

    protected

        function GetChilds(Index: integer): TGraphic; override;

        function GetCount: Integer; override;

    public

        constructor Create(ACanvas: TCanvas);

        destructor Destroy; override;

        //---

        procedure Draw; override;

        procedure Add(AGraphic: TGraphic); override;

        procedure Remove(AGraphic: TGraphic); override;

    end;

    TLine = class(TGraphic)

    public

        procedure Draw; override;

    end;

    TRectangle = class(TGraphic)

    public

        procedure Draw; override;

    end;

    TText = class(TGraphic)

    public

        procedure Draw; override;

    end;

 

implementation

 

constructor TGraphic.Create(ACanvas: TCanvas);

begin

    FCanvas := ACanvas;

end;

 

procedure TGraphic.Add(AGraphic: TGraphic);

begin

    raise Exception.Create('不支持该方法');

end;

 

procedure TGraphic.Remove(AGraphic: TGraphic);

begin

    raise Exception.Create('不支持该方法');

end;

 

function TGraphic.GetChilds(Index: integer): TGraphic;

begin

    Result := nil;

end;

 

function TGraphic.GetCount: Integer;

begin

    Result := 0;

end;

 

procedure TPicture.Clear;

var

    i: Integer;

begin

    with FList do

    begin

        for i := 0 to Count - 1 do

            TObject(Items[i]).Free;

        //---

        Clear;

    end;

end;

 

constructor TPicture.Create(ACanvas: TCanvas);

begin

    inherited;

    //---

    FList := TList.Create;

end;

 

destructor TPicture.Destroy;

begin

    self.Clear;

    FList.Free;

    //---

    inherited;

end;

 

procedure TPicture.Add(AGraphic: TGraphic);

begin

    FList.Add(AGraphic);

end;

 

function TPicture.GetChilds(Index: integer): TGraphic;

begin

    Result := FList[Index];

end;

 

function TPicture.GetCount: Integer;

begin

    Result := FList.Count;

end;

 

procedure TPicture.Remove(AGraphic: TGraphic);

begin

    FList.Remove(AGraphic)

end;

 

procedure TPicture.Draw;

var

    i: integer;

begin

    for i := 0 to self.Count - 1 do

        self.Childs[i].Draw;

end;

 

procedure TLine.Draw;

begin

    with FCanvas do

    begin

        with Pen do

        begin

            Color := clRed;

            Style := psSolid;

            Width := 2;

        end;

        //---

        MoveTo(20,20);

        LineTo(60,60);

    end;

end;

 

procedure TRectangle.Draw;

begin

    with FCanvas do

    begin

        with Pen do

        begin

            Color := clRed;

            Style := psSolid;

            Width := 2;

        end;

        //---

        Rectangle(20,20,80,80);

    end;

end;

 

procedure TText.Draw;

begin

    with FCanvas do

    begin

        Font.Color := clred;

        //---

        TextOut(20,30, '123');

    end;

end;

 

end.

 

procedure TForm1.Button1Click(Sender: TObject);

    //---

    function _GetGraphic1:TGraphic;

    begin

        Result := TLine.Create(self.Canvas);

    end;

    //---

    function _GetGraphic2:TGraphic;

    begin

        Result := TPicture.Create(self.Canvas);

        with Result do

        begin

            Add(TRectangle.Create(self.Canvas));

            Add(TText.Create(self.Canvas));

        end;

    end;

var

    AGraphic:TGraphic;

begin

    AGraphic := _GetGraphic2;

    try

        AGraphic.Draw;

    finally

        AGraphic.Free;

    end;

end;

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TVideoGrabber Video SDK v10.8 for C#, VB.NET, VC++, Delphi and C++Builder (all versions), and ActiveX compatible tools (PowerBuilder, LabView,...) ©2018 Datastead Software Home page: http://www.datastead.com/products/tvideograbber.html Description CAPTURE AND PREVIEW OF REALTIME VIDEO SOURCES The TVideoGrabber SDK supports most of the video capture devices: - IP cameras in RTSP / ONVIF mode with PTZ support (*) - capture of live streaming URLs through RTSP, RTMP, UDP, RTP, SDP, HTTP, MMS (*) - USB webcams, - PCIe capture/rendering cards (e.g. BlackMagic Decklink) - capture cards including a H264 or MPEG hardware encoder, e.g. AverMedia Live Gamer Portable, - analog composite USB capture devices (e.g. Easycap) - PCIe analog composite capture cards (e.g. OsPrey 210, OsPrey 440) - GigE cameras (e.g. PointGrey) (*) requires our optional Datastead RTSP/RTMP/HTTP/ONVIF DirectShow Source Filter VIDEO RECORDING - audio/video recording to various formats after installing our optional Datastead Multipurpose DirectShow Encoder filter, Most of the recording formats include MP4, MOV, FLV, AVI, Ogg/Theora, WebM, etc... - audio/video recording with Legacy DirectShow compressors - AVI recording, - WMV/ASF recording, - MKV recording after installing the Haali MKV muxer, - MP4/FLV recording (may require a third-party H264 or AAC DirectShow encoder) Timer-based recording (delayed start, timered stop, or create new file periodically) Compression of the audio and video streams, "on-the-fly" or after recording, Pause/resume during recording, with possibility to create a new clip for each pause/resume, Preview while recording.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值