我开发一个控件,控件是从TComponent继承的,我想将它放在一个Form上后可以得到,这个Form上的消息。如何做呢????

解决方案 »

  1.   

    不用做了, delphi已经有一个了
    Additional->TApplicationEvents
      

  2.   

    用在该控件上,做消息处理
    procedure wndproc(var message:Tmessage);override;
    procedure TForm.wndproc(var message:tmessage);beginif message.msg=wm_mousemove thenbegincursor:=crarrow;{设置光标为crarrow,而不是缺省的crBeam光标}exit;end;if message.msg=wm_SetFocus thenexit;{屏蔽掉WM_setfocus消息,不让Tmyedit控件获得输入焦点}inherited wndproc(message);{其他消息交父辈wndproc处理}end;end.
      

  3.   

    很简单:
    1、在你的组件里面声明两个变量:
      OldWndProc: TFarProc;  //用来保存Form的窗口过程
      NewWndProc: Pointer;   //形成自己的窗口过程
    2、声明一个自己的窗口过程:
      procedure MyWndProc(var Message: TMessage);
    ...
    procedure TMyComponent.MyWndProc(var Message: TMessage);
    begin
      with Message do
      begin
        case Msg of
          ...  //自己需要处理的一些消息,视情况需要调用CallWindowProc
          ...  //自己需要处理的一些消息,视情况需要调用CallWindowProc
          else Result := CallWindowProc(OldWndProc, TCustomForm(Owner).Handle, Msg, wParam, lParam);
        end;
      edn;
    end;
    3、再定义两个过程,用来Hook和Unhook窗口过程:
      proivate
        procedure HookOwner;
        procedure UnhookOwner;
    ..
    procedure TMyComponent.HookOwner;
    begin
      if not Assigned(Owner) or Assigned(OldWndProc) or Assigned(NewWndProc) then Exit;
      OldWndProc := TFarProc(GetWindowLong(TCustomForm(Owner).Handle, GWL_WndProc));
      NewWndProc := MakeObjectInstance(MyWndProc);
      SetWindowLong(TCustomForm(Owner).Handle, GWL_WndProc, LongInt(NewWndProc));
    end;procedure TMyComponent.UnhookOwner;
    begin
      if Assigned(NewWndProc) and Assigned(OldWndproc) then
      begin
        if Assigned(Owner) and Assigned(OldWndProc) then
          SetWindowLong(TCustomForm(Owner).Handle, GWL_WndProc, LongInt(OldWndProc));
        if Assigned(NewWndProc) then
          FreeObjectInstance(NewWndProc);
        NewWndProc := nil;
        OldWndProc := nil;
      end;
    end;然后根据需要就可以Hook TForm的消息了,注意在注销的时候别忘了Unhook
      

  4.   

    很简单:
    1、在你的组件里面声明两个变量:
      OldWndProc: TFarProc;  //用来保存Form的窗口过程
      NewWndProc: Pointer;   //形成自己的窗口过程
    2、声明一个自己的窗口过程:
      procedure MyWndProc(var Message: TMessage);
    ...
    procedure TMyComponent.MyWndProc(var Message: TMessage);
    begin
      with Message do
      begin
        case Msg of
          ...  //自己需要处理的一些消息,视情况需要调用CallWindowProc
          ...  //自己需要处理的一些消息,视情况需要调用CallWindowProc
          else Result := CallWindowProc(OldWndProc, TCustomForm(Owner).Handle, Msg, wParam, lParam);
        end;
      edn;
    end;
    3、再定义两个过程,用来Hook和Unhook窗口过程:
      proivate
        procedure HookOwner;
        procedure UnhookOwner;
    ..
    procedure TMyComponent.HookOwner;
    begin
      if not Assigned(Owner) or Assigned(OldWndProc) or Assigned(NewWndProc) then Exit;
      OldWndProc := TFarProc(GetWindowLong(TCustomForm(Owner).Handle, GWL_WndProc));
      NewWndProc := MakeObjectInstance(MyWndProc);
      SetWindowLong(TCustomForm(Owner).Handle, GWL_WndProc, LongInt(NewWndProc));
    end;procedure TMyComponent.UnhookOwner;
    begin
      if Assigned(NewWndProc) and Assigned(OldWndproc) then
      begin
        if Assigned(Owner) and Assigned(OldWndProc) then
          SetWindowLong(TCustomForm(Owner).Handle, GWL_WndProc, LongInt(OldWndProc));
        if Assigned(NewWndProc) then
          FreeObjectInstance(NewWndProc);
        NewWndProc := nil;
        OldWndProc := nil;
      end;
    end;然后根据需要就可以Hook TForm的消息了,注意在注销的时候别忘了Unhook