计算机二级DELPHI:thread中实现消息循环计算机二级考试
文章作者 100test 发表时间 2009:05:07 18:06:33
来源 100Test.Com百考试题网
2009年下半年全国计算机等级考试你准备好了没?考计算机等级考试的朋友,2009年下半年全国计算机等级考试时间是2009年9月19日至23日。更多优质资料尽在百考试题论坛 百考试题在线题库
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.
特别推荐:
2009年9月全国计算机等级考试时间及科目预告