Tutorials
|
|
Menu of tutorialsTutorial 1: The Simplest Window
Tutorial 7: Customising the ToolBarOur frame window has both a menu and a toolbar at the top. Customising the menu is relatively straight forward, since we can use a resource editor to perform that task. Customising the toolbar however is another matter. While its true that the resource editor that ships Microsoft's Visual Studio can edit the toolbar resource, this is not standard, and we so need to come up with a standards compliant way of modifying the toolbar. To set your own toolbar for your applications, you will need to perform the following steps:
void CMainFrame::SetupToolBar() { // Define our toolbar. AddToolBarButton( IDM_FILE_NEW ); AddToolBarButton( IDM_FILE_OPEN ); AddToolBarButton( IDM_FILE_SAVE ); AddToolBarButton( 0 ); // Separator AddToolBarButton( IDM_EDIT_CUT, FALSE ); AddToolBarButton( IDM_EDIT_COPY, FALSE ); AddToolBarButton( IDM_EDIT_PASTE, FALSE ); AddToolBarButton( IDM_FILE_PRINT ); AddToolBarButton( 0 ); // Separator AddToolBarButton( IDM_PEN_COLOR ); AddToolBarButton( 0 ); // Separator AddToolBarButton( IDM_HELP_ABOUT ); // Note: By default a single bitmap with a resource ID of IDW_MAIN and // a color mask of RGB(192,192,192) is used for the ToolBar. // The color mask is a color used for transparency. } Selecting our Pen ColorWindows provides a ChooseColor function. This function displays a dialog and allows us to select a color for our pen. The code that uses ChooseColor is as follows: void CMainFrame::OnPenColor() { // An array of custom colors, initialized to white. static COLORREF custColors[16] = { RGB(255,255,255), RGB(255,255,255), RGB(255,255,255), RGB(255,255,255), RGB(255,255,255), RGB(255,255,255), RGB(255,255,255), RGB(255,255,255), RGB(255,255,255), RGB(255,255,255), RGB(255,255,255), RGB(255,255,255), RGB(255,255,255), RGB(255,255,255), RGB(255,255,255), RGB(255,255,255) }; CColorDialog colorDlg; ColorDlg.SetCustomColors(custColors); // Initialize the Choose Color dialog. if (colorDlg.DoModal(*this) == IDOK) { // Store the custom colors in the static array. memcpy(custColors, colorDlg.GetCustomColors(), 16*sizeof(COLORREF)); // Retrieve the chosen color. m_view.SetPenColor(colorDlg.GetColor()); } } The source code for this tutorial is located within the Tutorial folder of the software available from SourceForge at http://sourceforge.net/projects/win32-framework. |