Link to home
Start Free TrialLog in
Avatar of mike_marquet
mike_marquet

asked on

ToolBar buttons background color

Hi to all,

How can I change the background color of a toolbarctrl button ?

The toolbar was created in the resource editor.

I have x buttons and one of them, is a color choise button. I want foir this button that the bakground color becomes the choisen color.

Any idea to make this ?
ASKER CERTIFIED SOLUTION
Avatar of mahesh1402
mahesh1402
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mike_marquet
mike_marquet

ASKER

Thanks for infos, but have some problems when modifying the bitmap bits.

My problem is that all bitmaps from the toolbar image list are modified.

pToolBar->GetToolBarCtrl().GetImageList()->GetImageInfo(stBI.iImage, &stImageInfo);

is retrieving the bitmaps. The coordinates are correct  but the bitmap handle is always the same if I pass 0, 1, ... image index.


Here's the code :

void SetToolBarButtonBackColor(CToolBar *pToolBar, int nID, COLORREF clr)
 {
  TBBUTTONINFO stBI;
 
  stBI.cbSize = sizeof(stBI);
 
  stBI.dwMask = TBIF_IMAGE;
 
  pToolBar->GetToolBarCtrl().GetButtonInfo(nID, &stBI);
 
  IMAGEINFO stImageInfo;
 
  pToolBar->GetToolBarCtrl().GetImageList()->GetImageInfo(stBI.iImage, &stImageInfo);
 
  TRACE("[%d] => [(%d,%d)(%d,%d)] => 0x%X , 0x%X\n", stBI.iImage, stImageInfo.rcImage.left, stImageInfo.rcImage.top, stImageInfo.rcImage.right, stImageInfo.rcImage.bottom, stImageInfo.hbmImage, stImageInfo.hbmMask);
 
  CBitmap *pBitmap = CBitmap::FromHandle(stImageInfo.hbmImage);
 
  BITMAP stBitmap;
 
  pBitmap->GetBitmap(&stBitmap);
 
  BYTE nBytesPixel = stBitmap.bmBitsPixel / 8;
 
  DWORD nCount = stBitmap.bmHeight * stBitmap.bmWidth * nBytesPixel;
 
  BYTE *pData = new BYTE[nCount];
 
  DWORD nRet = pBitmap->GetBitmapBits(nCount, pData);
 
  VERIFY(nRet == nCount);
 
  memset(pData, 0, nCount);
 
  DWORD I = 0;
 
  while (I < nCount)
   {
    pData[I + 0] = GetBValue(clr);
    pData[I + 1] = GetGValue(clr);
    pData[I + 2] = GetRValue(clr);
   
    I += nBytesPixel;
   }
 
  nRet = pBitmap->SetBitmapBits(nCount, pData);

  VERIFY(nRet == nCount);

  delete pData;
 
  pToolBar->RedrawWindow();
 }