How to color toolbar in MFC

How to color toolbar  which is  object  of CMFCToolBar.
I  done  following  code  but  it does not  change  default  color of  toolbar.
My toolbar is  having  Image  &  text  both.But inbetween  and  around  image  space  is  empty  where  I  have to  fill  color.
I have  attached image
       
  CRect crect;
	pMainFrame->m_wndToolBar.GetClientRect(&crect);
	CDC *pdcCtrl = pMainFrame->m_wndToolBar.GetDC();
	CDC dcDst;  
	dcDst.CreateCompatibleDC(pdcCtrl);
	dcDst.FillSolidRect(&crect,RGB(255,0,0));

Open in new window

1.png
ADITYA RAOSoftware DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZoppoCommented:
Hi enthurao,

allthough I never used it I think the proposed way to change the background of a CMFCToolBar is to derive your own class from it and to override its OnFillBackground method (see https://msdn.microsoft.com/en-us/library/bb983828.aspx).

Hope this helps,

ZOPPO
0
ADITYA RAOSoftware DeveloperAuthor Commented:
That is very lengthy  way.Please  tell me  some  shortcut
0
ZoppoCommented:
Hm - why lengthy? It's just something like:
class CMyToolBar : public CMFCToolBar
{
 void OnFillBackground(CDC* pDC) { /*do your drawing here*/ }
};

Open in new window

Then you can replace the CMFCToolBar instances you use now with this CMyToolBar class.

I don't think there's an easier way, at least there are no existing functions in CMFCToolBar (or I can't find them) to set a background color or bitmap.

BTW: From where do you call the drawin code you posted?

ZOPPO
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

ADITYA RAOSoftware DeveloperAuthor Commented:
I  derived  class  and  done  code like  below but I did  not  get  from where  should  I  pass CDC
         
     
     public:  // control bar embedded members
	CMFCMenuBar       m_wndMenuBar;
	CMerilToolBar       m_wndToolBar;
	CMFCStatusBar     m_wndStatusBar;

Open in new window

     
Following  two lines I  written  in InitInstance
       
CDC* pdc;
	pMainFrame->m_wndToolBar.OnFillBackground(pdc);

Open in new window



void CMerilToolBar::OnFillBackground(CDC* pDC)
{
	CRect crect;
	this->GetClientRect(&crect);
	CDC *pdcCtrl = this->GetDC();
	CDC dcDst;  
	dcDst.CreateCompatibleDC(pdcCtrl);
	dcDst.FillSolidRect(&crect,RGB(255,0,0));
}

Open in new window

0
ADITYA RAOSoftware DeveloperAuthor Commented:
#pragma once


// CMerilToolBar

class CMerilToolBar : public CMFCToolBar
{
	DECLARE_DYNAMIC(CMerilToolBar)

public:
	CMerilToolBar();
	virtual ~CMerilToolBar();
	 void OnFillBackground(CDC* pDC); 
protected:
	DECLARE_MESSAGE_MAP()
};

Open in new window

0
ZoppoCommented:
ok, first doing some drawing in InitInstance is meaningless since the toolbars will be repainted once or several times.

In fact you shouldn't have to do more than to replace the old CMFCToolBar with your derived one, the basic implementation of MFC's framework internally calls OnFillBackground whenever needed. Since OnFillBackground is declared as virtual in the base class this means your function is called instead. So you never need to call it directly yourself.

Further the implemented OnFillBackground you posted is mostly wrong, you should simply draw to the passed CDC, it's not senseful to draw to the window's own DC because probably the framework uses buffered drawing so what you draw directly on the window will be over-drawn. Further the real drawing operation you use works is called for a memory DC and never drawn to a real DC.

IMO it should simply look somehow like this:
void CMerilToolBar::OnFillBackground(CDC* pDC)
{
	CRect crect;
	GetClientRect(&crect);
	pDC->FillSolidRect(&crect,RGB(255,0,0));
}

Open in new window

Hope that helps,

ZOPPO
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ADITYA RAOSoftware DeveloperAuthor Commented:
Yes  it  worked
0
ADITYA RAOSoftware DeveloperAuthor Commented:
Image  attached  for view
1.png
0
ZoppoCommented:
Great, nice to hear this ... :o)

Have a nice day,

best regards,

ZOPPO
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.