Delphi XE's rtti enhanced, dynamic hook some internal events

Source: Internet
Author: User

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
  1. Begin
  2. Rtticontext: = Trtticontext.   Create;
  3. Rtype: = Rtticontext.  GetType (Tmenuitem);
  4. Rfield: = Rtype.  GetField (' fonchange ');
  5. if Rfield <> nil Then
  6. begin
  7. V: = Rfield. GetValue (MainMenu1.  Items);
  8. Oldmenuitemchangecode: = Tvaluedata (V). Fasmethod.  Code;
  9. Oldselfdata: = Tvaluedata (V). Fasmethod.  Data;
  10. Tvaluedata (V). Fasmethod. Code: = @TForm1. Menuitemchange; //menu item changes
  11. Tvaluedata (V). Fasmethod.  Data: = self;
  12. Rfield. SetValue (self. MainMenu1.  ITEMS,V);
  13. 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
  1. Procedure TForm1. Menuitemchange (Sender:tobject;  Source:tmenuitem;
  2. Rebuild:boolean);
  3. Var
  4. M: procedure (selfdata:tobject; Sender:tobject;  Source:tmenuitem;
  5. Rebuild:boolean);
  6. Begin
  7. if Assigned (oldmenuitemchangecode) Then
  8. begin
  9. M: = Oldmenuitemchangecode;
  10. M (oldselfdata,sender,source,rebuild);
  11. end;
  12. ShowMessage (' Menu modification Event hooks ');
  13. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.