在WM_CREATE中创建子窗口,是否与同一线程相关? [英] Create child window in WM_CREATE, relevance of same thread?

查看:133
本文介绍了在WM_CREATE中创建子窗口,是否与同一线程相关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

典型的模式是在消息WM_CREATE的消息回调(WndProc)中创建子窗口:

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?

在与父线程相同的线程中创建子窗口有多重要?自"可以生孩子父GUI对话框线程创建子窗口?这似乎没有什么大问题?

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.

例如,您可以在WM_COMMAND消息处理程序内创建子窗口(例如,当用户单击按钮时)或作为对WM_TIMER的响应.

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 WM_CREATEMYWINDOW WM_USER + 123)
  2. 从另一个线程将其发布到您的窗口中:

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

  • 在您的窗口过程中创建子窗口:

  • In your window procedure create the child window:

    if (message == WM_CREATEMYWINDOW)
        hwndChild[i] = CreateWindow(...);
    

  • 这篇关于在WM_CREATE中创建子窗口,是否与同一线程相关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆