Delphi Open Tools API Instance Research (1) First Knowledge: Delphi / Interface / VCL Component Pack / Com (Understanding) From: http://dev.9cbs.net/develop/Article/21/21725.shtm Difficulty: ★★ ☆ ☆☆ Before this article officially begins, first apologize to everyone. Because this month's column articles should be released very early, but because of some things, they have been postponed until now, and this month is only released. Also, about this article I think I should thank several friends on 9CBS, they are Chechy, Framesniper, Pankun, especially for Chechy, let me know the interesting of Delphi Open Tools API (hereinafter referred to as OTA) and decided Some energy is put above. And introduced me quite good information. Although the title is called XXXXX (1), the next article may not be xxxxx (2) Because each article of this series will be an independent content, the reason why XXX (1) is because I Will write some things about OTA in a subsequent article. Oh, I said so much nonsense, start now! But in the beginning, I have to go again. Probably introduce OTA OTA is a set of interesting interfaces available in each version of Delphi, using it you can extend the Delphi IDE to make it in line with your needs. For example, you can extend the IDE menu, code editor, window designer, attribute editor, and control editor (this already explained in the previous series of articles on VCL development), etc., almost all where you want. Even the code inside function can expand! This exciting feature has achieved better development after Delphi5, and it is easier to use. Make developers can use very little, very basic code to complete these interesting extensions to achieve powerful features. Through OTA, you can also appreciate the perfect design of Delphi IDE, based on COM technology, making Delphi IDEs to be easily expanded without re-compiling IDEs. Before conducting this example, you should point out that the best information you want to learn is a Toolsapi.PAS file in the SourceàToolsapi in the Delphi installation directory, which lists all OTA interfaces and more detailed annotations. In addition to OTA sites, you can go to http://delphi.about.com/library/weekly/aa033099.htm and http://www.tempeest-sw.com/opentools/ to see, you can also go to Borland's news Group borland.public.delphi.opentoolsapi participates in the discussion. Of course, 26 chapters in the Delphi5 Developer Guide also introduced some OTA's knowledge and demonstrated how to realize the guide in a delphi (this article does not tell this), you can also go see. Since Delphi OTA's version is relatively large, the content in this article is premised using Delphi7. When Delphi IDE is running, there is an important instance we should clearly (instance) is BorlandIDeServices, which implements many OTA interfaces, in order to get a lot of interfaces from BorlandIDeServices, and these interfaces have been run at Delphi Really realized, we can easily get a lot of things, such as menus, windows, etc., and extended Delphi IDE, which has been called, windows, etc.
In order to be able to expand Delphi IDE, we must go to Delphi to enter, which means we have two ways to achieve our delphi extension (can also be called plug-ins) and release it out, one way is to design a plugin When the VCL component package (this article takes this form, please see the article I published before the VCL component package), allowing customers to install at the Delphi runtime. Another way is to put the plugin based on a DLL and HK E y _ C U R E N t_ u S E R / S O f T W A R E / B O R L A N D / B / B in the registry D E L P H I / 7. 0 / E X P E R T S Registered, and in the DLL in a special export function as an entry point, Delphi IDE will load your plugin after restarting (this method will Description in the later article). The latter provides a possible possible plug-in installer, the former needs to install the same as the installation component in the Delphi operation. Our example will be? Package package1; // ... omitted the compiler instruction, pay attention to the design of this component as the design (Design Time) RTL, VCL, DesignIde;
Contains ntamenu in 'NTAMENU.PAS'; END. You can see our plugin in NTAMENU.PAS, in which we mainly use the following interface: Iotaservices, interface directly to BorlandIDeServices, is OTA A foundation interface, we use its getParentHandle method to get the handle of the Delphi IDE; intaservices, the interface that is implemented at the Delphi runtime, you can use it directly to get the model of Delphi IDE, imagelis, actionList, Toolbar, we can directly Do a lot of operations; IotamoduleServices, Iotamodule, IOTAEDITOR, IOTASourceEditor, IotaEditView, you can see that we can use these interfaces to get the code editor step by step and finally get an IOTaEditPosition interface that can process data in the current cursor position. We use it to insert a code to the current cursor, insert a large number of code segments, you can use the IOTAEDITWRITER interface. With regard to the interface methods used in the following code we will explain in the comments, no interfaces and other methods don't forget to view the Toolsapi.Pas file. It can also be noted that the following code is detected in many places to ensure that the code is trying to do not have problems after running and can reasonably release resources reasonably when an abnormality occurs. Don't forget, our purpose is to expand Delphi, but not to bring the face of Delphi ID. unit NTAMenu; interface uses SysUtils, Classes, Menus, ToolsApi, Controls, ImgList, Graphics, Forms, ComCtrls, windows; type TNTATest = class private FMainMenu: TMainMenu; // for storing the main menu NewMenu delphi IDE: TMenuItem; / / We will insert the menu fimagelist: tcustomImagelist; // to store the imagelist imageindex1: integer; // detection of the Delphi IDE main menu and toolbar, please see the following code IDEHANDLE: HWND; // Storage IDE Handle Protected procedure addmenu; // Add our menu Procedure RemoveMenu; // Uninstall our menu Procedure Recodeediter (Sender: TOBJECT); // Menu item Procedure Procedure Aboutform (Sender: TOBJECT); // Menu item 2 Event PUBLIC CONSTRUCTOR CRETE; DESTRUCTOR Destroy; Override; End; Procedure Register; Var Myntatest: TNTATEST
Implementation process.addMenu; / / The same name method of the traditional component is different. This is not installed on the component panel // but directly calls the AddMenu method to add our menu end; {TNTATEST} constructor TNTATEST.CREATE; Begin IDEHandle: = (BorlandIDEServices as IOTAServices) .GetParentHandle; // we have made the handle end by GetParentHandle ide method IOTAServices interface; procedure TNTATest.AddMenu; var MenuItem: array [0..2] of TMenuItem; i: integer; Icon1 : Ticon; // Menu item Icon Begin FmainMenu: = (BorlandIDeServices as intaservices) .mainMenu; // We used intaservices's mainMenu properties to get the IDE's main menu fimagelist: = (BorlandIdeServices as intaservices) .imagelist; We got the image list of IDE with intaservices's imagelist attributes newMenu: = tMenuItem.create (FmainMenu); // Create our menu newMenu.caption: = 'hk.barton'; ImageIndex1: = - 1; // No Load Icon // The following code uses for and CASE to add two menu items to have a bit small matter, but // We show a more common way to add more menu items without having to copy Code.
For i: = 0 to 2 do begin menuitem [i]: = tMenuitem.create (newMenu); // Creating submenu items Case I of 0: begin menuItem [i] .caption: = 'inserttext'; icon1: = Ticon .Create; try icon1.loadfromfile ('d: /myworks/mycomponent/otatest/newform.ico'); // I loaded an icon from the hard disk as a menu item one icon Except on E: Exception Do Begin Raise Exception.create (E.MESSAGE); EXIT; End; End; ImageIndex1: = fimagelist.addicon (icon1); // Add that loaded icon and return an imageindex menuitem [i] .ImageIndex: = imageIndex1; menuitem [ I] .onclick: = RecodeEditer; // Add event handler end; 1: MenuItem [i] .caption: = '-'; // Of course there is a split symbol, in fact, 3 menu items 2: Begin Menuitem [ I] .caption: = 'About'; MenuItem [i] .onclick: = Aboutform; end; end; newMenu.add (MenuItem [i]); // Add menu item END; FmainMenu.Items.Add (newMenu); // Finally Add our menu to IDE main menu end; procedure tntatest.recodeediter (sender: Tobject); var module: IotamoduleServices; curentmoudle: Iotamodule; intFeditor: IO TAEditor; Editor: IOTASourceEditor; EditView: IOTAEditView; EditWriterPos: IOTAEditPosition; i: integer; begin Module: = BorlandIDEServices as IOTAModuleServices; CurentMoudle: = Module.CurrentModule; // get the currently open project module if CurentMoudle = nil IOTAModuleServices method of using CurrentModule The beginningBox (IDEHANDLE, 'currently not open project file', 'hkTest', MB_ICONInInformation; EXIT; END; // Traverse All files I: = 0 to curentmoudle.ModuleFileCount-1 Do Begin IntFeditor: = CurentMoudle.ModuleFileEditors [i]; // iotamodule's modulefileEditors [] property Get an IotaEditor if IntFeditor.QueryInterface (IotasourceEditor, Editor) =
S_OK THEN / / View whether the file traversed is the code file and has begun to edit in the code editor. // If you get an instance book; end; if editor = nil dam that implements IotasourceEditor, you can pass an OUT parameter Editor (IdeHandle, ',' HKTEST ', MB_ICONITION); EXIT; End; EditView : = Editor.EditViews [i]; // use the IOTASourceEditor EditViews [] attribute to give a IOTAEditView EditWriterPos: = EditView.Position; // use IOTAEditView Position property finally obtained a IOTAEditPosition EditWriterPos.InsertText ( '{/// This is Add by the otatest of hk.barton, enjoy days! ///} '); // otaeditposition InsertText method Insert a row code to the current cursor location, here is a row of comments. End; Procedure TNTATEST.ABOUTFORM (Sender: TOBJECT); // A simple about dialog, pay attention to IDEHANDE BEGIN MessageBox (IDEHANDE, 'this is a test of ota write by hk.barton ",' HKTEST ', MB_ICONITIONMATION ); end; procedure TNTATest.ReMoveMenu; // uninstall menu begin if assigned (NewMenu) then NewMenu.Free; end; destructor TNTATest.Destroy; begin MyNTATest.ReMoveMenu; if ImageIndex1 <> - 1 then // if front loaded icon The work does not release the icon, otherwise it will be released to the icon used by Delphi itself. Myntatest.FImageList.delete (myntatest.imageIndex1); inherited; end; initialization // created TNTATAST Myntatest: = TNTATEST.CREATE; Finalization // Release MyntateSt MyntateSt.Free; end during component is unloaded. Note the comments in the above code. Single, this example may not have much place, but as long as you have a slight extension, you can make this example a little actual use, you can join a lot of menu items, each item corresponds to some user commonly used but cumbersome, so you can just Enter the same code in development, or even set shortcuts, you can also make a setting window to allow users to set the required code and dynamic add menu items. Of course, you need to complete more complex plugins, you still need other OTA knowledge, I will introduce these in the future column articles. This time I wrote here, I have recommended some information about OTA in the place where the article is started. In addition, if you need all the code of this example, please give me a letter: hk.barton@sohu.com.