为什么不手动发送WM_PAINT [英] why not to send WM_PAINT manually

查看:196
本文介绍了为什么不手动发送WM_PAINT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过,我不应该手动发送 WM_PAINT ,应该调用 InvalidateRect ,但没有发现任何东西关于为什么不,不过。那么为什么不呢?



更新使用 InvalidateRect ,但不能与 SendMessage(WM_PAINT)

  LRESULT CALLBACK window_proc(HWND wnd,UINT msg,WPARAM w_param, LPARAM l_param)
{
switch(msg)
{
case WM_PAINT:
PAINTSTRUCT ps;
HDC hdc = BeginPaint(wnd,& ps);

Polyline(..);

EndPaint(wnd,& ps);
return 0;

case WM_USER:
// SendMessage(wnd,WM_PAINT,NULL,NULL);
// InvalidateRect(wnd,NULL,FALSE);

return 0;
}
}


解决方案

WM_PAINT 的文档声明你不应该在备注部分的第一句。认为这应该是足够的理由不要了。



至于技术原因,我想这是其中之一,取自 BeginPaint 注释部分:


更新区域由 InvalidateRect InvalidateRgn 函数设置,系统会在调整大小,创建,滚动或影响客户区域的任何其他操作。


因此 BeginPaint WM_PAINT ,则c>可能无法正常工作。



可能有更多的原因/ p>

I have read that I should never send WM_PAINT manually and should call InvalidateRect instead but didn't found anything about why not, however. So why not?

update works with InvalidateRect but not with SendMessage(WM_PAINT)

LRESULT CALLBACK window_proc(HWND wnd, UINT msg, WPARAM w_param, LPARAM l_param)
{
  switch (msg)
  {
    case WM_PAINT:
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(wnd, &ps);

        Polyline(..);

        EndPaint(wnd, &ps);
        return 0;

    case WM_USER:           
        // SendMessage(wnd, WM_PAINT, NULL, NULL);
        // InvalidateRect(wnd, NULL, FALSE);

        return 0;
  }
}

解决方案

Official docs for WM_PAINT state that you shouldn't in the very first sentence of the remarks section. Seriously, that should be enough of a reason not to.

As for technical reasons why, I guess this is one of them, taken from BeginPaint remarks section:

The update region is set by the InvalidateRect or InvalidateRgn function and by the system after sizing, moving, creating, scrolling, or any other operation that affects the client area.

Thus BeginPaint might not work correctly if you send WM_PAINT manually.

There might be more reasons/surprises.

这篇关于为什么不手动发送WM_PAINT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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