向线程发消息The PostThreadMessage function posts a message to the message queue of the specified thread. It returns without waiting for the thread to process the message.SyntaxBOOL PostThreadMessage(          DWORD idThread,
    UINT Msg,
    WPARAM wParam,
    LPARAM lParam
);
ParametersidThread
[in] Identifier of the thread to which the message is to be posted. 
The function fails if the specified thread does not have a message queue. The system creates a thread's message queue when the thread makes its first call to one of the User or GDI functions. For more information, see the Res section. Windows 2000/XP: This thread must either belong to the same desktop as the calling thread or to a process with the same locally unique identifier (LUID). Otherwise, the function fails and returns ERROR_INVALID_THREAD_ID. Windows Server 2003 SP1: This thread must have the SE_TCB_NAME privilege to post a message to a thread that belongs to a process with the same locally unique identifier (LUID) but is in a different desktop. Otherwise, the function fails and returns ERROR_INVALID_THREAD_ID.Msg
[in] Specifies the type of message to be posted.
wParam
[in] Specifies additional message-specific information.
lParam
[in] Specifies additional message-specific information.
Return ValueIf the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError. GetLastError returns ERROR_INVALID_THREAD_ID if idThread is not a valid thread identifier, or if the thread specified by idThread does not have a message queue.
ResThe thread to which the message is posted must have created a message queue, or else the call to PostThreadMessage fails. Use one of the following methods to handle this situation: Call PostThreadMessage. If it fails, call the Sleep function and call PostThreadMessage again. Repeat until PostThreadMessage succeeds. 
Create an event object, then create the thread. Use the WaitForSingleObject function to wait for the event to be set to the signaled state before calling PostThreadMessage. In the thread to which the message will be posted, call PeekMessage as shown here to force the system to create the message queue. 
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE)
Set the event, to indicate that the thread is ready to receive posted messages. The thread to which the message is posted retrieves the message by calling the GetMessage or PeekMessage function. The hwnd member of the returned MSG structure is NULL. Messages sent by PostThreadMessage are not associated with a window. As a general rule, messages that are not associated with a window cannot be dispatched by the DispatchMessage function. Therefore, if the recipient thread is in a modal loop (as used by MessageBox or DialogBox), the messages will be lost. To intercept thread messages while in a modal loop, use a thread-specific hook. The system only does marshalling for system messages (those in the range 0 to WM_USER). To send other messages (those above WM_USER) to another process, you must do custom marshalling. Windows 2000/XP: There is a limit of 10,000 posted messages per message queue. This limit should be sufficiently large. If your application exceeds the limit, it should be redesigned to avoid consuming so many system resources. To adjust this limit, modify the following registry key: HKEY_LOCAL_MACHINE
SOFTWARE
Microsoft
Windows NT
CurrentVersion
Windows
USERPostMessageLimit
The minimum acceptable value is 4000. Windows 95/98/Me: PostThreadMessageW is supported by the Microsoft Layer for Unicode (MSLU). To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems. 

解决方案 »

  1.   

    1.可以,可以在线程中做任何事情
    2.线程一般没有消息队列,windows统一管理消息,但你可以控制消息的流向
      

  2.   

    当然,不过你需要处理消息循环。
    或你从CWinThread派生。
      

  3.   

    用UI线程可以建立窗口!
    线程处理消息PeekMessage()与方线程通信,
    PostThreadMessage()主线程与子线程通信
      

  4.   

    UI线程要从CWinThread派生,处理自定义消息要用宏:On_Thread_Message()
      

  5.   

    子线程的消息是不是发送到了主线程WinMain函数的while(GetMessage())循环了啊?可不可以由子线程的消息循环来处理子线程消息?比如在子线程里写个while(GetMessage())?
      

  6.   


    子线程的消息是不是发送到了主线程WinMain函数的while(GetMessage())循环了啊?可不可以由子线程的消息循环来处理子线程消息?比如在子线程里写个while(GetMessage())?
    ===============================================================================
    线程要处理消息,就应该这样,不过一般会使用PeekMessage
      

  7.   

    > 可不可以在线程函数中建立窗口啊?
    可以,不过线程创建的窗口要想能响应发给它的消息,创建它的线程必须具有消息循环
            while(GetMessage(&msg, NULL, 0, 0))
            {        
                switch(msg.message) 
                {
                case WM_COMMAND:
                case WM_TIMER:
                 // 可以做你自己想做的处理
                }
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
    这样才能把消息派发给消息的目标窗体。
    你可以从CWinThread继承出自己的消息线程,CWinThread有两种实现类型,一种工作者线程,没有消息循环,另一种是用户接口线程,有消息循环,这样就不用你自己去做消息派发了
    > 线程函数的消息队列在哪里啊?怎么往线程函数队列里发送消息啊?
    线程的消息队列由系统维护,当线程第一次调用GetMessage或PeekMessage时创建。
    要往线程的消息队列中发送消息用
    PostThreadMessage( idThread, msg, wParam, lParam )
    要往线程创建的窗口发消息,则可用
    SendMessage( hWnd, msg, wParam, lParam )

    PostMessage( hWnd, msg, wParam, lParam )