关于事件(40分)

H

hongsen

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样知道一个部件能接收什么样的消息,如果对任意一条WINDOWS消息,都用关键字<br>MESSAGE来赋予消息处理函数,可行吗?<br>例:在自己的部件中,写如下代码<br>&nbsp;proceudre &nbsp;wmxxxx(var amessage:tmessage);message wmxxxx;<br>我不知道此部件是否能收到wmxxxx消息。<br>另外,在DELPHI中部件中公布的事件是什么东西,一种指针吗?如何给自己的部件添家事件?
 
消息处理函数如你说明,即可<br>这是一种Message Crack技术,实际上Borland还对不同的消息<br>定义了不同的记录更方便我们处理<br> 如:<br>&nbsp; &nbsp; procedure WMSize(var SizeMessage: TWMSize); message WM_SIZE;<br>&nbsp; &nbsp; procedure WMLButtonDblClk (var Msg: TWMLButtonDblClk);message wm_LButtonDblClk;<br>&nbsp; &nbsp; procedure CMFocusChanged(var Message: TCMFocusChanged); message CM_FOCUSCHANGED;<br>  <br>部件能收到什么样的消息,最好查Help<br><br><br><br>关于事件说明如下:<br><br>事件实际上是函数指针,<br><br>请看例子:<br>这是Delphi定义的标准事件,不带参数<br>(与普通函数不同的是加上 of Object说明是对象属性)<br>type TNotifyEvent = procedure (Sender: TObject) of object;<br><br>带参数的话:<br>&nbsp; TScrollEvent = procedure (Sender: TObject; Pos:Integer) of object;<br>                        -》可以加任何参数 <br><br>  TSomeObject = class (TCustomcontrol)<br>&nbsp; &nbsp; pirvate:<br>&nbsp; FOnScroll: TScrollEvent;<br>&nbsp; &nbsp; &nbsp; &nbsp; ....<br>&nbsp; &nbsp;    procedure WMVScroll(var Message: TWMVScroll); message WM_VSCROLL;<br><br>       -》处理滚行条垂直滚动消息:<br>&nbsp; &nbsp;published:<br>&nbsp; &nbsp; &nbsp; Property OnScroll: TScrollEvent Read FOnScroll Write FOnScroll;<br><br>使用方法:<br>procedure TSomeObject.WMVScroll(var Message: TWMVScroll);<br>var<br> FIncrement:Integer;<br>begin<br>&nbsp; if (Message.ScrollBar = 0) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;with Message do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;case ScrollCode of<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SB_THUMBTRACK:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Assigned(FOnScroll) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FOnScroll(Self,Pos);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;...<br>&nbsp; &nbsp; inherited;<br>end;<br><br>
 
delphi中部件能接受任何消息, 包括自定义消息.<br>只是在消息发布上有点技巧吧, 如果是windows control, 只要给这个部件<br>的handle postmessage就能接收.<br>对于没有windows handle的控件, 可以通过perform方法传递消息.<br>其实所有delphi中消息的处理例程都在winproc中, 只不过入口不同而已.<br>一般情况下如果消息处理过程返回时将Message.Result设为0, 表示该消息已处理<br>完毕, 不再往下传递. 所以将Result置为非0可以实现消息的往下传递吧. 如果在<br>消息处理时不想遗漏其他的处理该消息的过程, 别忘记加上inherited;一句.<br>当然也可以不加inherited;一句并将Result置0 来达到阻断原来消息句柄的处理<br>过程. 比如在画透明效果图象过程中常将WM_ERASEBKGND,WM_PAINT阻断.
 
Another_eYes和 Another_eYes两位先生解答都不错,我希望有更多的朋友发表见解。<br><br>
 
多人接受答案了。
 

Similar threads

回复
0
查看
821
不得闲
S
回复
0
查看
628
SUNSTONE的Delphi笔记
S
S
回复
0
查看
707
SUNSTONE的Delphi笔记
S
顶部