Delphi6 之Expert Demo |
|
ccchen
版主 發表:61 回覆:940 積分:1394 註冊:2002-04-15 發送簡訊給我 |
Delphi6之後,對於本身之環境在ToolsAPI.pas中提供了一系列的interface,藉由這些
Interface可以很輕鬆的完成一些與Delphi IDE有良好互動的Expert Kuang Cheng兄在
http://forum.vclxx.org/topic.php?TOPIC_ID=8466&FORUM_ID=40&CAT_ID=7
中對於這些Interface 多有描述(有興趣的可以查閱), 在這裡我希望可以藉由Demo讓新手
很容易的進入Expert的世界, 開始時不必對所有Interface有太多了解,只須照做, 因為
Expert只是一個架構, 主要功能之Implement均與一般Delphi程式無異. 首先我們先Create一個Expert空架構,有了此架構以後只須將須要之功能加入Menu, 不須要
有多少額外的知識, 只須集中精神Implement自己所須之功能. 本文將完成此架構,加入一menuItem以Open AboutForm,以後將逐一介紹如何取得Form上之
元件資訊及Unit中Source Code並進行修改. 1. Expert架構
TDemoExpt = class(TNotifierObject, IOTAWizard) private FIndex: Integer; public function GetIDString: string; function GetName: string; function GetState: TWizardState; procedure Execute; property Index: Integer read FIndex write fIndex; //本Expert之index end; var Expt: TDemoExpt; function TDemoExpt.GetName: string; //傳回此Expert之name begin Result:='DemoExpert'; end; function TDemoExpt.GetIDString: string; //傳回此Expert之辨識字串 begin Result:='DemoExpert'; end; function TDemoExpt.GetState: TWizardState; //傳回Expert Style,由於我們將所有功能 begin //均Implement成MenuItem, 故不傳回任何 Result:=[]; end; procedure TDemoExpt.Execute; //Expert 由MenuItem啟動, 故忽略此項 begin end;2. 將此Expert加入Delphi IDE中 BorlandIDEServices為提供IDE環境資訊之interface, 此處用其AddWizard Method將我們的 Expert加入Delphi中 procedure InitActionServices; begin if (BorlandIDEServices <> nil) then begin Expt := TDemoExpt.Create; Expt.Index := (BorlandIDEServices as IOTAWizardServices).AddWizard(Expt as IOTAWizard); if Expt.Index < 0 then raise Exception.Create('無法Create Expert'); end; end; initialization InitActionServices; 3. Delphi結束時Remove此Expert procedure DoneActionServices; begin if (BorlandIDEServices <> nil) then (BorlandIDEServices as IOTAWizardServices).RemoveWizard(Expt.Index); end; finalization DoneActionServices; 4. 為方便於Delphi Menu之任意位置加入MenuItem先寫一utility function 此function 與一般程式無異, 直接操作MainMenu 定義menu加入位置 TInsertAction = (iaBefore, iaAfter, iaChild); vAction:TInsertAction 加入之MenuItem相對於TargetItem之位置 TargetItem:TMenuItem aCaption:string 加入之MenuItem之顯示字串 ClickMethod:TNotifyEvent=nil 加入之MenuItem被Click後要執行之method function InsertMenuAt(vAction:TInsertAction;TargetItem:TMenuItem; aCaption:string;ClickMethod:TNotifyEvent=nil):TMenuItem; var pmenu:TMenuItem; id:integer; begin Result := TMenuItem.Create(nil); with Result do begin Caption := aCaption; onClick:=ClickMethod; if vAction = iaChild then TargetItem.add(Result) else if TargetItem.hasParent then begin pmenu:=TargetItem.parent; id:=pmenu.indexof(TargetItem); if vAction = iaAfter then inc(id); pmenu.insert(id, Result); end; end; end; 5. 於Delphi Menu--Tools之後加上"MyExpert", 並Create一子menu--"About" a. 於Expert中加入此二MenuItem之宣告及menuClick後所要執行之methodd. about MenuItem Click後執行 procedure TDemoExpt.AboutClick(Sender:TObject); begin with TfmAbout.Create(application.MainForm) do begin showmodal; free; end; end;TDemoExpt = class(TNotifierObject, IOTAWizard) private FIndex: Integer; fMMyExpert : TMenuItem; fMAbout:TMenuItem; public procedure AboutClick(Sender:TObject); ...b.於initial時加入此Menuitemprocedure InitActionServices; var MainMenu:TMainMenu; begin if (BorlandIDEServices <> nil) then begin Expt := TDemoExpt.Create; Expt.Index:=(BorlandIDEServices as IOTAWizardServices).AddWizard(Expt as IOTAWizard); if Expt.Index < 0 then raise Exception.Create('無法Create Expert'); with Expt do begin MainMenu:=(BorlandIDEServices as INTAServices).MainMenu; //取得delphi之menu //於Delphi menu第9項之後加入menuItem, caption為"MyExpert", 無相關method fMMyExpert := InsertMenuAt(iaAfter,MainMenu.items.items[8],'MyExpert'); //於MyExpert之下加入一子MenuItem=="About", click 後執行metho--AboutClick fMAbout:= InsertMenuAt(iaChild,fMMyExpert,'About',AboutClick); end; end; end;c. 結束時, Free此MenuItem [code] procedure DoneActionServices; begin if (BorlandIDEServices <> nil) then begin with Expt do begin fMAbout.free; //次序與Create方向相反 fMMyExpert.free; end; (BorlandIDEServices as IOTAWizardServices).RemoveWizard(Expt.Index); end; end; |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |