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. how to change the device context for a status bar

how to change the device context for a status bar

Scheduled Pinned Locked Moved C / C++ / MFC
graphicstestingbeta-testingarchitecturehelp
11 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.
  • B BlrBoy

    Hello All.. I am trying to draw a bitmap as background for the status bar. I have derived my own status bar from CStatusBar. When I try to override OnPaint ().. it erases the default status bar messages and panes.. I tried to change the Device Context on OnCreate () and OnNCPaint () both are not working here is the code. void CMyStatusBar::OnNcPaint() { CDC* pdc= GetWindowDC(); CDC memdc; memdc.CreateCompatibleDC(pdc); memdc.SelectObject(m_hBmp); pdc->BitBlt(0, 0, m_rectLogo.Width(), m_rectLogo.Height(), &memdc, 0, 0, SRCCOPY); ReleaseDC(pdc); } int CMyStatusBar::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CStatusBar::OnCreate(lpCreateStruct) == -1) return -1; m_hBmp = (HBITMAP)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BKCOLOR_BMP), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION); BITMAP bm; GetObject(m_hBmp, sizeof(bm), &bm); m_rectLogo.left = 15; m_rectLogo.right = 15 + bm.bmWidth; m_rectLogo.top = 2 ; return 0; } Next I tried to create a solid brush (for testing) in OnCreate and tried to select that object but that is also not working int CMyStatusBar::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CStatusBar::OnCreate(lpCreateStruct) == -1) return -1; if (!m_oBrush.CreateSolidBrush (RGB(255, 0, 0))) AfxMessageBox ("Error in creating brush"); GetStatusBarCtrl().GetDC ()->SelectObject (&m_oBrush); return 0; } I am unable to change the DC of the status bar. Let me know how to draw a bitmap as back ground for a status bar? Thanks for your time

    J Offline
    J Offline
    jk chan
    wrote on last edited by
    #2

    Ok . Try CDC->fillsolidrect instead of bitmap painting. iF it not works , problem may with ur dc handle .

    If u can Dream... U can do it

    B 1 Reply Last reply
    0
    • J jk chan

      Ok . Try CDC->fillsolidrect instead of bitmap painting. iF it not works , problem may with ur dc handle .

      If u can Dream... U can do it

      B Offline
      B Offline
      BlrBoy
      wrote on last edited by
      #3

      Thanks for quick response. I am looking for chaging the background with an image. The function fillsolidrect changes the full color of the status bar.

      J 1 Reply Last reply
      0
      • B BlrBoy

        Thanks for quick response. I am looking for chaging the background with an image. The function fillsolidrect changes the full color of the status bar.

        J Offline
        J Offline
        jk chan
        wrote on last edited by
        #4

        Ok , i tried it and got the result. try moving the same code to onpaint.

        If u can Dream... U can do it

        B 1 Reply Last reply
        0
        • J jk chan

          Ok , i tried it and got the result. try moving the same code to onpaint.

          If u can Dream... U can do it

          B Offline
          B Offline
          BlrBoy
          wrote on last edited by
          #5

          I tried to override OnDraw () but all panes are not getting painted I am getting solid rectangle image on full status bar. here is the code void CMyStatusBar::OnPaint() { CPaintDC dc(this); // device context for painting RECT rect; GetClientRect (&rect); dc.FillSolidRect (&rect, RGB(255, 124, 20)); } If I remove FillSolidRect function the status message, panes are not visible. Please help me out. Thanks for your time.

          J 1 Reply Last reply
          0
          • B BlrBoy

            I tried to override OnDraw () but all panes are not getting painted I am getting solid rectangle image on full status bar. here is the code void CMyStatusBar::OnPaint() { CPaintDC dc(this); // device context for painting RECT rect; GetClientRect (&rect); dc.FillSolidRect (&rect, RGB(255, 124, 20)); } If I remove FillSolidRect function the status message, panes are not visible. Please help me out. Thanks for your time.

            J Offline
            J Offline
            jk chan
            wrote on last edited by
            #6

            Beacuse here you painted the entire status bar. IF you are doing it in onpaint, you need to handle every thing.:) or try WM_DARWITEM messages handling read this link from msdn (you can see ownder draw status bar). http://msdn2.microsoft.com/en-us/library/ms651126.aspx#sb_owner_draw[^]

            If u can Dream... U can do it

            B 1 Reply Last reply
            0
            • J jk chan

              Beacuse here you painted the entire status bar. IF you are doing it in onpaint, you need to handle every thing.:) or try WM_DARWITEM messages handling read this link from msdn (you can see ownder draw status bar). http://msdn2.microsoft.com/en-us/library/ms651126.aspx#sb_owner_draw[^]

              If u can Dream... U can do it

              B Offline
              B Offline
              BlrBoy
              wrote on last edited by
              #7

              Thanks... Here is an easy solution for that, you can call SetBkColor () directly. it will change the back ground color. m_wndStatusBar.GetStatusBarCtrl ().SetBkColor (RGB ( 255, 255, 22)); Thanks for your time

              J 1 Reply Last reply
              0
              • B BlrBoy

                Thanks... Here is an easy solution for that, you can call SetBkColor () directly. it will change the back ground color. m_wndStatusBar.GetStatusBarCtrl ().SetBkColor (RGB ( 255, 255, 22)); Thanks for your time

                J Offline
                J Offline
                jk chan
                wrote on last edited by
                #8

                Ok. But you said you need to Draw image as a background :) . I think there is no function to set background image. you need to handle drawitem

                If u can Dream... U can do it

                B 1 Reply Last reply
                0
                • J jk chan

                  Ok. But you said you need to Draw image as a background :) . I think there is no function to set background image. you need to handle drawitem

                  If u can Dream... U can do it

                  B Offline
                  B Offline
                  BlrBoy
                  wrote on last edited by
                  #9

                  You are right... I could achieve drawing background color, but not image. :-D Here is the code void CMyStatusBar::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC dc; dc.Attach(lpDrawItemStruct->hDC); // Get the pane rectangle and calculate text coordinates CRect rect(&lpDrawItemStruct->rcItem); switch(lpDrawItemStruct->itemID) { case 0: dc.FillSolidRect (rect, RGB (255,255,0)); break; here is the code in mainfrm.cpp in OnCreate () function for (int i=0; i<4; i++) { // Change Status Bar style to make it Owner-drawn m_wndStatusBar.GetStatusBarCtrl().SetText("", i, SBT_OWNERDRAW); } Since it is owner draw.. DrawItem is called. but FillSolidRect is getting executed but no change in UI. I tried to use CDC functions like TextOut, rectangle,etc.. UI there is no change. Can you say why is this behaviour? What went wrong Thanks for your time.

                  J 1 Reply Last reply
                  0
                  • B BlrBoy

                    You are right... I could achieve drawing background color, but not image. :-D Here is the code void CMyStatusBar::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { CDC dc; dc.Attach(lpDrawItemStruct->hDC); // Get the pane rectangle and calculate text coordinates CRect rect(&lpDrawItemStruct->rcItem); switch(lpDrawItemStruct->itemID) { case 0: dc.FillSolidRect (rect, RGB (255,255,0)); break; here is the code in mainfrm.cpp in OnCreate () function for (int i=0; i<4; i++) { // Change Status Bar style to make it Owner-drawn m_wndStatusBar.GetStatusBarCtrl().SetText("", i, SBT_OWNERDRAW); } Since it is owner draw.. DrawItem is called. but FillSolidRect is getting executed but no change in UI. I tried to use CDC functions like TextOut, rectangle,etc.. UI there is no change. Can you say why is this behaviour? What went wrong Thanks for your time.

                    J Offline
                    J Offline
                    jk chan
                    wrote on last edited by
                    #10

                    Because after your oncreate the framework again calls some SetText(with default stats messages , in that it is not ownerdraw)for puttif default messages for file -> new ,etc. So ur changes are lost.

                    If u can Dream... U can do it

                    J 1 Reply Last reply
                    0
                    • J jk chan

                      Because after your oncreate the framework again calls some SetText(with default stats messages , in that it is not ownerdraw)for puttif default messages for file -> new ,etc. So ur changes are lost.

                      If u can Dream... U can do it

                      J Offline
                      J Offline
                      jk chan
                      wrote on last edited by
                      #11

                      Sorry i forgot to say this , Also put m_wndStatusBar.SetPaneStyle(0,SBPS_OWNERDRAW); in ur on create. 0 -> pane number . this will be ok. :-D

                      If u can Dream... U can do it

                      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