Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. [edit: most problems fixed, one problem remaining] Invalidate rectangle follow up, flickering and no movement

[edit: most problems fixed, one problem remaining] Invalidate rectangle follow up, flickering and no movement

Scheduled Pinned Locked Moved C / C++ / MFC
helpgraphicsworkspace
4 Posts 3 Posters 10 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Calin Negru
    wrote on last edited by
    #1

    [edit] I solved the background flickering problem by changing the last argument in the InvalidateRect() function to false. I also got the test rectangle moving. The only problem left is that the white moving rectangle is still flickering. [/edit] I wrote some code, for some reason the rectangle that should work as background is flickering and I can`t get a test rectangle to move. I`m pasting the source bellow, please help me get my code in proper configuration.

    while (WM_QUIT != msg.message)
    {
    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    else
    {
    UpdateGame();
    }
    }

    //...
    switch (msg)
    {
    case WM_PAINT:
    hdc = BeginPaint(hwnd, &ps);
    draw(hdc);
    movingR = movingR + 0.1;
    RECT ARectangle;
    ARectangle.left = 0;
    ARectangle.top = 0;
    ARectangle.right = 600;
    ARectangle.bottom = 600;

    InvalidateRect(hwnd, &ARectangle, false);
    UpdateWindow(hwnd);
    
    EndPaint(hwnd, &ps);
    return 0;
    

    //...

    void draw(HDC hdc)
    {
    Gdiplus::Graphics gf(hdc);
    Gdiplus::Pen pen(Gdiplus::Color(255, 255, 0, 0));
    Gdiplus::SolidBrush brush(Gdiplus::Color(255, 0, 255, 0));
    Gdiplus::SolidBrush brush2(Gdiplus::Color(255, 255, 255, 255));

    gf.FillRectangle(&brush, 0, 0, 600, 600);
    gf.FillRectangle(&brush2,(int)movingR, 200, 100, 100);
    

    }
    //...

    void UpdateGame()
    {
    //movingR = movingR + 1;
    }

    Mircea NeacsuM L 2 Replies Last reply
    0
    • C Calin Negru

      [edit] I solved the background flickering problem by changing the last argument in the InvalidateRect() function to false. I also got the test rectangle moving. The only problem left is that the white moving rectangle is still flickering. [/edit] I wrote some code, for some reason the rectangle that should work as background is flickering and I can`t get a test rectangle to move. I`m pasting the source bellow, please help me get my code in proper configuration.

      while (WM_QUIT != msg.message)
      {
      if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
      {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
      }
      else
      {
      UpdateGame();
      }
      }

      //...
      switch (msg)
      {
      case WM_PAINT:
      hdc = BeginPaint(hwnd, &ps);
      draw(hdc);
      movingR = movingR + 0.1;
      RECT ARectangle;
      ARectangle.left = 0;
      ARectangle.top = 0;
      ARectangle.right = 600;
      ARectangle.bottom = 600;

      InvalidateRect(hwnd, &ARectangle, false);
      UpdateWindow(hwnd);
      
      EndPaint(hwnd, &ps);
      return 0;
      

      //...

      void draw(HDC hdc)
      {
      Gdiplus::Graphics gf(hdc);
      Gdiplus::Pen pen(Gdiplus::Color(255, 255, 0, 0));
      Gdiplus::SolidBrush brush(Gdiplus::Color(255, 0, 255, 0));
      Gdiplus::SolidBrush brush2(Gdiplus::Color(255, 255, 255, 255));

      gf.FillRectangle(&brush, 0, 0, 600, 600);
      gf.FillRectangle(&brush2,(int)movingR, 200, 100, 100);
      

      }
      //...

      void UpdateGame()
      {
      //movingR = movingR + 1;
      }

      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      You should use double buffering. Just google for “OpenGL double buffering”.

      Mircea

      1 Reply Last reply
      0
      • C Calin Negru

        [edit] I solved the background flickering problem by changing the last argument in the InvalidateRect() function to false. I also got the test rectangle moving. The only problem left is that the white moving rectangle is still flickering. [/edit] I wrote some code, for some reason the rectangle that should work as background is flickering and I can`t get a test rectangle to move. I`m pasting the source bellow, please help me get my code in proper configuration.

        while (WM_QUIT != msg.message)
        {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        }
        else
        {
        UpdateGame();
        }
        }

        //...
        switch (msg)
        {
        case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        draw(hdc);
        movingR = movingR + 0.1;
        RECT ARectangle;
        ARectangle.left = 0;
        ARectangle.top = 0;
        ARectangle.right = 600;
        ARectangle.bottom = 600;

        InvalidateRect(hwnd, &ARectangle, false);
        UpdateWindow(hwnd);
        
        EndPaint(hwnd, &ps);
        return 0;
        

        //...

        void draw(HDC hdc)
        {
        Gdiplus::Graphics gf(hdc);
        Gdiplus::Pen pen(Gdiplus::Color(255, 255, 0, 0));
        Gdiplus::SolidBrush brush(Gdiplus::Color(255, 0, 255, 0));
        Gdiplus::SolidBrush brush2(Gdiplus::Color(255, 255, 255, 255));

        gf.FillRectangle(&brush, 0, 0, 600, 600);
        gf.FillRectangle(&brush2,(int)movingR, 200, 100, 100);
        

        }
        //...

        void UpdateGame()
        {
        //movingR = movingR + 1;
        }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Do not call InvalidateRect and UpdateWindow from inside your WM_PAINT handler. You should be setting the rectangle's dimensions from outside the handler, and then calling InvalidateRect to cause the update to happen. Then when you get to the drawing code you get the rectangle details from inside the Paintstruct. Also using hardcoded values like you show above is not the correct way to do it.

        C 1 Reply Last reply
        0
        • L Lost User

          Do not call InvalidateRect and UpdateWindow from inside your WM_PAINT handler. You should be setting the rectangle's dimensions from outside the handler, and then calling InvalidateRect to cause the update to happen. Then when you get to the drawing code you get the rectangle details from inside the Paintstruct. Also using hardcoded values like you show above is not the correct way to do it.

          C Offline
          C Offline
          Calin Negru
          wrote on last edited by
          #4

          Richard and Mircea thanks for advice

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups