How to create a clean winapi window

The goal is to create a relationship between two threads, one of which is the main thread. What I'm looking for creates a window that takes up less resources and uses it only to receive messages.

What could you refer to?

+6
source share
3 answers

What you need to do is set up a message loop in your thread and use AllocateHWnd in your main thread to send the message back and forth. It is pretty simple.

Your thread execution function has 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 stream, do the following:

 PostThreadMessage(MyThread.Handle, WM_USER, 0, 0); 

On the side of the main flow of things, set the window handle using AllocateHWnd (in the Classes module), passing it the WndProc method. AllocateHWnd is very lightweight and easy 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 using SendMessage , which will block until the message is processed, or PostMessage , which does this asynchronously.

Hope this helps.

+7
source

This is for messages only for messages . I wrote a Delphi send + receive sample on the previous question .

+7
source

Gabr’s OmniThread Library offers a small, small DSiWin32 unit, you use it to create message boxes, or you can simply use OmniThread message for you.

+2
source

Source: https://habr.com/ru/post/894035/


All Articles