how to change the device context for a status bar
-
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 workingint 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 -
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 workingint 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 -
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
-
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.
-
Ok , i tried it and got the result. try moving the same code to onpaint.
If u can Dream... U can do it
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. -
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.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
-
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
-
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 -
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
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 () functionfor (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. -
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 () functionfor (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. -
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