加入收藏  广告服务  关于我们
 2008-5-13

Delphi利用SVGActiveX 3.0 Type Library监听事件的例子

发表:sgnah   阅读:次  关键字:Delphi 利用 SVGActiveX 3.0 Type Library监听事件的例子   字体:[ ]

Delphi利用SVGActiveX 3.0 Type Library监听事件的例子
Example showing how to receive events in Delphi from Adobe ActiveX viewer


Delphi利用Adobe ActiveX viewer 监听事件的例子


最近学习Delphi+svg,找到这个例子,我研究了一下,发现特别好!
该例子演示了动态生成SVG图形元素的方法,并能加入事件驱动。
可惜不知道如何保存动态生成的SVG图形。

---------------------------------------
unit TestUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
   Forms, Dialogs, ActiveX, SVGACTIVEXLib_TLB, StdCtrls, OleCtrls, SvgEXT;

type
 TForm1 = class(TForm)
   SVGCtl1 TSVGCtl;
   Debug TMemo;
    Button1 TButton;
    Button2 TButton;
    Label1 TLabel;
    procedure FormCreate(Sender TObject);
    procedure Button1Click(Sender TObject);
    procedure Button2Click(Sender TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

 var
  Form1 TForm1;

implementation
 {$R .dfm}


procedure TForm1.FormCreate(Sender TObject);
begin
    Form1.SVGCtl1.setSrc(GetCurrentDir+'test.svg');
end;

procedure TForm1.Button1Click(Sender TObject);
begin
  Application.Terminate;
end;

procedure TForm1.Button2Click(Sender TObject);
 var
   ReadyState integer;
   rcDisp IDispatch;
   SVGDocument TSVGDocument;
   SVGRoot, SVGElement, rc TSVGDocument;
   txtDisp,txtnodeDisp IDispatch;
   txt TSVGDocument;
   imgDisp IDispatch;
   img TSVGDocument;
   EventListener TEventListener;
 const NamespaceURI = 'httpwww.w3.org2000xlinknamespace';
 begin
  ReadyState=SVGCtl1.ReadyState;
  if ReadyState4 then
    Form1.Debug.Lines.Add('SVG viewer not ready')
  else
  begin
     Form1.Debug.Lines.Add('Adding another click event to 1st rectangle');
     Form1.Debug.Lines.Add('  creating a 2nd rectangle with click event,');
     Form1.Debug.Lines.Add('  a piece of text and and image');
     getMem(disparm.rgvarg,4SizeOf(TVariantArg));  allow for max of four args
     SVGDocument=TSVGDocument.Create(SVGCtl1.getSVGDocument);

        add a click event to the rectangle from test.svg
     SVGElement=TSVGDocument.Create(SVGDocument.getElementById('OldRect'));
     EventListener=TEventListener.Create;
     SVGElement.addEventListener('click',EventListener, false);
     SVGRoot=TSVGDocument.Create(SVGDocument.getRootElement);

        define a new rectangle and put click event on it
     rcDisp=SVGDocument.createElement('rect');
     rc=TSVGDocument.Create(rcDisp);
     rc.setAttribute('id', 'NewElement');
     rc.setAttribute('x','50');
     rc.setAttribute('y','50');
     rc.setAttribute('height','10');
     rc.setAttribute('width','20');
     rc.setAttribute('style','fillblue; fill-opacity0.2; strokeblack');
     rc.addEventListener('click',EventListener, false);
     SVGRoot.appendChild(rcDisp);

        define a new piece of text
     txtDisp=SVGDocument.createElement('text');
     txt=TSVGDocument.Create(txtDisp);
     txt.setAttribute('id', 'TextElement');
     txt.setAttribute('x','70');
     txt.setAttribute('y','80');
     txt.setAttribute('style','font-familyVerdana;font-size15;strokered;fillred');
     txtnodeDisp=SVGDocument.createTextNode('Howdy!');
     txt.appendChild(txtnodeDisp);
     SVGRoot.appendChild(txtDisp);

        define an image
     imgDisp=SVGDocument.createElement('image');
     img=TSVGDocument.Create(imgDisp);
     img.setAttributeNS(NamespaceURI,'xlinkhref', 'hello.png');
     img.setAttribute('height','36');
     img.setAttribute('width','111');
     img.setAttribute('y','100');
     SVGRoot.appendChild(imgDisp);

     FreeMem(disparm.rgvarg);
     rc.Free;
     txt.Free;
     img.Free;
     SVGRoot.Free;
     SVGElement.Free;
     SVGDocument.Free;
     Button2.Visible=false;
     Label1.Visible=true;
  end;
end;

end.

---------------------------------------
unit TestUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
   Forms, Dialogs, ActiveX, SVGACTIVEXLib_TLB, StdCtrls, OleCtrls, SvgEXT;

type
 TForm1 = class(TForm)
   SVGCtl1 TSVGCtl;
   Debug TMemo;
    Button1 TButton;
    Button2 TButton;
    Label1 TLabel;
    procedure FormCreate(Sender TObject);
    procedure Button1Click(Sender TObject);
    procedure Button2Click(Sender TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

 var
  Form1 TForm1;

implementation
 {$R .dfm}


procedure TForm1.FormCreate(Sender TObject);
begin
    Form1.SVGCtl1.setSrc(GetCurrentDir+'test.svg');
end;

procedure TForm1.Button1Click(Sender TObject);
begin
  Application.Terminate;
end;

procedure TForm1.Button2Click(Sender TObject);
 var
   ReadyState integer;
   rcDisp IDispatch;
   SVGDocument TSVGDocument;
   SVGRoot, SVGElement, rc TSVGDocument;
   txtDisp,txtnodeDisp IDispatch;
   txt TSVGDocument;
   imgDisp IDispatch;
   img TSVGDocument;
   EventListener TEventListener;
 const NamespaceURI = 'httpwww.w3.org2000xlinknamespace';
 begin
  ReadyState=SVGCtl1.ReadyState;
  if ReadyState4 then
    Form1.Debug.Lines.Add('SVG viewer not ready')
  else
  begin
     Form1.Debug.Lines.Add('Adding another click event to 1st rectangle');
     Form1.Debug.Lines.Add('  creating a 2nd rectangle with click event,');
     Form1.Debug.Lines.Add('  a piece of text and and image');
     getMem(disparm.rgvarg,4SizeOf(TVariantArg));  allow for max of four args
     SVGDocument=TSVGDocument.Create(SVGCtl1.getSVGDocument);

        add a click event to the rectangle from test.svg
     SVGElement=TSVGDocument.Create(SVGDocument.getElementById('OldRect'));
     EventListener=TEventListener.Create;
     SVGElement.addEventListener('click',EventListener, false);
     SVGRoot=TSVGDocument.Create(SVGDocument.getRootElement);

        define a new rectangle and put click event on it
     rcDisp=SVGDocument.createElement('rect');
     rc=TSVGDocument.Create(rcDisp);
     rc.setAttribute('id', 'NewElement');
     rc.setAttribute('x','50');
     rc.setAttribute('y','50');
     rc.setAttribute('height','10');
     rc.setAttribute('width','20');
     rc.setAttribute('style','fillblue; fill-opacity0.2; strokeblack');
     rc.addEventListener('click',EventListener, false);
     SVGRoot.appendChild(rcDisp);

        define a new piece of text
     txtDisp=SVGDocument.createElement('text');
     txt=TSVGDocument.Create(txtDisp);
     txt.setAttribute('id', 'TextElement');
     txt.setAttribute('x','70');
     txt.setAttribute('y','80');
     txt.setAttribute('style','font-familyVerdana;font-size15;strokered;fillred');
     txtnodeDisp=SVGDocument.createTextNode('Howdy!');
     txt.appendChild(txtnodeDisp);
     SVGRoot.appendChild(txtDisp);

        define an image
     imgDisp=SVGDocument.createElement('image');
     img=TSVGDocument.Create(imgDisp);
     img.setAttributeNS(NamespaceURI,'xlinkhref', 'hello.png');
     img.setAttribute('height','36');
     img.setAttribute('width','111');
     img.setAttribute('y','100');
     SVGRoot.appendChild(imgDisp);

     FreeMem(disparm.rgvarg);
     rc.Free;
     txt.Free;
     img.Free;
     SVGRoot.Free;
     SVGElement.Free;
     SVGDocument.Free;
     Button2.Visible=false;
     Label1.Visible=true;
  end;
end;

end.

 

 

 热门文章
 推荐信息