method pointer and regular procedure ( 积分: 100 )

  • 主题发起人 主题发起人 catkiller
  • 开始时间 开始时间
C

catkiller

Unregistered / Unconfirmed
GUEST, unregistred user!
unit UIEMonitor;<br><br>interface<br><br>uses<br> &nbsp;Windows, ActiveX, Classes, ComObj, SHDOCVW, Dialogs, SysUtils, Forms,<br> &nbsp;Messages, Graphics, Controls, StdCtrls,DDEman,ShellAPI, ExtCtrls,Registry,IniFiles,UrlMon;<br><br>type<br> &nbsp;TIEMonitor = class(TComObject, IDispatch, IObjectWithSite)<br> &nbsp;public<br> &nbsp;private<br> &nbsp;protected<br> &nbsp;end;<br><br>const<br> &nbsp;Class_IEMonitor: TGUID = '{A2765F19-26D9-4729-9BA2-930DC9DE2287}';<br><br>var<br> &nbsp;RUNTIMER:TTimer;<br><br>implementation<br><br>uses ComServ;<br><br>procedure ShowAD;<br>begin<br> &nbsp; &nbsp; RUNTIMER.Enabled := false;<br> &nbsp; &nbsp; ShellExecute(null,nil,PChar('http://www.sina.com'),nil,nil,SW_SHOWNORMAL);<br> &nbsp; &nbsp; RUNTIMER.Enabled := true;<br>end;<br><br>procedure init;<br>begin<br> &nbsp; &nbsp; &nbsp; &nbsp;RUNTIMER := TTimer.Create(nil);<br> &nbsp; &nbsp; &nbsp; &nbsp;RUNTIMER.Enabled:=false;<br> &nbsp; &nbsp; &nbsp; &nbsp;RUNTIMER.Interval := 60*1000;<br> &nbsp; &nbsp; &nbsp; &nbsp;RUNTIMER.OnTimer := ShowAD ;(这行报错)<br> &nbsp; &nbsp; &nbsp; &nbsp;RUNTIMER.Enabled:=true;<br>end;<br><br>initialization<br> &nbsp;TComObjectFactory.Create(ComServer, TIEMonitor, Class_IEMonitor,<br> &nbsp; &nbsp;'IEMonitor', '', ciMultiInstance, tmApartment);<br>end.<br><br><br>为何RUNTIMER.OnTimer := ShowAD ;这行不行???报错“method pointer and regular procedure”
 
unit UIEMonitor;<br><br>interface<br><br>uses<br> &nbsp;Windows, ActiveX, Classes, ComObj, SHDOCVW, Dialogs, SysUtils, Forms,<br> &nbsp;Messages, Graphics, Controls, StdCtrls,DDEman,ShellAPI, ExtCtrls,Registry,IniFiles,UrlMon;<br><br>type<br> &nbsp;TIEMonitor = class(TComObject, IDispatch, IObjectWithSite)<br> &nbsp;public<br> &nbsp;private<br> &nbsp;protected<br> &nbsp;end;<br><br>const<br> &nbsp;Class_IEMonitor: TGUID = '{A2765F19-26D9-4729-9BA2-930DC9DE2287}';<br><br>var<br> &nbsp;RUNTIMER:TTimer;<br><br>implementation<br><br>uses ComServ;<br><br>procedure ShowAD;<br>begin<br> &nbsp; &nbsp; RUNTIMER.Enabled := false;<br> &nbsp; &nbsp; ShellExecute(null,nil,PChar('http://www.sina.com'),nil,nil,SW_SHOWNORMAL);<br> &nbsp; &nbsp; RUNTIMER.Enabled := true;<br>end;<br><br>procedure init;<br>begin<br> &nbsp; &nbsp; &nbsp; &nbsp;RUNTIMER := TTimer.Create(nil);<br> &nbsp; &nbsp; &nbsp; &nbsp;RUNTIMER.Enabled:=false;<br> &nbsp; &nbsp; &nbsp; &nbsp;RUNTIMER.Interval := 60*1000;<br> &nbsp; &nbsp; &nbsp; &nbsp;RUNTIMER.OnTimer := ShowAD ;(这行报错)<br> &nbsp; &nbsp; &nbsp; &nbsp;RUNTIMER.Enabled:=true;<br>end;<br><br>initialization<br> &nbsp;TComObjectFactory.Create(ComServer, TIEMonitor, Class_IEMonitor,<br> &nbsp; &nbsp;'IEMonitor', '', ciMultiInstance, tmApartment);<br>end.<br><br><br>为何RUNTIMER.OnTimer := ShowAD ;这行不行???报错“method pointer and regular procedure”
 
procedure TForm1.time(Sender: TObject);<br>var<br> &nbsp;i:integer;<br>begin<br><br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> &nbsp;timer1.OnTimer:=time;<br>end;<br><br>end.
 
如果是在有窗体的应用程序里面是可以的,为何在dll里面不行呢???
 
OnTimer的声明是<br>TNotifyEvent = procedure (Sender: TObject); of object;<br>你的ShowAD没有Sender参数啊,当然不行。而且你的ShowAd必须是一个类里面的方法,因为OnTimer是TNotifyEvent的指针类型,TNotifyEvent是 of object的方法。<br>除非这样<br>TMyclass = class<br>public<br> &nbsp;procedure ShowAD(Sender: TObject);<br>end;<br><br>再<br>RunTimer.OnTimer := TMyClass.ShowAD;<br>可以。
 
我是创建的com object
 
com object没关系的,你给你的TIEMonitor 增加个showad的方法,给ontimer绝对没问题。
 
多人接受答案了。
 
后退
顶部