简体   繁体   English

InvalidateRect 没有重新绘制窗口

[英]InvalidateRect is not repainting the window

I got a polygon that is drawn whenever I press a button.每当我按下按钮时,我都会绘制一个多边形。

I wanted to repaint the polygon as I press the button again, but when I press it, it just paints another polygon without erasing the other one:我想在再次按下按钮时重新绘制多边形,但是当我按下它时,它只是绘制了另一个多边形而没有擦除另一个:

//header
#define CREATETRIANGLE 1

//WM_COMMAND
    case 2:
      PAINTPROCEDURE = CREATETRIANGLE;
      InvalidateRect(hwnd, NULL, TRUE);
      break;

//WM_PAINT
case WM_PAINT:
  switch(PAINTPROCEDURE){
    case 0:{
      hdc = BeginPaint(hwnd, &ps);
      EndPaint(hwnd,&ps);
      }
    break;
    case 1:
      RedrawWindow(hwnd, &rect, NULL, RDW_NOCHILDREN); //I tried this function, but it did nothing
      TriangleDC = BeginPaint(hwnd, &tps);
        SelectPen(TriangleDC, CreatePen(PS_SOLID, 2, RGB(256,256,256)));
        SelectBrush(TriangleDC, CreateSolidBrush(RGB(0,192,192)));
       {
       POINT vertices[] = {{baseX,baseY}, {(baseX-(triangle.sideB)),baseY}, {baseX,(baseY-triangle.sideC)}};
       Polygon(TriangleDC, vertices, 3);
       }
      EndPaint(hwnd,&ps);
      PAINTPROCEDURE = 0;
                break;

I also tried to get the polygon out of its scope, but it did nothing as well.我也试图将多边形移出它的范围,但它也什么也没做。

it just paints another polygon without erasing the other one它只是绘制另一个多边形而不擦除另一个

When you call BeginPaint, the system will normally call WM_ERASEBKGND before returning.当您调用 BeginPaint 时,系统通常会在返回前调用 WM_ERASEBKGND。 The WM_ERASEBKGND handler is then given a chance to "erase" the window.然后 WM_ERASEBKGND 处理程序有机会“擦除”窗口。 The default handler (from DefWndProc) will fill the area with the brush from the WNDCLASS for this window.默认处理程序(来自 DefWndProc)将使用来自此窗口的 WNDCLASS 的画笔填充该区域。

It sounds like the erasing isn't happening which can mean: (1) you've provided WM_ERASEBKGND handler that doesn't actually erase the screen, or (2) the hbrBackground in the WNDCLASS is set wrong or set to the null brush.听起来擦除没有发生,这可能意味着:(1) 您提供的 WM_ERASEBKGND 处理程序实际上并未擦除屏幕,或者 (2) WNDCLASS 中的 hbrBackground 设置错误或设置为空画笔。

In either of those cases, what should happen is that the fErase field of the PAINTSTRUCT you got from BeginPaint will be set to something other than 0, which tells you that you [the WM_PAINT handler] still have to erase the window first.在这两种情况中的任何一种情况下,应该发生的是您从 BeginPaint 获得的 PAINTSTRUCT 的 fErase 字段将设置为 0 以外的值,这告诉您 [WM_PAINT 处理程序] 仍然必须先擦除窗口。

Now sometimes people play games with their own custom WM_ERASEBKGND handler when trying to optimize painting (or in a miguided attempt to reduce flicker).现在,有时人们在尝试优化绘画时(或在尝试减少闪烁的情况下)使用自己的自定义 WM_ERASEBKGND 处理程序玩游戏。 That can result in the fErase flag being 0 even if you do need to erase first.这可能导致 fErase 标志为 0,即使您确实需要先擦除。

My advice is to let DefWndProc handle the WM_ERASEBKGND and to make sure you've got a proper value set for hbrBackground in the WNDCLASS.我的建议是让 DefWndProc 处理 WM_ERASEBKGND 并确保您在 WNDCLASS 中为 hbrBackground 设置了正确的值。 Once that works, you can experiment with other approaches.一旦成功,您就可以尝试其他方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM