非可视控件接受消息

使用sendmessage()postmessage()向控件发送消息时, 需要指定对应的句柄,而非可视控件继承于 TComponent 没有 Handle,所以一般非可视控件都不能接受外部发来的消息.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

为了让非可视控件能够接受消息,首先想到的就是给其加上句柄.

在查阅资料后,发现 Classes 里有两个个函数 AllocateHWnd(Method: TWndMethod): HWND; DeallocateHWnd(Wnd: HWND) 顾名思义 一个是分配 Handle 一个是释放 Handle

程序代码简单如下:

Type

TMyObject = class(TComponent)

private

  FWindowHandle: HWND;

Protected

  procedure WndProc(var Msg: TMessage);

public

  constructor Create(AOwner: TComponent); override;

  destructor  Destry;override;

  property Handle: HWND read FWindowHandle;
end;

 

procedure Register; 

 

implementation

 

procedure Register; 

begin

  .....

end;

 

constructor TMyObject.Create(AOwner: TComponent);

begin

  inherited Create(AOwner);

  FWindowHandle := AllocateHWnd(WndProc);

end;

 

destructor  TMyObject.Destry;

begin

  DeallocateHWnd(FWindowHandle);

  inherited Destroy;

end;

 

每个有Handle的控件都有这个段过程  }

procedure TMyObject.WndProc(var Msg: TMessage);

begin

  with Msg do

  begin

    case Msg of

      //自己的处理消息的过程.....

    else

      Result := DefWindowProc(FWindowHandle, Msg, WParam, LParam);

    end;

  end;

end;

 

向此控件发送消息时,跟从 TWinControl继承下来的控件一样的使用

  PostMessage(MyObject.Handle, ..., ..., ...);

就可以了。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值