-- 作者:SCYANGYU
-- 发布时间:2/26/2005 6:40:00 PM
-- [推荐]Delphi利用Adobe ActiveX viewer 监听事件的例子
Example showing how to receive events in Delphi from Adobe ActiveX viewer Delphi利用Adobe ActiveX viewer 监听事件的例子 这个例子我研究了一下,发现特别好,值得推荐! 该例子演示了动态生成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 = 'http://www.w3.org/2000/xlink/namespace/'; begin ReadyState:=SVGCtl1.ReadyState; if ReadyState<>4 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,4*SizeOf(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','fill:blue; fill-opacity:0.2; stroke:black'); 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-family:Verdana;font-size:15;stroke:red;fill:red'); txtnodeDisp:=SVGDocument.createTextNode('Howdy!'); txt.appendChild(txtnodeDisp); SVGRoot.appendChild(txtDisp); // define an image imgDisp:=SVGDocument.createElement('image'); img:=TSVGDocument.Create(imgDisp); img.setAttributeNS(NamespaceURI,'xlink:href', '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 = 'http://www.w3.org/2000/xlink/namespace/'; begin ReadyState:=SVGCtl1.ReadyState; if ReadyState<>4 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,4*SizeOf(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','fill:blue; fill-opacity:0.2; stroke:black'); 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-family:Verdana;font-size:15;stroke:red;fill:red'); txtnodeDisp:=SVGDocument.createTextNode('Howdy!'); txt.appendChild(txtnodeDisp); SVGRoot.appendChild(txtDisp); // define an image imgDisp:=SVGDocument.createElement('image'); img:=TSVGDocument.Create(imgDisp); img.setAttributeNS(NamespaceURI,'xlink:href', '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.
|