-1
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
HBRUSH brush;
HBRUSH hBrush;
HPEN hPen;

static int dX[3] , dY[3] ;
static int x[3], y[3], oldX[3], oldY[3];
switch (message)
{
case WM_CREATE:
    SetTimer(hWnd, 1, 3, NULL);
    Beep(750, 300);
    for (int i = 0; i < 3; i++){
        dX[i] = rand() % 5 + 0;
        dY[i] = rand() % 5 + 0;
        x[i] = rand() % 5 + 0;
        y[i] = rand() % 5 + 0;
        oldX[i] = x[i];
        oldY[i] = y[i];
    }
    break;
case WM_TIMER:
    hdc = GetDC(hWnd);
    brush = (HBRUSH)SelectObject(hdc, GetStockObject(WHITE_BRUSH));

    RECT temp[3];
    RECT rect;
    GetClientRect(hWnd, &rect);
    for (int i = 0; i <3; i++){
        temp[i].left = oldX[i];
        temp[i].top = oldY[i];
        temp[i].right = oldX[i] + 30;
        temp[i].bottom = oldY[i] + 30;
        FillRect(hdc, &temp[i], brush); 
        brush = (HBRUSH)SelectObject(hdc, GetStockObject(GRAY_BRUSH));
        Ellipse(hdc, x[i], y[i], 30 + x[i], 30 + y[i]);
        oldX[i] = x[i];
        oldY[i] = y[i];

        x[i] += dX[i];
        y[i] += dY[i];


        if (x[i] + 30 > rect.right || x[i] < 0)
        {
            dX[i] = -dX[i];
        }
        if (y[i] + 30 > rect.bottom || y[i] < 0)
        {
            dY[i] = -dY[i];
        }
    }

    SelectObject(hdc, brush);

    ReleaseDC(hWnd, hdc);

    break;

If i draw > 2 circle . It blur streaks behind but i dont' want it . Please Help me . Thanks

Here is a picture of my problem:

enter image description here

jww
  • 97,681
  • 90
  • 411
  • 885
user3883921
  • 51
  • 1
  • 2
  • 7

2 Answers2

2

Your entire approach is broken. You are painting at the wrong time. You should perform your painting in response to WM_PAINT. The entire design of Win32 is based on programs responding to WM_PAINT by painting themselves. The framework will erase the background leaving your program to paint the foreground.

There are many reasons why a window might need to be repainted. You are concentrating on the regular pulse of your animation. But what if another program is dragged over yours?

Solve the problem like this:

  1. Move the drawing code to a WM_PAINT handler.
  2. Obtain the device context by calling BeginPaint.
  3. Call InvalidateRect from your timer to mark the window as invalid and thereby force a paint cycle.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

The first time you call FillRect you do it with a white brush. But on subsequent times through the loop the rectangle is drawn with the gray brush so you get the gray streaks. I suggest you create a white brush and a gray brush just once, before the loop. Then select the brush you want before each paint.

ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15