Delphi:VCL中消息处理初探
来源:优易学  2011-9-10 18:00:17   【优易学:中国教育考试门户网】   资料下载   IT书店
 TObject是基类,所以我们先看一下TObject的DISPATCH方法。Dispatch根据传入的message来寻找相应的消息处理方法,如果找不到的话,就继续向上到父类的消息处理方法表中寻找响应的处理方法,一直到找到为止,如果找到顶还没有,则调用DefaultHandle来处理该消息。message可以是任何的类型,Dispatch假设message的头两位是消息的ID,它就是根据ID来寻找消息处理方法的。虽然任何类型的message都可以被接受,但是TObject的子类还是希望传入的message参数是TMessage的记录类型或其他证明的记录类型。
  以下声明和注释摘自与system.pas:
  { TObject.Dispatch accepts any data type as its Message parameter. The
  first 2 bytes of the data are taken as the message id to search for
  in the object\'s message methods. TDispatchMessage is an example of
  such a structure with a word field for the message id.
  }
  TDispatchMessage = record
  MsgID: Word;
  end;
  类的继承关系如下:
  TObject->TPersistent->TComponent->TControl
  TControl是所以可视化组件的父类,TControl提供了一个新的方法,WndProc:
  procedure TControl.WndProc(var Message: TMessage);
  var
  Form: TCustomForm;
  KeyState: TKeyboardState;
  WheelMsg: TCMMouseWheel;
  begin
  //如果处在设计期
  if (csDesigning in ComponentState) then
  begin
  Form := GetParentForm(Self);//得到拥有该组件的窗体
  if (Form <> nil) and (Form.Designer <> nil) and
  Form.Designer.IsDesignMsg(Self, Message) then Exit //消息由窗体来处理
  end;
  //窗体可以为其拥有的组件来处理键盘消息。
  if (Message.Msg >= WM_KEYFIRST) and (Message.Msg <= WM_KEYLAST) then
  begin
  Form := GetParentForm(Self);
  if (Form <> nil) and Form.WantChildKey(Self, Message) then Exit;
  end
  //关于鼠标的消息
  else if (Message.Msg >= WM_MOUSEFIRST) and (Message.Msg <= WM_MOUSELAST) then
  begin
  //如果组件不可以接受和处理双击消息,就将双击消息映射为单击消息。
  if not (csDoubleClicks in ControlStyle) then
  case Message.Msg of
  WM_LBUTTONDBLCLK, WM_RBUTTONDBLCLK, WM_MBUTTONDBLCLK:
  Dec(Message.Msg, WM_LBUTTONDBLCLK - WM_LBUTTONDOWN);
  end;
  case Message.Msg of
  WM_MOUSEMOVE: Application.HintMouseMessage(Self, Message);//如果是鼠标移动的消息,则出现hint窗口
  WM_LBUTTONDOWN, WM_LBUTTONDBLCLK://如果是左键被按下,或者双击,如果是自动拖动模式,则开始拖动,并将左键按下的状态加入组件的状态。

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训