增加24色Rebar的代码片断
int CMainFrame::CreateRebar()
{
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
// attach the hicolor bitmaps to the toolbar
AttachToolbarImages (IDB_HICOLOR_TOOLBAR,
IDB_HICOLOR_TOOLBAR_DISABLED,
IDB_HICOLOR_TOOLBAR_HOT);
return 0;
}
// find every pixel of the default background color in the specified
// bitmap and set each one to the user's button color.
static void ReplaceBackgroundColor (CBitmap& ioBM)
{
// figure out how many pixels there are in the bitmap
BITMAP bmInfo;
VERIFY (ioBM.GetBitmap (&bmInfo));
// add support for additional bit depths here if you choose
VERIFY (bmInfo.bmBitsPixel == 24);
VERIFY (bmInfo.bmWidthBytes == (bmInfo.bmWidth * 3));
const UINT numPixels (bmInfo.bmHeight * bmInfo.bmWidth);
// get a pointer to the pixels
DIBSECTION ds;
VERIFY (ioBM.GetObject (sizeof (DIBSECTION), &ds) == sizeof (DIBSECTION));
RGBTRIPLE* pixels = reinterpret_cast<RGBTRIPLE*>(ds.dsBm.bmBits);
VERIFY (pixels != NULL);
// get the user's preferred button color from the system
const COLORREF buttonColor (::GetSysColor (COLOR_BTNFACE));
const RGBTRIPLE userBackgroundColor = {
GetBValue (buttonColor), GetGValue (buttonColor), GetRValue (buttonColor)};
// search through the pixels, substituting the user's button
// color for any pixel that has the magic background color
for (UINT i = 0; i < numPixels; ++i)
{
if (pixels [i].rgbtBlue == kBackgroundColor.rgbtBlue &&
pixels [i].rgbtGreen == kBackgroundColor.rgbtGreen &&
pixels [i].rgbtRed == kBackgroundColor.rgbtRed)
{
pixels [i] = userBackgroundColor;
}
}
}
// create an image list for the specified BMP resource
static void MakeToolbarImageList (UINT inBitmapID,
CImageList& outImageList)
{
CBitmap bm;
// if we use CBitmap::LoadBitmap() to load the bitmap, the colors
// will be reduced to the bit depth of the main screen and we won't
// be able to access the pixels directly. To avoid those problems,
// we'll load the bitmap as a DIBSection instead and attach the
// DIBSection to the CBitmap.
VERIFY (bm.Attach (::LoadImage (::AfxFindResourceHandle(
MAKEINTRESOURCE (inBitmapID), RT_BITMAP),
MAKEINTRESOURCE (inBitmapID), IMAGE_BITMAP, 0, 0,
(LR_DEFAULTSIZE | LR_CREATEDIBSECTION))));
// replace the specified color in the bitmap with the user's
// button color
::ReplaceBackgroundColor (bm);
// create a 24 bit image list with the same dimensions and number
// of buttons as the toolbar
VERIFY (outImageList.Create (
kImageWidth, kImageHeight, kToolBarBitDepth, kNumImages, 0));
// attach the bitmap to the image list
VERIFY (outImageList.Add (&bm, RGB (0, 0, 0)) != -1);
}
// load the high color toolbar images and attach them to m_wndToolBar
void CMainFrame::AttachToolbarImages (UINT inNormalImageID,
UINT inDisabledImageID,
UINT inHotImageID)
{
// make high-color image lists for each of the bitmaps
::MakeToolbarImageList (inNormalImageID, m_ToolbarImages);
::MakeToolbarImageList (inDisabledImageID, m_ToolbarImagesDisabled);
::MakeToolbarImageList (inHotImageID, m_ToolbarImagesHot);
// get the toolbar control associated with the CToolbar object
CToolBarCtrl& barCtrl = m_wndToolBar.GetToolBarCtrl();
// attach the image lists to the toolbar control
barCtrl.SetImageList (&m_ToolbarImages);
barCtrl.SetDisabledImageList (&m_ToolbarImagesDisabled);
barCtrl.SetHotImageList (&m_ToolbarImagesHot);
}
void CMainFrame::OnCreateRebar()
{
if(0 > CreateRebar()) return;
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
}