Delphi2010 after the Rtti did a great rest, now use it very cool oh. You can even get the internal private units of some classes and assign them a value! Speaking of this rtti enhancement, you can refer to a number of online blog content, I enumerate:
Enhancement of the Delphi2010rtti
The exploration of Delphi's Anymouse method
In case of Rtti series
My main purpose here is to hook up some internal private events and then add some of my own processing, where I use Tmenuitem's private internal event Fonchange as a routine. This private event is bound inside the menu, and we are not able to change it outside of the normal state! But after xe this problem is not there, using its new rtti, it is easy to get a class object of basically all the fields of the class and methods and properties.
[Delphi]View Plaincopy
- Begin
- Rtticontext: = Trtticontext. Create;
- Rtype: = Rtticontext. GetType (Tmenuitem);
- Rfield: = Rtype. GetField (' fonchange ');
- if Rfield <> nil Then
- begin
- V: = Rfield. GetValue (MainMenu1. Items);
- Oldmenuitemchangecode: = Tvaluedata (V). Fasmethod. Code;
- Oldselfdata: = Tvaluedata (V). Fasmethod. Data;
- Tvaluedata (V). Fasmethod. Code: = @TForm1. Menuitemchange; //menu item changes
- Tvaluedata (V). Fasmethod. Data: = self;
- Rfield. SetValue (self. MainMenu1. ITEMS,V);
- end;
The above code sets the MainMenu's items ' private event variable Fonchange's new point address as the implementation code for this form, Oldmenuitemchangecode, Oldselfdata is the original method pointer that holds the private event and the self pointer object passed. Used to make a call in the object we replaced, otherwise the original process would not be called. And then we'll take a look at the new Fonchange implementation process.
[Delphi]View Plaincopy
- Procedure TForm1. Menuitemchange (Sender:tobject; Source:tmenuitem;
- Rebuild:boolean);
- Var
- M: procedure (selfdata:tobject; Sender:tobject; Source:tmenuitem;
- Rebuild:boolean);
- Begin
- if Assigned (oldmenuitemchangecode) Then
- begin
- M: = Oldmenuitemchangecode;
- M (oldselfdata,sender,source,rebuild);
- end;
- ShowMessage (' Menu modification Event hooks ');
- End
The most important thing in this code is how to invoke the original Fonchange execution process, the person familiar with Delphi should know that the Delphi object process can use self to represent the current object, and this object in the process of the object is passed through EAX, At this point our new process is not executed inside the Mainmenu.items object, so we need to show the pass Selfdata as the Self object pointer in the source object, so I declare a pointer procedure as follows:
M:procedure (Selfdata:tobject; Sender:tobject; Source:tmenuitem;
Rebuild:boolean);
For this declaration process, if not very clear, please go to Cnpack's forum to find my detailed analysis of such information process.
At this point, the internal fonchange process is completed, the complete code
http://blog.csdn.net/suiyunonghen/article/details/6116791
Delphi XE's rtti enhanced, dynamic hook some internal events