⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch12.htm

📁 有关Visual C++ 6.0 编程实例与技巧方面的相关资料
💻 HTM
📖 第 1 页 / 共 3 页
字号:
45:         !m_wndStatusBar.SetIndicators(indicators,46:           sizeof(indicators)/sizeof(UINT)))47:     {48:         TRACE0(&quot;Failed to create status bar\n&quot;);49:         return -1;      // fail to create50:     }51: 52:     // TODO: Delete these three lines if you don't want the toolbar to53:     //  be dockable54:     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);55: 56:     ///////////////////////57:     // MY CODE STARTS HERE58:     ///////////////////////59: 60:     // Enable docking for the Color Toolbar61:     m_wndColorBar.EnableDocking(CBRS_ALIGN_ANY);62: 63:     ///////////////////////64:     // MY CODE ENDS HERE65:     ///////////////////////66: 67:     EnableDocking(CBRS_ALIGN_ANY);68:     DockControlBar(&amp;m_wndToolBar);69: 70:     ///////////////////////71:     // MY CODE STARTS HERE72:     ///////////////////////73: 74:     // Dock the Color Toolbar75:     DockControlBar(&amp;m_wndColorBar);76: 77:     ///////////////////////78:     // MY CODE ENDS HERE79:     ///////////////////////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. Thefirst 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 tocreate.</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 framewindow 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 introducedwith 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 normallya combination of window and control bar styles. Normally, only two or three windowstyles are used, and the rest of the toolbar styles are control bar styles. The listof 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 thetoolbar borders. This argument is passed as a standard CRect rectangle class to providethe length and height desired for the toolbar. The default value is 0 for all ofthe 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, isthe toolbar's child window ID. This defaults to AFX_IDW_TOOLBAR, but you can specifyany 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 toolbariTBCtlID = m_wndColorBar.CommandToIndex(ID_COLOR_BLACK);if (iTBCtlID &gt;= 0){    // Loop through the buttons, setting them to act as radio buttons    for (i= iTBCtlID; i &lt; (iTBCtlID + 8); i++)        m_wndColorBar.SetButtonStyle(i, TBBS_CHECKGROUP);}</PRE><P>The first line in this code snippet uses the CommandToIndex toolbar function tolocate the control number of the ID_COLOR_BLACK button. If you design your toolbarin 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 indexof any toolbar button that you need to alter, just in case it's not where you expectit to be. This function returns the index of the toolbar control specified, and youuse 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 onthe toolbar, you use the SetButtonStyle function to control the behavior of the toolbarbuttons. The first argument to this function is the index of the button that youare changing. The second argument is the style of button that you want for the toolbarbutton specified. In this case, you are specifying that each of the buttons be TBBS_CHECKGROUPbuttons, which makes them behave like radio buttons, where only one of the buttonsin the group can be selected at any time. The list of the available button stylesis 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>		<TD ALIGN="LEFT">Creates a button that remains pressed until another button in the group is pressed.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">TBSTYLE_NOPREFIX		</TD>		<TD ALIGN="LEFT">The button text will not have an accelerator prefix associated with it.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">TBSTYLE_SEP		</TD>		<TD ALIGN="LEFT">Creates a separator, making a small gap between the buttons on either side.		</TD>	</TR></TABLE><H4>Docking the Toolbar</H4><P>The last thing that you do in the code that you add to the OnCreate function inthe CMainFrame class is the following:</P><P><PRE>// Enable docking for the Color Toolbarm_wndColorBar.EnableDocking(CBRS_ALIGN_ANY);EnableDocking(CBRS_ALIGN_ANY);  // (AppWizard generated line)// Dock the Color ToolbarDockControlBar(&amp;m_wndColorBar);</PRE><P>In the first of these lines, you called the EnableDocking toolbar function. Thisfunction enables the toolbar for docking with the frame window. The value passedto this toolbar function must match the value passed in the following EnableDockingfunction that is called for the frame window. The available values for these functionsare listed in Table 12.4. These functions enable the borders of the toolbar, andthe frame window, for docking. If these functions are not called, then you will notbe able to dock the toolbar with the frame window. If a specific side is specifiedin these functions for use in docking, and the sides do not match, you will not beable to dock the toolbar with the frame.</P><P><H4>TABLE 12.4. TOOLBAR DOCKING SIDES.</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">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>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -