|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionXTrueColorToolBar allows you to use true-color (24 bits per pixel) toolbars in your app by adding only one line of code. XTrueColorToolBar is based on Peter Lee's article Full-Featured 24-bit Color Toolbar. Peter describes his implementation in great detail, so I will not bother repeating it here. What I have done is wrap Peter's code in a C++ class, and add one additional feature: support for Office-style toolbar color button.
What's New in v1.1In this version I have added drop-down button to demo project, after receiving several emails asking for this feature. You can still download version without drop-down button (simple button demo project).
Office-Style Color ButtonMany applications have need to change text color, and they typically allow user to do this by means of toolbar button. For example, you might see this:
This color button shows the currently selected color in a bar underneath the letter
The way this button works is that when you click the down arrow, a color picker will be displayed:
After you choose a color, the color button will be updated:
Now you can select some text:
and click the left side of the color button to apply the color you just picked to the selected text:
Notes on the Demo AppTo illustrate how to use XTrueColorToolBar and color button, I used WordPad Sample as distributed in VS6. In demo app I have simply used standard CColorDialog MFC class, rather than one of the better-looking color pickers available here on CodeProject. The toolbars in the demo app are 16-color toolbars that I converted to 24-bit bitmaps with MS Paint. Loading the True-color ToolbarIn the demo app mainfrm.cpp, this code loads the toolbar bitmap and creates the image list: // set up true-color image buttons m_wndFormatBar.AttachToolbarImages(IDB_FORMATBAR, 8, // no. of images in bitmap 0); // 0 = normal, 1 = disabled, 2 = hotThis is all you need to do if you just want true-color toolbar support with no color button. Setting up the Color Button
The first step in using color button is to create image in toolbar bitmap. You must use a unique color for bar that appears below the letter
The bar under the letter
After toolbar has been loaded (see Loading the True-color Toolbar), the following code will set initial color bar of color button: // create template for color button m_wndFormatBar.CreateTemplate(RGB(255,0,1)); // init toolbar for drop-down color button and set initial color m_wndFormatBar.InitColorButton(ID_SET_TEXT_COLOR, RGB(0,0,0));The call to CreateTemplate() is necessary only once for each toolbar. It creates a copy of toolbar image list (the actual toolbar image list is modified each time that user changes color) and sets mask color.
The call to When user clicks on down arrow of color button in demo app mainfrm.cpp, the message map entry ON_NOTIFY(TBN_DROPDOWN, ID_VIEW_FORMATBAR, OnColorDropDown)
calls //============================================================================= void CMainFrame::OnColorDropDown(NMTOOLBAR *pNMTOOLBAR, LRESULT * /*pResult*/) //============================================================================= { if (pNMTOOLBAR && (pNMTOOLBAR->iItem == ID_SET_TEXT_COLOR)) { TRACE(_T("OnColorDropDown - click on color button drop-down arrow\n")); // get color of color button (bar under letter A) COLORREF rgb = m_wndFormatBar.GetColorButton(); CColorDialog dlg(rgb); // display color picker dialog if (dlg.DoModal() == IDOK) { rgb = dlg.GetColor(); // update color of color button (bar under letter A) m_wndFormatBar.SetColorButton(ID_SET_TEXT_COLOR, rgb); } } } When user clicks on left side of color button in demo app mainfrm.cpp, the message map entry ON_COMMAND(ID_SET_TEXT_COLOR, OnTextColor)
calls //============================================================================= void CMainFrame::OnTextColor() //============================================================================= { TRACE(_T("OnTextColor - click on color button left side\n")); // get color of color button (bar under letter A) COLORREF rgb = m_wndFormatBar.GetColorButton(); CWordPadView * pView = (CWordPadView *) GetActiveView(); // set new text color in current view if (pView) pView->SetColor(rgb); }
Color Button Tooltip
If you want to display tooltip for color button, take a look at
XTrueColorToolBar Implementation
The class
How To UseFollow these steps to integrate XTrueColorToolBar into your app:
Step 1: Add XTrueColorToolBar FilesAdd following files to your project:
NOTE: For the above .cpp file, you must select
Using precompiled headers in Visual Studio.
Step 2: Create True-color Toolbar with (Optional) Color ButtonI used MS Paint to replace existing color button image in demo app toolbar's bitmap. I also used Paint to convert bitmap to 24 bpp:
Step 3: Define CXTrueColorToolBar Toolbar
Usually in SDI or MDI app you will find toolbar declared in mainfrm.h.
In the demo app, you will see CFormatBar m_wndFormatBar;I changed class CFormatBar (declared in formatba.h) to the following:
class CFormatBar : public CXTrueColorToolBarIn this case, the header file XTrueColorToolBar.h is included in formatba.h.
Step 4: Initialize CXTrueColorToolBar Toolbar
As shown above in
Loading the True-color Toolbar and
Setting up the Color Button,
you should initialize
SummaryXTrueColorToolBar provides easy way to transition GUI to new MFC UI controls provided in VS2008, which will fully support Office-style color picker with split button:
References
Revision HistoryVersion 1.1 - 2007 December 28
Version 1.0 - 2007 December 17
UsageThis software is released under The Code Project Open License (CPOL). You are free to use this
software in any way you like, except that you may not sell this source code.
If you modify it or extend it, please to consider posting new code here for
everyone to share. This software is provided "as is" with no expressed or
implied warranty. I accept no liability for any damage or loss of business that
this software may cause.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||