Why?

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; static Car A; HBRUSH hbrush; switch (message) { /* handle the messages */ case WM_CREATE: break; case WM_PAINT: hdc = GetDC(hwnd); hbrush = CreateSolidBrush(RGB(0, 255, 0)); SelectObject(hdc, hbrush); A.Draw(hdc, hbrush); //Rectangle(hdc,0,0,100,200); DeleteObject(hbrush); ReleaseDC(hwnd, hdc); break; case WM_KEYDOWN: switch (wParam) { case VK_RIGHT: //A.Move(5,5);change degree there break; case VK_LEFT: //A.Move(-5,-5); change degree there break; case VK_UP: A.Move(5, 0); //MessageBoxA(hwnd,"Yo","Yo",MB_OK|MB_SYSTEMMODAL); break; case VK_DOWN: A.Move(-5, 0); break; } //InvalidateRgn(hwnd,NULL,true); InvalidateRect(hwnd, NULL, 1); break; case WM_KILLFOCUS: //Game.Pause(); Game class method is raised here //when window lasts focus break; case WM_DESTROY: //delete A; //DeleteObject(hbrush); PostQuitMessage(0); /* send a WM_QUIT to the message queue */ break; default: /* for messages that we don't deal with */ return DefWindowProc(hwnd, message, wParam, lParam); } return 0; } class Car { float degree; bool collision; int pos_x; int pos_y; double x0, y0, x1, y1; const int width, height; /*const */ POINT pt[4]; //={{0,0},{125,0},{125,50},{0,50}}; public: Car():degree(0.f), collision(0), pos_x(0), pos_y(0), width(200), height(100), x0(250), y0(250), x1(500), y1(250), pt( { {0, 0}, {125, 0}, {125, 50}, {0, 50}}) {} void Draw(HDC hdc, HBRUSH oldbrush, HPEN hOldPen) { HPEN hNewPen; HBRUSH newbrush = CreateSolidBrush(RGB(255, 0, 0)); SelectObject(hdc, newbrush); //Using degree val to correctly draw the car frame ///Transform coordinates there ///pt[1].x = pt[0].x-(pt[1].x-pt[0].x)*cos(degree); ///pt[1].y = pt[0].y-(pt[1].y-pt[0].y)*sin(degree); ///^^^^^^^^^^^^^^^^^^^^^^^^^^^ Polygon(hdc, &pt[0], 4); /*Rectangle(hdc,pos_x,pos_y,pos_x+width,pos_y+height); */ MoveToEx(hdc, ceil(x0), ceil(y0), NULL); /// y1++; x1 = x0 + (x1 - x0) * (cos(degree)); char buff[40]; //itoa(cos(degree),buff,10); sprintf(buff, "%5.2lf", cos(degree)); string tmp("cos is: "); tmp.append(buff); TextOutA(hdc, 1, 2, tmp.c_str(), (int) tmp.length()); // itoa(ceil(sin(degree)),buff,10); tmp.assign("sin is: "); tmp.append(buff); TextOutA(hdc, 1, 25, tmp.c_str(), (int) tmp.length()); y1 = y0 + (y1 - y0) * (sin(degree)); hNewPen = CreatePen(PS_SOLID, 4, RGB(0, 255, 0)); SelectObject(hdc, hNewPen); LineTo(hdc, ceil(x1), ceil(y1)); SelectObject(hdc, hOldPen); SelectObject(hdc, oldbrush); DeleteObject(hNewPen); DeleteObject(newbrush); } void Move(const int dx, const int dy) { for (int i = 0; i < 4; i++) { pt[i].x += dx; pt[i].y += dy; } } void Degree(const float dx) { degree += dx; } void CheckCollision( /*const GameClass& A */ ) { } }; 

It eats constant.

  • one
    You, it seems, copied the code from somewhere and did not understand it. - andrybak
  • Eats continuously or only when working with a mouse? Car code lay out. - Ildar
  • You did not specify the most interesting piece - the message retrieval cycle. Most likely, the problem is there. - ganouver

3 answers 3

Typically, a WM_PAINT message is framed with a BeginPain() ... EndPaint() construct like this:

 PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_PAINT: hdc = BeginPaint(hwnd, &ps); TextOut(hdc, 0, 0, "Hello, Windows!", 15); EndPaint(hwnd, &ps); 

You do not have to call these functions. Maybe this is the case, i.e. in WM_PAINT processing, you draw on the window thereby invoking a new WM_PAINT message.

    The case may indeed be in the absence of BeginPaint and EndPaint , only WM_PAINT not called in response to drawing.

    WM_PAINT is called because the window area that requires rendering is not acknowledged. This is done by the functions ValidateRect and ValidateRgn .

    BeginPaint just calls one of them, and since it was absent, WM_PAINT never removed from the message queue, which caused the message loop to spin constantly, not plunging into the kernel for a long time and devouring all the user's free time.

      You brought too much code to figure it out right away. Find out a narrow section with the help of a profiler or independently measuring the execution time of code sections. After that, it will be easier for you to understand where the resource is consumed.