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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. (MFC)Comfused in OnEraseBkgnd & OnPaint

(MFC)Comfused in OnEraseBkgnd & OnPaint

Scheduled Pinned Locked Moved C / C++ / MFC
c++winformsgraphicshelpquestion
5 Posts 3 Posters 0 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.
  • H Offline
    H Offline
    Hamed Musavi
    wrote on last edited by
    #1

    1.I created some ownerdrawn controls and put them on a dialog. 2.I want to use a custom gradient as the background of the dialog. 3.I override OnEraseBkgnd:

    BOOL CDlgButtons::OnEraseBkgnd(CDC* pDC)
    {
    // I draw a gradient here, using gdi+ and pDC
    return TRUE;
    }

    4.I leave OnPaint without any changes. Problem: None of the buttons on the form draw correctly. I don't have any other control. Ownerdraw buttons are completely hidden. None ownerdraw, turn to white rectangles, no text, nothing. if I draw my gradient without using pDC in OnEraseBkgnd, and instead use a new dc: CPaintDC dc(this); it's ok. I am completely confused. Can any one please describe this strange behavior?

    // "Life is very short and is very fragile also." Yanni
    while (I'm_alive)
    {
    cout<<"I love programming.";
    }

    M K 2 Replies Last reply
    0
    • H Hamed Musavi

      1.I created some ownerdrawn controls and put them on a dialog. 2.I want to use a custom gradient as the background of the dialog. 3.I override OnEraseBkgnd:

      BOOL CDlgButtons::OnEraseBkgnd(CDC* pDC)
      {
      // I draw a gradient here, using gdi+ and pDC
      return TRUE;
      }

      4.I leave OnPaint without any changes. Problem: None of the buttons on the form draw correctly. I don't have any other control. Ownerdraw buttons are completely hidden. None ownerdraw, turn to white rectangles, no text, nothing. if I draw my gradient without using pDC in OnEraseBkgnd, and instead use a new dc: CPaintDC dc(this); it's ok. I am completely confused. Can any one please describe this strange behavior?

      // "Life is very short and is very fragile also." Yanni
      while (I'm_alive)
      {
      cout<<"I love programming.";
      }

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Something wrong is happening elsewhere. I use this code in the main dialog of the app I use for quickly testing code I post here on CP:

      BOOL CMFCTesterDlg::OnEraseBkgnd(CDC* pDC)
      {
          CRect rect;
          GetClientRect(&rect);
          COLORREF c1 = RGB(64, 0, 64);
          COLORREF c2 = RGB(0, 128, 128);
          for(int i = 0; i < rect.Height(); i++)
          {
              int r, g, b;
              r = GetRValue(c1) + (GetRValue(c2) - GetRValue(c1)) * i / rect.Height();
              g = GetGValue(c1) + (GetGValue(c2) - GetGValue(c1)) * i / rect.Height();
              b = GetBValue(c1) + (GetBValue(c2) - GetBValue(c1)) * i / rect.Height();
              pDC->FillSolidRect(0, i, rect.Width(), 1, RGB(r, g, b));
          }
          return TRUE;
      }

      There's a potpourri of controls on the dialog and they all draw correctly. What happens if you comment-out the ON_WM_ERASEBKNGD() entry in the message map? Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      H 1 Reply Last reply
      0
      • H Hamed Musavi

        1.I created some ownerdrawn controls and put them on a dialog. 2.I want to use a custom gradient as the background of the dialog. 3.I override OnEraseBkgnd:

        BOOL CDlgButtons::OnEraseBkgnd(CDC* pDC)
        {
        // I draw a gradient here, using gdi+ and pDC
        return TRUE;
        }

        4.I leave OnPaint without any changes. Problem: None of the buttons on the form draw correctly. I don't have any other control. Ownerdraw buttons are completely hidden. None ownerdraw, turn to white rectangles, no text, nothing. if I draw my gradient without using pDC in OnEraseBkgnd, and instead use a new dc: CPaintDC dc(this); it's ok. I am completely confused. Can any one please describe this strange behavior?

        // "Life is very short and is very fragile also." Yanni
        while (I'm_alive)
        {
        cout<<"I love programming.";
        }

        K Offline
        K Offline
        karle
        wrote on last edited by
        #3

        I would use the device Context given as parameter. May this pDC has cliping areas defined. CPaintDC is not useful here because it calls BeginPaint / EndPaint. Beginpaint emit's an WM_ERASEBACKGROUND. see MSDN documentation of BeginPaint. You should also consider setting WS_CLIPCHILDREN style for dialog window.

        H 1 Reply Last reply
        0
        • M Mark Salsbery

          Something wrong is happening elsewhere. I use this code in the main dialog of the app I use for quickly testing code I post here on CP:

          BOOL CMFCTesterDlg::OnEraseBkgnd(CDC* pDC)
          {
              CRect rect;
              GetClientRect(&rect);
              COLORREF c1 = RGB(64, 0, 64);
              COLORREF c2 = RGB(0, 128, 128);
              for(int i = 0; i < rect.Height(); i++)
              {
                  int r, g, b;
                  r = GetRValue(c1) + (GetRValue(c2) - GetRValue(c1)) * i / rect.Height();
                  g = GetGValue(c1) + (GetGValue(c2) - GetGValue(c1)) * i / rect.Height();
                  b = GetBValue(c1) + (GetBValue(c2) - GetBValue(c1)) * i / rect.Height();
                  pDC->FillSolidRect(0, i, rect.Width(), 1, RGB(r, g, b));
              }
              return TRUE;
          }

          There's a potpourri of controls on the dialog and they all draw correctly. What happens if you comment-out the ON_WM_ERASEBKNGD() entry in the message map? Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          H Offline
          H Offline
          Hamed Musavi
          wrote on last edited by
          #4

          Sorry for late response.:sigh: I was out for a (long) while. Thank you for your help. I have more time to find the problem, today. I think the problem is with clipping. Thank you again for your help. :)

          // "Life is very short and is very fragile also." Yanni
          while (I'm_alive)
          {
              cout<<"I love programming.";
          }

          1 Reply Last reply
          0
          • K karle

            I would use the device Context given as parameter. May this pDC has cliping areas defined. CPaintDC is not useful here because it calls BeginPaint / EndPaint. Beginpaint emit's an WM_ERASEBACKGROUND. see MSDN documentation of BeginPaint. You should also consider setting WS_CLIPCHILDREN style for dialog window.

            H Offline
            H Offline
            Hamed Musavi
            wrote on last edited by
            #5

            Sorry for late response.:sigh: I was out for a (long) while.

            karle wrote:

            CPaintDC is not useful here because it calls BeginPaint / EndPaint. Beginpaint emit's an WM_ERASEBACKGROUND.

            However I'm not sure how it works exactly, but it was also strange to me that why it does not cause an infinite loop. It's more like every time OnPaint or OnEraseBkgnd is called, it's in a new thread! I forgot clipping totally. Thank you for your kind helpful answer. :)

            // "Life is very short and is very fragile also." Yanni
            while (I'm_alive)
            {
                cout<<"I love programming.";
            }

            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