如何释放其事件处理程序中的对象? [英] How to free an object inside its event handler?

查看:62
本文介绍了如何释放其事件处理程序中的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有TMyClass,它是从TObject派生的类。它有一个TTimer。每隔几分钟,我从Timer.OnTimer中检查一个网页。当网页更改时,我完成了,我想释放MyClass。我如何释放它?

I have TMyClass, a class derived from TObject. It has a TTimer. Every few minutes, from Timer.OnTimer I check a web page. When the web page changes, I am done and I want to free MyClass. How do I free it?

我的问题类似于但我的控件不是TControl。它是TObject的后代。因此,消息将不起作用。
显然,解决方案是从TControl或更高版本继承我的课程。但是,我不想这样做。在这种情况下,解决方案是什么?

My question is similar to this one BUT my 'control' is not a TControl. It is descendent of TObject. So, Messages won't work. Obviously, the solution will be to derive my class from TControl or higher. But let's say I don't want to do that. What would be the solution in this case?

推荐答案

要接收消息,您需要具有窗口句柄。您可以使用 AllocateHWnd 分配一个,

To receive messages you need to have window handle. You can allocate one using AllocateHWnd, something like

type
  TMyClass = class(TObject)
  private
    FHandle: HWND;
    procedure MyWndProc(var Msg: TMessage);
  public
    constructor Create; virtual;
    destructor Destroy; override;
  end;

constructor TMyClass.Create();
begin
  inherited Create();
  FHandle := AllocateHWnd(myWndProc);
end;

destructor TMyClass.Destroy;
begin
  DeallocateHWnd(FHandle);
  inherited;
end;

procedure TMyClass.MyWndProc(var Msg: TMessage);
begin
  case Msg.Msg of
    CM_RELEASE: begin
       Free;
    end;
    else Msg.Result := DefWindowProc(FHandle, Msg.Msg, Msg.WParam, Msg.LParam);
  end;
end;

现在,您可以使用 FHandle ,如您引用的帖子中所示。

Now you can post messages to the object using the FHandle as demonstrated in the post youre reffering to.

这篇关于如何释放其事件处理程序中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆