Visual Graph圖形平臺在delphi中的使用
第一步:安裝Visual Graph
首先點擊Visual Graph目錄上的註冊COM.bat,最後顯示註冊成功,其實這一步就是註冊Visual Graph的ActiveX組件,他的組件名爲vg.dll.用命令行批處理寫上 regsvr32 vg.dll。
<wbr> 那麼就要在Delphi裏面調出ActiveX了。
Visual Graph的VG組件是遵循ActiveX標準的控件,很容易安裝到自己的開發環境中。</wbr>
1、註冊北京圖王的Visual Graph的VG組件。
2、打開DELPHI,選擇Component;Import ActiveX Control…菜單。
3、在Import ActiveX窗體中選擇Visual Graph ActiveX Control Module選項,點擊最下面的【Install…】按鈕。
4、在Package窗體中點擊【Compile】、【Install】按鈕,退出時選擇不保存。
5、在控件區的ActiveX頁中出現一個Visual Graph的VG組件圖標,表示安裝成功。
6、直接將Cell圖標拖到Form中即可使用。
7、Visual Graph的VG組件提供了幾種開發環境中的Demo,可以直接照辦使用,那基本就是一個樣的Demo。
第二步:在Delphi窗體裏面加入並調用 Visual Graph
最開始使用Visual Graph的時候是進行圖形管理與控制,也就是說,畫出幾個庫圖元之後,用戶可以自己加載圖庫,從圖庫裏面加入圖形,並對圖形進行一些屬性設置。
我覺得它的技術特點如下:
1)非常人性化設計,提供最爲完善的繪製、修改方法,各種顯示模式,隨意切換。
2)與常見的繪圖、文檔編輯軟件的操作方式相似,使用戶可以在短時間內掌握繪圖的使用方法。
3)豐富的一致的圖元庫讓用戶繪製圖形無需從頭開始,只要輕輕的拖曳即可做出漂亮的圖形。
4)VG裏面全部是矢量繪圖,縮放自如。
5)繪圖過程比用紙筆畫圖更簡單方便和精準,圖層控制等格式設置.
6)基本繪圖庫讓用戶可以通過直線、曲線、弧線、矩形和橢圓工具等元素繪製出新的圖形。
7)矢量圖形抗鋸齒。讓您繪製的圖形在任何角度都保持美觀。
8)無限撤銷和重做功能,使用更加方便靈活。
9)可以輸出爲VG專用的繪圖格式TBL與Bin,其他圖形格式.
10)所見即所得的操作方式。
第三步:圖形控制
首先拖入一個VG控件,在窗口的OnShow代碼裏面寫上:
procedure TForm1.FormShow(Sender: TObject);
begin
<wbr> vgctrl1.Run( ExtractFilePath( Application.ExeName ) + 'hmi.tbl' );
end;
運行一張圖,如下所示:</wbr>
<wbr>procedure TForm1.Button1Click(Sender: TObject);
var
</wbr><wbr> uu : IUnit;
</wbr><wbr> ret : Integer;
begin
</wbr><wbr> uu := vgctrl1.vg.ActiveSheet.UnitByName( TButton( Sender ).Caption );
</wbr><wbr> ret := uu.GetPropertyValue( 'Value' );
//或者也可以用
//ret := vgctrl1.vg.Execute('return '+TButton(sender).Caption+'.Value');
</wbr><wbr> ShowMessage( IntToStr( ret ) );
end;</wbr>
procedure TForm1.Button3Click(Sender: TObject);
var
<wbr> uu : IUnit;
</wbr><wbr> ret : Boolean;
begin
</wbr><wbr> uu := vgctrl1.vg.ActiveSheet.UnitByName( TButton( Sender ).Caption );
</wbr><wbr> ret := uu.GetPropertyValue( 'OnOff' );
//或者也可以用
//ret := vgctrl1.vg.Execute( 'return開關指示A1.OnOff' );
</wbr><wbr> if ret then
</wbr><wbr> </wbr><wbr> </wbr><wbr> ShowMessage( 'true' )
</wbr><wbr> else
</wbr><wbr> </wbr><wbr> </wbr><wbr> ShowMessage( 'false' );
end;</wbr>
procedure TForm1.Button4Click(Sender: TObject);
var
<wbr> uu : IUnit;
</wbr><wbr> ret : String;
begin
//首先從圖形裏面取得這個Iunit對象的指針,通過Isheet類裏面的UnitByName函數,通過圖元名字得到對象指針
uu := vgctrl1.vg.ActiveSheet.UnitByName( TButton( Sender ).Caption );
//再從對象指針裏面獲取其屬性,由於text是Itext類的屬性,而Iunit是Itext類的父類,如果想直接用COM接口得到Text屬性,那就要Itext(uu).text,纔行。同樣GetPropertyValue是萬能的取得屬性值的函數。SetPropertyValue是萬能的設置屬性值的函數,這兩個函數是Iunit的接口函數.
</wbr><wbr> ret := uu.GetPropertyValue( 'text' );
//或者也可以用
//ret := vgctrl1.vg.Execute( 'return文字A.text' );
</wbr><wbr> ShowMessage( ret );
end;</wbr>
procedure TForm1.Button5Click(Sender: TObject);
var
<wbr> uu : IUnit;
begin
</wbr><wbr> uu := vgctrl1.vg.ActiveSheet.UnitByName( TButton(sender).Caption );
</wbr><wbr> uu.SetPropertyValue( 'value', 6 );
end;</wbr>
procedure TForm1.Button7Click(Sender: TObject);
var
<wbr> uu : IUnit;
begin
</wbr><wbr> uu := vgctrl1.vg.ActiveSheet.UnitByName( TButton( Sender ).Caption );
</wbr><wbr> uu.SetPropertyValue( 'OnOff', True );
end;</wbr>
procedure TForm1.Button8Click(Sender: TObject);
var
<wbr> uu : IUnit;
begin
</wbr><wbr> uu := vgctrl1.vg.ActiveSheet.UnitByName( TButton( Sender ).Caption );
</wbr><wbr> uu.SetPropertyValue( 'text', 'Visual Graph' );
end;</wbr>
<wbr></wbr>
第四步:在Delphi與Visual Graph腳本混合編程交互
這兩個事例,都是運用了AddFunction函數。
<wbr></wbr>
AddFunction |
void=AddFunction(string, string, int, long) |
該函數屬於高級應用,給Visual Graph腳本體系添加一個內部函數。利用此函數可以擴充腳本語言的能力。 |
<wbr></wbr>
比如:
procedure TForm1.FormShow(Sender: TObject);
begin
<wbr> vgctrl1.AddFunction( 'CallDelphiEditChange', '44', 48, Longint( @CallDelphiEditChange ) );
</wbr><wbr> vgctrl1.AddFunction( 'CallDelphiRadioButtonCli</wbr><wbr>ck', '41', 48, Longint( @CallDelphiRadioButtonCli</wbr><wbr>ck ) );
</wbr><wbr> vgctrl1.AddFunction( 'CallDelphiCheckBoxClick', '43', 48, Longint( @CallDelphiCheckBoxClick ) );
</wbr><wbr> vgctrl1.Run( ExtractFilePath( Application.ExeName ) + 'demo.tbl' );
</wbr><wbr> UseDelphiControl := true;
end;</wbr>
在Visual Graph腳本里面是這樣寫的:
CallDelphiCheckBoxClick(Sender.Name, Sender.Checked)
CallDelphiEditChange( Sender.Name, Sender.Value )
CallDelphiRadioButtonCli<wbr>ck(Name, 0)
那麼VG腳本就能調用delphi內部的函數,
那麼現在Delphi如何調用VG的函數呢?,那麼要運用VG的Execute萬能函數了。</wbr>
Execute |
variant=Execute(string) |
執行腳本語句,並返回一個值(如果腳本最後有return語句的話),腳本語句可以是多行的長串。 |
<wbr></wbr>
在Delphi裏面就這樣使用:
Vgctrl1.Execute(“ActiveSheet. AddUnit(‘text’,1,1,0,0)”)就可以增加一個圖元了。
如查在設計圖形時,給頁面增加了一個自定義函數。
那麼用
Vgctrl1.Execute(“ActiveSheet. SelfInvoke()”),就能調用它了。
北京圖王軟件:http://www.visual-graph.com