Create child window in WM_CREATE, relevance of same thread?

限于喜欢 提交于 2019-12-25 01:39:44

问题


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?


回答1:


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(...);
    


来源:https://stackoverflow.com/questions/11181649/create-child-window-in-wm-create-relevance-of-same-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!