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. set back ground color for main frame in SDI

set back ground color for main frame in SDI

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
9 Posts 2 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.
  • G Offline
    G Offline
    gurucplusplus
    wrote on last edited by
    #1

    I want to set the back ground color for my SDI main frame using MFC. I don't know how?

    M 1 Reply Last reply
    0
    • G gurucplusplus

      I want to set the back ground color for my SDI main frame using MFC. I don't know how?

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

      The easiest way is to add a WM_ERASEBKGND handler to the window class. In response to the message, draw the background in the color of your choice:

      BOOL CMyFrameWnd::OnEraseBkgnd(CDC* pDC)
      {
      // draw background bright red

      CBrush bkBrush(RGB(0xFF,0x00,0x00));
      pDC->SelectObject(bkBrush);
      CRect CliRect;
      GetClientRect(&CliRect);
      pDC->PatBlt(CliRect.left, CliRect.top, CliRect.Width(), CliRect.Height(), PATCOPY);

      return TRUE;
      }

      Another way is to register a window class (instead of using the default) and in the WNDCLASS struct set the hbrBackground member to a brush of the desired color. Note that if your main frame has a view in it then you need to do this in the view class, not the frame class. Mark

      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

      G 1 Reply Last reply
      0
      • M Mark Salsbery

        The easiest way is to add a WM_ERASEBKGND handler to the window class. In response to the message, draw the background in the color of your choice:

        BOOL CMyFrameWnd::OnEraseBkgnd(CDC* pDC)
        {
        // draw background bright red

        CBrush bkBrush(RGB(0xFF,0x00,0x00));
        pDC->SelectObject(bkBrush);
        CRect CliRect;
        GetClientRect(&CliRect);
        pDC->PatBlt(CliRect.left, CliRect.top, CliRect.Width(), CliRect.Height(), PATCOPY);

        return TRUE;
        }

        Another way is to register a window class (instead of using the default) and in the WNDCLASS struct set the hbrBackground member to a brush of the desired color. Note that if your main frame has a view in it then you need to do this in the view class, not the frame class. Mark

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        G Offline
        G Offline
        gurucplusplus
        wrote on last edited by
        #3

        I add OnEraseBkgnd as you suggest in my main frame class, and when I launch my window application I see the red back ground flash quickly and then back to white back ground. Do you know why? Thanks

        M 1 Reply Last reply
        0
        • G gurucplusplus

          I add OnEraseBkgnd as you suggest in my main frame class, and when I launch my window application I see the red back ground flash quickly and then back to white back ground. Do you know why? Thanks

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

          gurucplusplus wrote:

          Do you know why?

          Yes, because you should have done it in the view class :)

          "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

          G 1 Reply Last reply
          0
          • M Mark Salsbery

            gurucplusplus wrote:

            Do you know why?

            Yes, because you should have done it in the view class :)

            "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

            G Offline
            G Offline
            gurucplusplus
            wrote on last edited by
            #5

            I got it. Thanks. But I have another question. I want to draw lines and rectangle box as overlay to exiting image display on main window frame. How can I do it? I currently can drawing lines, rectangle box on my image as I use mouse left and right mouse buttons. but what I did is to draw onto the image but not on graphic overlay frame. Thanks.

            M 1 Reply Last reply
            0
            • G gurucplusplus

              I got it. Thanks. But I have another question. I want to draw lines and rectangle box as overlay to exiting image display on main window frame. How can I do it? I currently can drawing lines, rectangle box on my image as I use mouse left and right mouse buttons. but what I did is to draw onto the image but not on graphic overlay frame. Thanks.

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

              As with the background, if you have a view in the frame then you do your drawing there instead of on the frame window - otherwise it will be "covered" by the view window like when you saw the frame background window flash. Typically, you'd do your drawing in response to a WM_PAINT message, after the image has been drawn. If it's a CView-derived class, overriding OnDraw() may be a more appropriate place, especially if the base class draws the image in OnDraw(). Either way, this means you need to store the coordinates (or whatever info is necessary) to redraw everything every time repainting is necessary (when WM_PAINT is received). How is the image being rendered in your case? Mark

              "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

              G 1 Reply Last reply
              0
              • M Mark Salsbery

                As with the background, if you have a view in the frame then you do your drawing there instead of on the frame window - otherwise it will be "covered" by the view window like when you saw the frame background window flash. Typically, you'd do your drawing in response to a WM_PAINT message, after the image has been drawn. If it's a CView-derived class, overriding OnDraw() may be a more appropriate place, especially if the base class draws the image in OnDraw(). Either way, this means you need to store the coordinates (or whatever info is necessary) to redraw everything every time repainting is necessary (when WM_PAINT is received). How is the image being rendered in your case? Mark

                "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                G Offline
                G Offline
                gurucplusplus
                wrote on last edited by
                #7

                I am new to MFC/VisualC++ 6.0. Can you point me few examples of how to draw graphic overlay in MFC?

                M 1 Reply Last reply
                0
                • G gurucplusplus

                  I am new to MFC/VisualC++ 6.0. Can you point me few examples of how to draw graphic overlay in MFC?

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

                  Do you have an image that's already being drawn? If so, how is being done? With a little more specifics I think I can give you a more specific example :) Mark

                  "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                  G 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    Do you have an image that's already being drawn? If so, how is being done? With a little more specifics I think I can give you a more specific example :) Mark

                    "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

                    G Offline
                    G Offline
                    gurucplusplus
                    wrote on last edited by
                    #9

                    I load the image from disk using IPicture renderring. This is my OnDraw(CDC* pDC) from CView class: CMyDoc* pDoc = this->GetDocument(); ASSERT_VALID(pDoc); HPALETTE hPal; pDoc->m_pPicture->get_hPal(reinterpret_cast(&hPal)); CPalette *pPalOld = NULL; if (hPal != NULL) { TRACE(_T("CXpiProView::OnDraw - RealizePalette\n")); pPalOld = pDC->SelectPalette(CPalette::FromHandle(hPal), FALSE); pDC->RealizePalette(); } RECT rc; this->GetClientRect(&rc); // display picture using IPicture::Render pDoc->m_pPicture->Render(pDC->GetSafeHdc(),pDoc->m_sizeInPix.cx, pDoc->m_sizeInPix.cy,0,pDoc->m_sizeInHiMetric.cy, pDoc->m_sizeInHiMetric.cx,-pDoc->m_sizeInHiMetric.cy,&rc);

                    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