计算机二级DELPHI辅导:thread中实现消息循环
来源:优易学  2011-6-7 12:33:41   【优易学:中国教育考试门户网】   资料下载   IT书店
  unit MsgThreadUnit;
  interface
  uses
  Classes, Windows, Messages, ComCtrls, SysUtils, Dialogs;
  type
  TMsgThread = class(TThread)
  private
  { Private declarations }
  FWinHandle: HWND;
  procedure DeallocateHWnd(Wnd: HWND);
  protected
  procedure Execute; override;
  procedure WndProc(var msg: TMessage);
  public
  constructor Create;
  destructor Destroy; override;
  property WinHandle: HWND read FWinHandle;
  end;
  implementation
  { THookThread }
  constructor TMsgThread.Create;
  begin
  FreeOnTerminate := true;
  FWinHandle := AllocateHWND(WndProc);
  Inherited Create(false);
  end;
  destructor TMsgThread.Destroy;
  begin
  DeallocateHWnd(FWinHandle);
  inherited;
  end;
  procedure TMsgThread.DeallocateHWnd(Wnd:HWND);
  var
  Instance: Pointer;
  begin
  Instance := Pointer(GetWindowLong(Wnd, GWL_WNDPROC));
  if Instance <> @DefWindowProc then
  SetWindowLong(Wnd, gwl_wndproc, LongInt(@DefWindowProc));
  FreeObjectInstance(Instance);
  DestroyWindow(Wnd);
  end;
  {这里可以根据自己需要处理的消息进行处理,所有消息都是windows的标准消息,当然也可以自定义消息,也可以使用RegisterWindowMessage注册消息,然后再在这里面使用,在这里列举了一个wm_copydata消息,是这个消息经常用于进程间的数据交换,比较难用}
  procedure TMsgThread.WndProc(var msg: TMessage);
  begin
  if msg.Msg = WM_COPYDATA then
  begin
  //ProcessCopyDataMsg(msg)
  end
  else Msg.Result := DefWindowProc(FWinHandle, Msg.Msg, Msg.wParam, Msg.lParam);
  end;
  procedure TMsgThread.Execute;
  begin
  { Place thread code here }
  while not Terminated do
  begin
  sleep(10);
  end;
  end;
  end.

责任编辑:小草

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