如何创建一个纯winapi窗口 [英] How to create a pure winapi window
问题描述
目标是创建两个线程之间的通信,其中一个线程是主线程。我正在寻找的是创建一个占用较少资源并使用它来仅接收消息的窗口。
你可以引用我什么?
要做的是在线程中设置消息循环,并在主线程中使用AllocateHWnd来向后发送消息。这很简单。
在你的线程执行函数中有以下内容:
procedure TMyThread.Execute;
begin
//设置线程消息循环
PeekMessage(LMessage,0,WM_USER,WM_USER,PM_NOREMOVE);
//你的主循环
而不是终止do
begin
//在线程消息队列中查找消息并依次处理它们。
//你可以在这里使用GetMessage,它会阻止等待消息
//如果你的线程中没有其他的东西,这是很好的。
PeekMessage(LMessage,0,WM_USER,$ 7FFF,PM_REMOVE)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
结束
//做其他事情在这里可能会有一些
// kind的等待。睡眠(500);睡眠(500);睡眠呼吸
end;
结束
要向您的主题发送消息,请执行以下操作:
PostThreadMessage(MyThread.Handle,WM_USER,0,0);
在主线程方面,使用AllocateHWnd(在Classes单元中)设置一个窗口句柄, ,传递一个WndProc方法。 AllocateHWnd非常轻便,使用简单:
TMyMessageReciever = class
private
FHandle:integer;
程序WndProc(var Msg:TMessage);
public
构造函数创建;
drestructor Destroy;覆盖
属性句柄:整数读取FHandle;
end;
实现
构造函数TMyMessageReciever.Create;
开始
继承创建;
FHandle:= Classes.AllocateHWnd(WndProc);
结束
析构函数TMyMessageReciever.Destroy;
begin
DeallocateHWnd(FHandle);
继承了Destroy;
结束
程序TMyMessageReciever.WndProc(var Msg:TMessage);
begin
case Msg.Msg
//处理你的消息
end;
结束
并发送消息与 SendMessage
,其中将阻止邮件已被处理,或 PostMessage
它异步执行。
希望这有帮助。 p>
The goal is to create communication between the two threads, one of which is the main thread. What I'm searching for is creating a window that takes less resource and use it to only receive messages.
What could you refer me to?
What you need to do is set up a message loop in your thread, and use AllocateHWnd in your main thread to send message backward and forwards. It's pretty simple.
In your thread execute function have the following:
procedure TMyThread.Execute;
begin
// this sets up the thread message loop
PeekMessage(LMessage, 0, WM_USER, WM_USER, PM_NOREMOVE);
// your main loop
while not terminated do
begin
// look for messages in the threads message queue and process them in turn.
// You can use GetMessage here instead and it will block waiting for messages
// which is good if you don't have anything else to do in your thread.
while PeekMessage(LMessage, 0, WM_USER, $7FFF, PM_REMOVE) do
begin
case LMessage.Msg of
//process the messages
end;
end;
// do other things. There would probably be a wait of some
// kind in here. I'm just putting a Sleep call for brevity
Sleep(500);
end;
end;
To send a message to your thread, do something like the following:
PostThreadMessage(MyThread.Handle, WM_USER, 0, 0);
On the main thread side of things, set up a window handle using AllocateHWnd (in the Classes unit), passing it a WndProc method. AllocateHWnd is very lightweight and is simple to use:
TMyMessageReciever = class
private
FHandle: integer;
procedure WndProc(var Msg: TMessage);
public
constructor Create;
drestructor Destroy; override;
property Handle: integer read FHandle;
end;
implementation
constructor TMyMessageReciever.Create;
begin
inherited Create;
FHandle := Classes.AllocateHWnd(WndProc);
end;
destructor TMyMessageReciever.Destroy;
begin
DeallocateHWnd(FHandle);
inherited Destroy;
end;
procedure TMyMessageReciever.WndProc(var Msg: TMessage);
begin
case Msg.Msg of
//handle your messages here
end;
end;
And send messages with either SendMessage
, which will block till the message has been handled, or PostMessage
which does it asynchronously.
Hope this helps.
这篇关于如何创建一个纯winapi窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!