Tutorials

 

Home
About
How it Works
Documentation
Tutorials

Examples

Why not MFC
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: 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 which 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 );
}
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()
{
  // 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.