Tutorials

 

Home
About
How it Works
Documentation
Tutorials

Examples

Links
Contact

 

 

Menu of tutorials

Tutorial 1:   The Simplest Window
Tutorial 2:   Using Classes and Inheritance
Tutorial 3:   Using Messages to Create a Scribble Window
Tutorial 4:   Repainting the Window
Tutorial 5:   Wrapping a Frame around our Scribble Window
Tutorial 6:   Customising Window Creation
Tutorial 7:   Customising the ToolBar
Tutorial 8:   Loading and Saving Files
Tutorial 9:   Printing
Tutorial 10: Print Previewing
Tutorial 11: Finishing Touches

Tutorial 7:  Customising the ToolBar

Our 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:

  • Modify the toolbar bitmap.
  • Add the toolbar resource IDs to to the string table.
  • Assign resource IDs to the toolbar buttons, as shown below.
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 Color

Windows 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.