{"id":30,"date":"2014-08-23T23:20:58","date_gmt":"2014-08-23T15:20:58","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=30"},"modified":"2024-05-10T15:17:47","modified_gmt":"2024-05-10T07:17:47","slug":"delphi%e4%b8%ad%e5%8c%bf%e5%90%8d%e6%96%b9%e6%b3%95%e5%8a%a8%e6%80%81%e7%bb%91%e5%ae%9a%e4%ba%8b%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=30","title":{"rendered":"Delphi\u4e2d\u533f\u540d\u65b9\u6cd5\u52a8\u6001\u7ed1\u5b9a\u4e8b\u4ef6"},"content":{"rendered":"<p><span style=\"color: #454545;\">\u5e94\u6062\u5f18\u4e4b\u7ea6\uff0c\u5199\u4e86\u4e00\u4e2a\u5bf9\u5176\u53d1\u5e03\u7684\u533f\u540d\u51fd\u6570\u52a8\u6001\u7ed1\u5b9a\u5230\u4e8b\u4ef6<\/span><span style=\"color: #454545;\">\u7684\u5c01\u88c5\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/span><\/p>\n<pre class=\"lang:delphi decode:true \">type\n  TAnonEvent=class\n  public\n    class function Wrap&lt;T1,T2&gt;(OnEvent:T1):T2;\n    class procedure Remove&lt;TEventType&gt;(const AEvent: TEventType);\n  end;\n{ TAnonEvent }\n\nclass procedure TAnonEvent.Remove&lt;TEventType&gt;(const AEvent: TEventType);\nvar\n  AMethod:TMethod absolute AEvent;\nbegin\n  if Assigned(AMethod.Data) then\n    IInterface(AMethod.Data)._Release;\nend;\nclass function TAnonEvent.Wrap&lt;T1,T2&gt;(OnEvent: T1):T2;\nvar\n  Addr:Pointer;\n  AMethod:PMethod;\nbegin\nAddr:=PPointer(@OnEvent)^;\nAMethod:=@Result;\nAMethod.Code:=PPointer(IntPtr(PPointer(Addr)^)+SizeOf(Pointer)*3)^;\/\/QueryInterface\/AddRef\/Release\/Invoke\nAMethod.Data:=Pointer(Addr);\nend;\n \ntype\n  TNotifyEventA=reference to procedure(ASender:TObject);\nprocedure TForm3.Button1Click(Sender: TObject);\nbegin\nButton2.OnClick:=TAnonEvent.Wrap&lt;TNotifyEventA,TNotifyEvent&gt;(procedure (sender: tObject)\n     begin\n      Button2.Caption:=DateTimeToStr(Now);\n       ShowMessage('OK');\n      TAnonEvent.Remove&lt;TNotifyEvent&gt;(Button2.OnClick);\n      Button2.OnClick:=nil;\n     end);\nButton2.OnClick(Sender);\nend;<\/pre>\n<p><strong>\u3010\u6ce8\u610f\u3011<\/strong><\/p>\n<p style=\"color: #454545;\"><strong>\u7531\u4e8e\u533f\u540d\u51fd\u6570\u4e2d\uff0c\u6211\u5c06TMethod.Data\u6307\u5411\u4e86\u533f\u540d\u51fd\u6570\u5bf9\u5e94\u7684\u63a5\u53e3\u5bf9\u8c61\uff0c\u6240\u4ee5\u5728\u533f\u540d\u51fd\u6570\u4e2d\uff0c\u4e0d\u8981\u4f7f\u7528Self\u6765\u4ee3\u8868\u5f53\u524d\u5bf9\u8c61\u3002\u5982\u679c\u975e\u8981\u4f7f\u7528\u7684\u8bdd\uff0c\u90a3\u4e48\u81ea\u5df1\u4fee\u6539\u4e0bWrap\u7684\u4ee3\u7801\uff0c\u5c06AMethod.Data:=Pointer(Addr)\u6539\u4e3a\u6307\u5411\u5f53\u524d\u51fd\u6570\u6240\u96b6\u5c5e\u5bf9\u8c61\u7684Self\u5373\u53ef\u3002<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e94\u6062\u5f18\u4e4b\u7ea6\uff0c\u5199\u4e86\u4e00\u4e2a\u5bf9\u5176\u53d1\u5e03\u7684\u533f\u540d\u51fd\u6570\u52a8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[8],"tags":[],"class_list":["post-30","post","type-post","status-publish","format-standard","hentry","category-delphi"],"views":14916,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/30","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=30"}],"version-history":[{"count":2,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/30\/revisions"}],"predecessor-version":[{"id":5801,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/30\/revisions\/5801"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}