Creating the Sample's Toolbar and Combo Boxes

I created a toolbar for the RICHED sample by calling the CreateToolbarEx function. (For a detailed discussion of toolbars, refer to Chapter 1.) I specified a TBBUTTON structure containing information about the toolbar's buttons. Buttons on a toolbar are “free”—that is, you don't need to do anything special to include them other than filling out the structure and giving the CreateToolbarEx function a pointer to that structure.

Other controls on a toolbar, such as the combo boxes I used, require a bit more work. To reserve space on the toolbar for the controls, the application must place separators in the TBBUTTON structure where these extra controls will reside. If you are creating a static structure to hold the buttons, you can determine heuristically how many separators to use (try it out to see what looks good). If you are creating your toolbar dynamically, you can send the TB_GETITEMRECT message to determine the width of a separator and then use the values returned (rect.right, rect.left) to calculate how many separators you need to add. The application then creates the control and parents it to the toolbar. To include ToolTips for the various controls and buttons, the application uses the TTM_ADDTOOL message to add ToolTip support.

HWND InitToolbar (HWND hWndParent)
{
TOOLINFO lpToolInfo;
HWND hWndToolbar, hWndTT;
HFONT hFont;

// Create the toolbar control.
hWndToolbar = CreateToolbarEx (
hWndParent, // parent
WS_CHILD | WS_BORDER | WS_VISIBLE | TBSTYLE_TOOLTIPS, // style
IDB_TOOLBAR, // toolbar ID
6, // number of bitmaps
hInst, // mod instance
IDB_TOOLBAR, // resource ID for bitmap
(LPCTBBUTTON)&tbButtons, // address of buttons
34, // number of buttons
16, 16, // width & height of buttons
16, 16, // width & height of bitmaps
sizeof (TBBUTTON)); // structure size

if (hWndToolbar == NULL)
{
MessageBox (NULL, "Toolbar not created!", NULL, MB_OK);
return NULL;
}

// Create the combo box for the typeface.
hWndComboFont = CreateWindowEx (0L, // no extended styles
"COMBOBOX", // class name
"", // default text
WS_CHILD | WS_BORDER | WS_VISIBLE |
CBS_HASSTRINGS | CBS_DROPDOWN, // window styles
0, 3, 150, 250, // size and position
hWndToolbar, // parent window
(HMENU)IDM_COMBOFONT, // ID
hInst, // current instance
NULL); // no class data

// Set the window procedure for the combo box.
lpfnDefComboFont = (WNDPROC) GetWindowLong (hWndComboFont, GWL_WNDPROC);
SetWindowLong (hWndComboFont, GWL_WNDPROC, (LONG)ComboWndProcFont);

// Create the combo box for the point size.
hWndComboSize = CreateWindowEx (0L, // no extended styles
"COMBOBOX", // class name
"", // default text
WS_CHILD | WS_BORDER | WS_VISIBLE |
CBS_HASSTRINGS | CBS_DROPDOWN, // window styles
160, 3, 50, 250, // size and position
hWndToolbar, // parent window
(HMENU)IDM_COMBOSIZE, // ID
hInst, // current instance
NULL); // no class data

// Set the window procedure for the combo box.
lpfnDefComboSize = (WNDPROC) GetWindowLong (hWndComboSize, GWL_WNDPROC);
SetWindowLong (hWndComboSize, GWL_WNDPROC, (LONG)ComboWndProcSize);

// Get the handle to the ToolTip window.
hWndTT = (HWND) SendMessage (hWndToolbar, TB_GETTOOLTIPS, 0, 0);

if (hWndTT)
{
// Fill out the TOOLINFO structure.
lpToolInfo.cbSize = sizeof (lpToolInfo);
lpToolInfo.uFlags = TTF_IDISHWND | TTF_CENTERTIP;
lpToolInfo.lpszText = (LPSTR)IDM_COMBOFONT;
lpToolInfo.hwnd = hWndParent;
lpToolInfo.uId = (UINT)hWndComboFont;
lpToolInfo.hinst = hInst;
// Set up ToolTips for the typeface combo box.
SendMessage (hWndTT, TTM_ADDTOOL, 0,
(LPARAM)(LPTOOLINFO)&lpToolInfo);

lpToolInfo.lpszText = (LPSTR)IDM_COMBOSIZE;
lpToolInfo.uId = (UINT)hWndComboSize;
// Set up ToolTips for the point size combo box.
SendMessage (hWndTT, TTM_ADDTOOL, 0,
(LPARAM)(LPTOOLINFO)&lpToolInfo);
}
else
MessageBox (NULL, "Could not get ToolTip window handle.", NULL,
MB_OK);

// Set the fonts for the combo boxes on the toolbar.
hFont = (HFONT) SendMessage (hWndToolbar, WM_GETFONT, 0, 0);
SendMessage (hWndComboFont, WM_SETFONT, (WPARAM)hFont, 0);
SendMessage (hWndComboSize, WM_SETFONT, (WPARAM)hFont, 0);

return hWndToolbar;
}