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

📄 ch12.htm

📁 VC 21天 学习VC 的好东西
💻 HTM
📖 第 1 页 / 共 5 页
字号:
string table editor.</I></P>

<P>For instance, to add the strings for the combo box that you will be adding to
the color toolbar, insert a new string, either by selecting Insert|New String from
the menu or by right-clicking the string table and selecting New String from the
pop-up menu.</P>
<P>In the String properties dialog, specify a string ID for the string and then enter
the string to appear in the drop-down list. Close the properties dialog to add the
string. For the strings in the Width combo box that you are going to add to the color
toolbar, add the strings in Table 12.6.</P>
<P>
<H4>TABLE 12.6. WIDTH TOOLBAR COMBO BOX STRINGS.</H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT"><I>ID</I></TD>
		<TD ALIGN="LEFT"><I>Caption</I></TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">IDS_WIDTH_VTHIN		</TD>
		<TD ALIGN="LEFT">Very Thin		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">IDS_WIDTH_THIN		</TD>
		<TD ALIGN="LEFT">Thin		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">IDS_WIDTH_MEDIUM		</TD>
		<TD ALIGN="LEFT">Medium		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">IDS_WIDTH_THICK		</TD>
		<TD ALIGN="LEFT">Thick		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">IDS_WIDTH_VTHICK		</TD>
		<TD ALIGN="LEFT">Very Thick		</TD>
	</TR>
</TABLE>

<H3><A NAME="Heading8"></A>Creating the Toolbar Combo Box</H3>
<P>Before you can add the combo box to the color toolbar, you need to create a combo
box variable that you can use for the combo box. Because you are not able to add
this combo box through any of the designers, you need to add it as a variable to
the CMainFrame class.</P>
<P>To add the combo box variable to the main frame class for the color toolbar, select
the Class View tab in the workspace pane. Right-click the CMainFrame class and select
Add Member Variable from the pop-up menu. Specify the variable type as CComboBox,
the name as m_ctlWidth, and the access as protected.</P>
<P>Once you add the combo box variable to the main frame class, you need to perform
a series of actions, all once the toolbar has been created:</P>
<P>

<DL>
	<DT></DT>
	<DD><B>1. </B>Set the width and the ID of the combo box place holder on the toolbar
	to the width and ID of the combo box.
	<P>
	<DT></DT>
	<DD><B>2. </B>Get the position of the toolbar placeholder and use it to size and
	position the combo box.
	<P>
	<DT></DT>
	<DD><B>3. </B>Create the combo box, specifying the toolbar as the parent window of
	the combo box.
	<P>
	<DT></DT>
	<DD><B>4. </B>Load the strings into the drop-down list on the combo box.
	<P>
</DL>

<P>To organize this so that it doesn't get too messy, it might be advisable to move
the creation of the color toolbar to its own function that can be called from the
OnCreate function of the main frame class. To create this function, right-click the
CMainFrame class in the workspace pane and select Add Member Function from the pop-up
menu. Specify the function type as BOOL, the function description as CreateColorBar,
and the access as public. Edit the new function, adding the code in Listing 12.5.</P>
<P>
<H4>LISTING 12.5. THE CMainFrame CreateColorBar FUNCTION.</H4>
<PRE> 1: BOOL CMainFrame::CreateColorBar()
 2: {
 3:     int iTBCtlID;
 4:     int i;
 5: 
 6:     if (!m_wndColorBar.CreateEx(this, TBSTYLE_FLAT,
                  &Acirc;WS_CHILD | WS_VISIBLE | CBRS_TOP
 7:         | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY |             &Acirc;CBRS_SIZE_DYNAMIC) ||
 8:         !m_wndColorBar.LoadToolBar(IDR_TBCOLOR))
 9:     {
10:         TRACE0(&quot;Failed to create toolbar\n&quot;);
11:         return FALSE;      // fail to create
12:     }
13:     iTBCtlID = m_wndColorBar.CommandToIndex(ID_COLOR_BLACK);
14:     if (iTBCtlID &gt;= 0)
15:     {
16:         for (i= iTBCtlID; i &lt; (iTBCtlID + 8); i++)
17:             m_wndColorBar.SetButtonStyle(i, TBBS_CHECKGROUP);
18:     }
19:     // Add the Combo
20:     int nWidth = 100;
21:     int nHeight = 125;
22: 
23:     // Configure the combo place holder
24:     m_wndColorBar.SetButtonInfo(9, IDC_CBWIDTH, TBBS_SEPARATOR,         &Acirc;nWidth);
25: 
26:     // Get the colorbar height
27:     CRect rect;
28:     m_wndColorBar.GetItemRect(9, &amp;rect);
29:     rect.bottom = rect.top + nHeight;
30: 
31:     // Create the combo box
32:     m_ctlWidth.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL |
33:         CBS_DROPDOWNLIST, rect, &amp;m_wndColorBar, IDC_CBWIDTH);
34: 
35:     //  Fill the combo box
36:     CString szStyle;
37:     if (szStyle.LoadString(IDS_WIDTH_VTHIN))
38:         m_ctlWidth.AddString((LPCTSTR)szStyle);
39:     if (szStyle.LoadString(IDS_WIDTH_THIN))
40:         m_ctlWidth.AddString((LPCTSTR)szStyle);
</PRE>
<PRE>41:     if (szStyle.LoadString(IDS_WIDTH_MEDIUM))
</PRE>
<PRE>42:         m_ctlWidth.AddString((LPCTSTR)szStyle);
43:     if (szStyle.LoadString(IDS_WIDTH_THICK))
44:         m_ctlWidth.AddString((LPCTSTR)szStyle);
45:     if (szStyle.LoadString(IDS_WIDTH_VTHICK))
46:         m_ctlWidth.AddString((LPCTSTR)szStyle);
47: 
48:     return TRUE;
49: }
</PRE>
<P>On line 24 in Listing 12.5, you specify that the combo box should be created using
the object ID IDC_CBWIDTH. This object ID is used to identify the combo box when
the combo box sends an event message to the application or when you need to specify
what list entry is displayed in the edit field. However, this object ID doesn't exist
in your application. Before you can compile the application, you'll need to add this
ID to the project resource IDs, just as you did on Day 4, &quot;Working with Timers.&quot;
To add this ID to your project, select the Resource view in the workspace pane. Select
the top of the resource tree and right-click the mouse to trigger the context menu.
Select Resource Symbols from the pop-up menu and add the object ID IDC_CBWIDTH. Make
sure that you add the new object ID with a unique numerical value so that it won't
conflict with any other objects in use in your application.</P>
<P>
<H4>Configuring the Placeholder</H4>
<P>After creating the toolbar and configuring all of the toolbar buttons, the first
thing you need to do is to configure the separator that is acting as the place holder
for the combo box you are about to create. You do this with the SetButtonInfo toolbar
function, as follows:</P>
<P>
<PRE>m_wndColorBar.SetButtonInfo(9, IDC_CBWIDTH, TBBS_SEPARATOR, nWidth);
</PRE>
<P>This function takes four arguments. The first argument is the current index of
the control in the toolbar--in this case, the tenth control in the toolbar (eight
color buttons and two separators). The second argument is the new ID of the toolbar
control. This is the ID that will be placed in the event message queue when a control
event occurs. The third argument is the type of toolbar control this control should
be. The fourth and final argument is somewhat deceptive. If you look at the function
documentation, the fourth argument is the new index of the control in the toolbar.
This is the position to which the control will be moved. However, if the control
is a separator, this argument specifies the width of the control and doesn't move
it anywhere. Because this toolbar control is a separator, this argument has the effect
of setting it to be as wide as the combo box that you are going to create.</P>
<P>
<H4>Getting the Toolbar Combo Box Position</H4>
<P>Now that you have configured the toolbar separator as the place holder for the
combo box, you need to get the position of the combo box place holder on the toolbar
so that you can use it to set the position of the combo box:</P>
<P>
<PRE>m_wndColorBar.GetItemRect(9, &amp;rect);
rect.bottom = rect.top + nHeight;
</PRE>
<P>In the first line, you called the toolbar function GetItemRect to get the position
and size of the placeholder for the combo box. In the next line, you added the height
of the drop-down list to the height that the combo box will eventually be.</P>
<P>
<H4>Creating the Combo Box</H4>
<P>Now that you've got a place holder sized correctly, and you have the position
and size for the combo box, it's time to create the combo box. You do this with the
Create combo box function, as follows:</P>
<P>
<PRE>m_ctlWidth.Create(WS_CHILD | WS_VISIBLE | WS_VSCROLL |
    CBS_DROPDOWNLIST, rect, &amp;m_wndColorBar, IDC_CBWIDTH);
</PRE>
<P>The first argument to the combo box Create function is the combo box style. Normally,
several style flags are combined to create a combination style value. Table 12.7
lists the flags that you can use in this value.</P>
<P>
<H4>TABLE 12.7. COMBO BOX 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">Designates this as a child window (required).		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">WS_VISIBLE		</TD>
		<TD ALIGN="LEFT">Makes the combo box visible.		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">WS_DISABLED		</TD>
		<TD ALIGN="LEFT">Disables the combo box.		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">WS_VSCROLL		</TD>
		<TD ALIGN="LEFT">Adds vertical scrolling to the drop-down list.		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">WS_HSCROLL		</TD>
		<TD ALIGN="LEFT">Adds horizontal scrolling to the drop-down list.		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">WS_GROUP		</TD>
		<TD ALIGN="LEFT">Groups controls.		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">WS_TABSTOP		</TD>
		<TD ALIGN="LEFT">Includes the combo box in the tabbing order.		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CBS_AUTOHSCROLL		</TD>
		<TD ALIGN="LEFT">Automatically scrolls the text in the edit control to the right when the user types
			a character at the end of the line. This allows the user to enter text wider than
			the edit control into the combo box.		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CBS_DROPDOWN		</TD>
		<TD ALIGN="LEFT">Similar to CBS_SIMPLE, but the list is not displayed unless the user selects the
			icon next to the edit control.		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CBS_DROPDOWNLIST		</TD>
		<TD ALIGN="LEFT">Similar to CBS_DROPDOWN, but the edit control is replaced with a static-text item
			displaying the currently selected item in the list.		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CBS_HASSTRINGS		</TD>
		<TD ALIGN="LEFT">The owner of the list box is responsible for drawing the list box contents. The list
			box items consist of strings.		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CBS_OEMCONVERT		</TD>
		<TD ALIGN="LEFT">Text entered in the edit control is converted from ANSI to the OEM character set
			and then back to ANSI.		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CBS_OWNERDRAWFIXED		</TD>
		<TD ALIGN="LEFT">The owner of the list box is responsible for drawing the list box contents. The contents
			of the list are fixed in height.		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CBS_OWNERDRAWVARIABLE		</TD>
		<TD ALIGN="LEFT">The owner of the list box is responsible for drawing the list box contents. The contents
			of the list are variable in height.		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CBS_SIMPLE		</TD>
		<TD ALIGN="LEFT">The list box is displayed at all times.		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CBS_SORT		</TD>
		<TD ALIGN="LEFT">Automatically sorts the strings in the list box.		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CBS_DISABLENOSCROLL		</TD>
		<TD ALIGN="LEFT">List shows a disabled scrollbar when there are not enough items in the list to require
			scrolling.		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CBS_NOINTEGRALHEIGHT		</TD>
		<TD ALIGN="LEFT">Specifies that the combo box is exactly the size specified.		</TD>
	</TR>
</TABLE>

⌨️ 快捷键说明

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