سؤال

A typical pattern is to create a child window in the message callback (WndProc) at message WM_CREATE:

LRESULT APIENTRY WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
...
switch (message) {
  case WM_CREATE:
  ....
  hwndChild[i] = CreateWindow (szChildClass[i], NULL, WS_CHILDWINDOW | WS_BORDER  ...

I perfectly understand this is a good opportunity, but is it a problem to do it any time later? One reason for doing so is that the child window is created within the same thread. But is there any other reason?

And how important is to create the child window in the same thread (as the parent)? As of " Can a child thread of parent GUI dialog thread create a child window? " this seems to be no general problem?

هل كانت مفيدة؟

المحلول

It's no problem to create your child window later, however, as you have mentioned, it should be created from the same thread.

For instance, you can create a child window inside a WM_COMMAND message handler (e.g. when a user clicks on a button) or as a response to WM_TIMER.

Creating a child window from another thread is a bad idea, as each thread has its own message queue. However, if you want another thread to initiate creating the window, you can work around it by sending a user-defined message to your window:

  1. Define your message (e.g. #define WM_CREATEMYWINDOW WM_USER + 123)
  2. From another thread post it to your window:

    PostMessage(g_hWnd, WM_CREATEMYWINDOW, 0, 0);
    
  3. In your window procedure create the child window:

    if (message == WM_CREATEMYWINDOW)
        hwndChild[i] = CreateWindow(...);
    
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top