Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a pure winapi window

Tags:

delphi

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?

like image 429
netboy Avatar asked Aug 01 '11 15:08

netboy


People also ask

How do I create a new window in C++?

lpfnWndProc = WindowProc; wc. hInstance = hInstance; wc. lpszClassName = CLASS_NAME; RegisterClass(&wc); // Create the window. HWND hwnd = CreateWindowEx( 0, // Optional window styles.

Is Win32 a window?

(1) A 32-bit version of Windows. Many editions of Windows have come in both 32-bit and 64-bit versions. See 32-bit computing and x86/x64. (2) Win32 is the programming interface (API) for 32-bit and 64-bit Windows operating systems.

What is Hwnd C++?

A Windows window is identified by a "window handle" ( HWND ) and is created after the CWnd object is created by a call to the Create member function of class CWnd . The window may be destroyed either by a program call or by a user's action.


1 Answers

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.

like image 149
Nat Avatar answered Oct 09 '22 10:10

Nat