1.一个简单的有界面单线程程序,假如在某消息处理过程中调用 MessageBox 弹出一个消息框,在点确定前主线程被停止在 MessageBox 调用处,但这时候程序仍然可以正常响应其它消息,比如优先级很低的 WM_TIMER,请问在主线程被停住的时候,是谁在完成这些消息处理? 再比如主线程进行某消息处理时调用 Invalidate、UpdateWindow,这时候主窗口就更新了,WM_PAINT 是如何被处理的?2.一个有两个线程的简单程序,主线程在处理某消息(该消息由主线程自己产生)时调用 MessageBox 从而使主线程停住,子线程仍以一定周期向主线程 SendMessage ,这时候会发现主线程仍可会响应子线程的这些消息,我想知道 SendMessage 时内部真是主线程在完成消息响应的吗?

解决方案 »

  1.   

    两个问题实际是同一问题,MessageBox实际上就是一个模态对话框,其内部有消息循环,会处理本线程所有窗口的消息。
      

  2.   

    那在一个消息处理中 Invalidate、UpdateWindow 呢?直接找到窗口过程函数并调用 ?
      

  3.   

    MessageBox也是一个DoModal()的对话框,它内部有自己的消息循环,从而阻塞主窗口消息循环,而它的消息循环会继续转发,处理消息...
      

  4.   

    Invalidate和UpdateWindow比较复杂。Invalidate是把窗口区域标记为“无效”,当窗口存在“无效”区域时,系统会不断向窗口发WM_PAINT消息,直到窗口全部“有效”为止。窗口在响应WM_PAINT消息时,会执行BeginPaint、EndPaint函数,BeginPaint函数会从消息队列中取出所有WM_PAINT消息,并将窗口标记为“有效”。UpdateWindow可以理解为直接调用窗口函数响应WM_PAINT消息。
      

  5.   

    UpdateWindow 函数通过发送重绘消息 WM_PAINT 给目标窗体来更新目标窗体客户区的无效区域。如果那个窗体的无效区域没有,就不发送重绘消息 WM_PAINT 了 。注意了,这个 API 函数是直接发送消息 WM_PAINT 给目标窗体的,没有进入过消息队列。
      

  6.   

    1, MessageBox内部会不停的PeekMessage并派发,就是MessageBox内部维持了消息的正常运作,你可以理解它只是执行到MessageBox还没返回,你不能理解为这个线程被挂起了。2,同理,主线程还是有消息循环在运作,所以可以响应其他线程的SendMessage
      

  7.   

    总结:   MessageBox 创建模态对话框,在其内部继续消息循环的处理,好比 MFC 对话框程序结构中构造一个对话框对象,模态显示对话框,在对话框对象释放后程序也就退出了。   UpdateWindow 时绕过了消息队列,走了非消息队列的实现方法。
      

  8.   

    The invalidated areas accumulate in the update region until the region is processed when the next WM_PAINT message occurs or until the region is validated by using the ValidateRect or ValidateRgn function. The system sends a WM_PAINT message to a window whenever its update region is not empty and there are no other messages in the application queue for that window. 
    The UpdateWindow function updates the client area of the specified window by sending a WM_PAINT message to the window if the window's update region is not empty. The function sends a WM_PAINT message directly to the window procedure of the specified window, bypassing the application queue. If the update region is empty, no message is sent.