📄 ch12.htm
字号:
18: // Add the color toolbar
19: int iTBCtlID;
20: int i;
21:
22: // Create the Color Toolbar
23: if (!m_wndColorBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD |
24: WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS |
25: CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
26: !m_wndColorBar.LoadToolBar(IDR_TBCOLOR))
27: {
28: TRACE0("Failed to create toolbar\n");
29: return -1; // fail to create
30: }
31: // Find the Black button on the toolbar
32: iTBCtlID = m_wndColorBar.CommandToIndex(ID_COLOR_BLACK);
33: if (iTBCtlID >= 0)
34: {
35: // Loop through the buttons, setting them to act as radio Âbuttons
36: for (i= iTBCtlID; i < (iTBCtlID + 8); i++)
37: m_wndColorBar.SetButtonStyle(i, TBBS_CHECKGROUP);
38: }
39:
40: ///////////////////////
41: // MY CODE ENDS HERE
42: ///////////////////////
43:
44: if (!m_wndStatusBar.Create(this) ||
45: !m_wndStatusBar.SetIndicators(indicators,
46: sizeof(indicators)/sizeof(UINT)))
47: {
48: TRACE0("Failed to create status bar\n");
49: return -1; // fail to create
50: }
51:
52: // TODO: Delete these three lines if you don't want the toolbar to
53: // be dockable
54: m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
55:
56: ///////////////////////
57: // MY CODE STARTS HERE
58: ///////////////////////
59:
60: // Enable docking for the Color Toolbar
61: m_wndColorBar.EnableDocking(CBRS_ALIGN_ANY);
62:
63: ///////////////////////
64: // MY CODE ENDS HERE
65: ///////////////////////
66:
67: EnableDocking(CBRS_ALIGN_ANY);
68: DockControlBar(&m_wndToolBar);
69:
70: ///////////////////////
71: // MY CODE STARTS HERE
72: ///////////////////////
73:
74: // Dock the Color Toolbar
75: DockControlBar(&m_wndColorBar);
76:
77: ///////////////////////
78: // MY CODE ENDS HERE
79: ///////////////////////
80:
81: return 0;
82: }
</PRE>
<H4>Creating the Toolbar</H4>
<P>The first part of the code you added,</P>
<P>
<PRE>if (!m_wndColorBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD |
WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS |
CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndColorBar.LoadToolBar(IDR_TBCOLOR))
</PRE>
<P>contains two separate functions that are necessary in creating a toolbar. The
first function, CreateEx, creates the toolbar itself, whereas the second, LoadToolBar,
loads the toolbar that you designed in the toolbar designer. The second function,
LoadToolBar, requires a single argument, the ID for the toolbar that you want to
create.</P>
<P>The CreateEx function has several arguments that you can pass with the function.
The first argument, and the only required argument, is a pointer to the parent window.
In this case (which is normally the case), this argument is a pointer to the frame
window to which the toolbar will be attached.</P>
<P>The second argument is the style of controls on the toolbar that is to be created.
Several toolbar control styles are available for use, some of which have been introduced
with the last two versions of Internet Explorer. Table 12.1 lists the available styles.</P>
<P>
<H4>TABLE 12.1. TOOLBAR CONTROL STYLES.</H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"><I>Style</I></TD>
<TD ALIGN="LEFT"><I>Description</I></TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">TBSTYLE_ALTDRAG </TD>
<TD ALIGN="LEFT">Allows the user to move the toolbar by dragging it while holding down the Alt key. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">TBSTYLE_CUSTOMERASE </TD>
<TD ALIGN="LEFT">Generates a NM_CUSTOMDRAW message when erasing the toolbar and button background,
allowing the programmer to choose when and whether to control the background erasing
process. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">TBSTYLE_FLAT </TD>
<TD ALIGN="LEFT">Creates a flat toolbar. Button text appears under the bitmap image. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">TBSTYLE_LIST </TD>
<TD ALIGN="LEFT">Button text appears to the right of the bitmap image. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">TBSTYLE_REGISTERDROP </TD>
<TD ALIGN="LEFT">For use in dragging and dropping objects onto toolbar buttons. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">TBSTYLE_TOOLTIPS </TD>
<TD ALIGN="LEFT">Creates a tooltip control that can be used to display descriptive text for the buttons. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">TBSTYLE_TRANSPARENT </TD>
<TD ALIGN="LEFT">Creates a transparent toolbar. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">TBSTYLE_WRAPABLE </TD>
<TD ALIGN="LEFT">Creates a toolbar that can have multiple rows of buttons. </TD>
</TR>
</TABLE>
</P>
<P>The third argument is the style of the toolbar itself. This argument is normally
a combination of window and control bar styles. Normally, only two or three window
styles are used, and the rest of the toolbar styles are control bar styles. The list
of the normally used toolbar styles appears in Table 12.2.</P>
<P>
<H4>TABLE 12.2. TOOLBAR STYLES.</H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"><I>Style</I></TD>
<TD ALIGN="LEFT"><I>Description</I></TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">WS_CHILD </TD>
<TD ALIGN="LEFT">The toolbar is created as a child window. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">WS_VISIBLE </TD>
<TD ALIGN="LEFT">The toolbar will be visible when created. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CBRS_ALIGN_TOP </TD>
<TD ALIGN="LEFT">Allows the toolbar to be docked to the top of the view area of the frame window. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CBRS_ALIGN_BOTTOM </TD>
<TD ALIGN="LEFT">Allows the toolbar to be docked to the bottom of the view area of the frame window. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CBRS_ALIGN_LEFT </TD>
<TD ALIGN="LEFT">Allows the toolbar to be docked to the left side of the view area of the frame window. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CBRS_ALIGN_RIGHT </TD>
<TD ALIGN="LEFT">Allows the toolbar to be docked to the right side of the view area of the frame window. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CBRS_ALIGN_ANY </TD>
<TD ALIGN="LEFT">Allows the toolbar to be docked to any side of the view area of the frame window. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CBRS_BORDER_TOP </TD>
<TD ALIGN="LEFT">Places a border on the top edge of the toolbar when the top of the toolbar is not
docked. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CBRS_BORDER_BOTTOM </TD>
<TD ALIGN="LEFT">Places a border on the bottom edge of the toolbar when the top of the toolbar is
not docked. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CBRS_BORDER_LEFT </TD>
<TD ALIGN="LEFT">Places a border on the left edge of the toolbar when the top of the toolbar is not
docked. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CBRS_BORDER_RIGHT </TD>
<TD ALIGN="LEFT">Places a border on the right edge of the toolbar when the top of the toolbar is not
docked. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CBRS_FLOAT_MULTI </TD>
<TD ALIGN="LEFT">Allows multiple toolbars to be floated in a single miniframe window. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CBRS_TOOLTIPS </TD>
<TD ALIGN="LEFT">Causes tooltips to be displayed for the toolbar buttons. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CBRS_FLYBY </TD>
<TD ALIGN="LEFT">Causes status bar message text to be updated for the toolbar buttons at the same
time as the tooltips. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">CBRS_GRIPPER </TD>
<TD ALIGN="LEFT">Causes a gripper to be drawn on the toolbar. </TD>
</TR>
</TABLE>
</P>
<P>The fourth argument, which you did not provide in your code, is the size of the
toolbar borders. This argument is passed as a standard CRect rectangle class to provide
the length and height desired for the toolbar. The default value is 0 for all of
the rectangle dimensions, thus resulting in a toolbar with no borders.</P>
<P>The fifth and final argument, which you also did not provide in your code, is
the toolbar's child window ID. This defaults to AFX_IDW_TOOLBAR, but you can specify
any defined ID that you need or want to use for the toolbar.</P>
<P>
<H4>Setting the Button Styles</H4>
<P>After you create the toolbar, there is a curious bit of code:</P>
<P>
<PRE>// Find the Black button on the toolbar
iTBCtlID = m_wndColorBar.CommandToIndex(ID_COLOR_BLACK);
if (iTBCtlID >= 0)
{
// Loop through the buttons, setting them to act as radio buttons
for (i= iTBCtlID; i < (iTBCtlID + 8); i++)
m_wndColorBar.SetButtonStyle(i, TBBS_CHECKGROUP);
}
</PRE>
<P>The first line in this code snippet uses the CommandToIndex toolbar function to
locate the control number of the ID_COLOR_BLACK button. If you design your toolbar
in the order of colors that you used on the menu, this should be the first control,
with a index of 0. It's best to use the CommandToIndex function to locate the index
of any toolbar button that you need to alter, just in case it's not where you expect
it to be. This function returns the index of the toolbar control specified, and you
use this as a starting point to specify the button style of each of the color buttons.</P>
<P>In the loop, where you are looping through each of the eight color buttons on
the toolbar, you use the SetButtonStyle function to control the behavior of the toolbar
buttons. The first argument to this function is the index of the button that you
are changing. The second argument is the style of button that you want for the toolbar
button specified. In this case, you are specifying that each of the buttons be TBBS_CHECKGROUP
buttons, which makes them behave like radio buttons, where only one of the buttons
in the group can be selected at any time. The list of the available button styles
is in Table 12.3.</P>
<P>
<H4>TABLE 12.3. TOOLBAR BUTTON STYLES.</H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"><I>Style</I></TD>
<TD ALIGN="LEFT"><I>Description</I></TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">TBSTYLE_AUTOSIZE </TD>
<TD ALIGN="LEFT">The button's width will be calculated based on the text on the button. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">TBSTYLE_BUTTON </TD>
<TD ALIGN="LEFT">Creates a standard push button. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">TBSTYLE_CHECK </TD>
<TD ALIGN="LEFT">Creates a button that acts like a check box, toggling between the pressed and unpressed
state. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">TBSTYLE_CHECKGROUP </TD>
<TD ALIGN="LEFT">Creates a button that acts like a radio button, remaining in the pressed state until
another button in the group is pressed. This is actually the combination of the TBSTYLE_CHECK
and TBSTYLE_GROUP button styles. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">TBSTYLE_DROPDOWN </TD>
<TD ALIGN="LEFT">Creates a drop-down list button. </TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">TBSTYLE_GROUP </TD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -