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

📄 ch04.htm

📁 24小时学会vc++
💻 HTM
📖 第 1 页 / 共 3 页
字号:
operands, the result will be zero. For example, this expression is equal to 7:</P>
<PRE><FONT COLOR="#0066FF"><TT>4 | 3</TT>
</FONT></PRE>
<P>However, the result of the following is also equal to 7:</P>
<PRE><FONT COLOR="#0066FF"><TT>4 | 7</TT>
</FONT></PRE>
<P>Each possible parameter has a unique bit pattern, enabling you to use the bitwise
<TT>OR</TT> operator when you combine parameter values in <TT>AfxMessageBox</TT>
or other MFC function calls.</P>
<P>When using the bitwise <TT>OR</TT> operator with <TT>AfxMessageBox</TT>, you can
combine one icon style and one button style. You can't combine two icon styles or
two button styles. If no styles are provided, the message box will contain the exclamation-mark
icon and an OK button.
<H3><FONT COLOR="#000077"><B>Adding a Dialog Box</B></FONT></H3>
<P>Adding a dialog box to a program usually takes four steps:

<DL>
	<DD>1. Design and create a dialog box resource using the Developer Studio resource
	tools.<BR>
	<BR>
	2. Use ClassWizard to create a C++ class derived from <TT>CDialog</TT> that will
	manage the dialog box.<BR>
	<BR>
	3. Add functions to handle messages sent to the dialog box, if needed.<BR>
	<BR>
	4. If the dialog box is selected from the main menu, the menu resource must be modified
	and message-handling functions must be created using ClassWizard.
</DL>

<P>Each of these steps is covered in the following sections.
<H4><FONT COLOR="#000077">Understanding Resources</FONT></H4>
<P>Dialog boxes are just specialized types of windows. However, because they commonly
are used for short periods of time they usually are stored as program resources and
loaded only when needed. You can see this behavior when running a Windows program
on a machine that has little free memory. Every time a dialog box is opened, the
hard disk is accessed to load the dialog box resources from the EXE.</P>
<P>Menus and accelerators, which are covered in Hour 10, &quot;Menus,&quot; are two
types of resources. Here are some of the other resource types used by Windows programs:

<UL>
	<LI><I>Bitmaps</I> store images such as the logo from the Visual C++ opening, or
	splash screen. You will learn more about bitmaps in Hour 15, &quot;Using Bitmaps.&quot;<BR>
	<BR>
	
	<LI><I>Cursors</I> indicate the current mouse position. A program can modify the
	cursor to indicate that a specific action can be taken with the mouse at its current
	position, or for other user-feedback purposes. You will learn more about cursors
	in Hour 14, &quot;Icons and Cursors.&quot;<BR>
	<BR>
	
	<LI><I>Dialog boxes</I> are windows used to interact with a program's user. The message
	box covered earlier in this hour is one example of a dialog box.<BR>
	<BR>
	
	<LI><I>Icons</I> are small bitmaps in a special format that can be used to represent
	another object in a Windows program. For example, icons are often used to represent
	programs and documents that aren't currently visible.
</UL>

<P>In this hour you will learn how to create and use dialog box resources. Later
hours deal with the other resource types.
<H4><FONT COLOR="#000077">Creating a Dialog Box Resource Using Developer Studio</FONT></H4>
<P>Developer Studio enables you to create a dialog box and configure it visually.
You can add and size controls by using a mouse. You can set attributes for the dialog
box and its controls with a click of a mouse button.</P>
<P>Before using the following steps, create a Single Document MFC AppWizard application
named HelloSDI, following the steps presented in the first hour. Create a new dialog
box resource for the HelloSDI project using either of the following methods:

<UL>
	<LI>Select Resource from the Insert menu, then select Dialog as the resource type.<BR>
	<BR>
	
	<LI>Right-click the Dialog folder in the Resource tree, and select Insert Dialog
	from the pop-up menu.
</UL>

<P>With either of these methods the dialog box editor is displayed, as shown in Figure
4.2.</P>
<P><A NAME="02"></A><A HREF="02.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch04/02.htm"><B>Figure 4.2.</B></A> <I><BR>
The Developer Studio dialog box editor.</I></P>
<P>The dialog box that is displayed for editing initially contains two button controls,
one labeled OK and another labeled Cancel. As you will learn in Hour 5, &quot;Button
Controls,&quot; these are two standard dialog box controls. The MFC class library
usually handles the operation of these controls.
<H4><FONT COLOR="#000077">Customizing the Dialog Box's Properties</FONT></H4>
<P>Every dialog box has properties that you can display by right-clicking and selecting
Properties from the pop-up menu. Here are the dialog box properties under the tab
labeled General:

<UL>
	<LI>ID: Normally set to something like <TT>IDD_DIALOG1</TT>. Naming dialog boxes
	with an identifier that begins with <TT>IDD_</TT> is an MFC convention, although
	you should try to name your dialog boxes with a more meaningful name; rename this
	dialog box <TT>IDD_HELLO</TT>.<BR>
	<BR>
	
	<LI>Caption: Normally set to a default of <TT>Dialog</TT>. You should change this
	to something more meaningful as well, such as <TT>Hello</TT> for the sample dialog
	box.<BR>
	<BR>
	
	<LI>Menu: Normally cleared because few dialog boxes use a menu.<BR>
	<BR>
	
	<LI>X Pos: Normally cleared to use the default positioning for the dialog box.<BR>
	<BR>
	
	<LI>Y Pos: Normally cleared to use the default positioning for the dialog box.<BR>
	<BR>
	
	<LI>Font Name: Contains the current font used by the dialog box.<BR>
	<BR>
	
	<LI>Font Size: Contains the current font size used by the dialog box.
</UL>



<BLOCKQUOTE>
	<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>There is also a pushbutton
	labeled Font... that you can use to change the default font for the dialog box. However,
	just because you can doesn't mean that you should. Windows enables users to set the
	font style used in dialog boxes; many users, such as the visually impaired, might
	need specific fonts to be able to use your dialog box. 
<HR>


</BLOCKQUOTE>

<P>Like all windows, a dialog box has several style attributes. You can display these
attributes by selecting the tab labeled Styles. Here are the default values for the
following attributes:

<UL>
	<LI>Style: Usually set to <TT>Popup</TT> for most dialog boxes. In the case of special
	dialog box templates used in form views or dialog bars, the style is set to <TT>Child</TT>.<BR>
	<BR>
	
	<LI>Border: Set to <TT>Dialog Frame</TT> for most dialog boxes.<BR>
	<BR>
	
	<LI>Minimize Box: Creates a minimize box for the dialog box. This check box is cleared
	for most dialog boxes, indicating that no minimize box is provided.<BR>
	<BR>
	
	<LI>Maximize Box: Used to create a maximize box for the dialog box. This check box
	is cleared for most dialog boxes, indicating that no maximize box is provided.<BR>
	<BR>
	
	<LI>Title Bar: Creates a title bar for the dialog box. This check box is almost always
	checked because most dialog boxes have a title bar.<BR>
	<BR>
	
	<LI>System Menu: Used to indicate that a system menu should be provided for the dialog
	box. This check box is normally checked.<BR>
	<BR>
	
	<LI>Horizontal Scroll: Used to create a scrollbar for the dialog box. This check
	box is almost always cleared because dialog boxes rarely use scrollbars.<BR>
	<BR>
	
	<LI>Vertical scroll: Used to create a vertical scrollbar for the dialog box. Like
	the horizontal scrollbar, this attribute is rarely used.<BR>
	<BR>
	
	<LI>Clip Siblings: Used only with child windows. This check box is normally cleared.<BR>
	<BR>
	
	<LI>Clip Children: Used for parent windows. This check box is rarely checked for
	most dialog boxes.
</UL>

<P>The tab labeled More Styles contains additional properties for the dialog box:

<UL>
	<LI>System Modal: Creates a system-modal dialog box. If this option is enabled, the
	user cannot switch to another program.<BR>
	<BR>
	
	<LI>Absolute Align: Used to indicate how the dialog box is positioned when initially
	displayed. If this check box is checked, the dialog box is aligned with the screen
	instead of with the parent window.<BR>
	<BR>
	
	<LI>No Idle Message: Prevents a particular window message, <TT>WM_ENTERIDLE</TT>,
	from being sent when the dialog box's message queue is empty. This check box is normally
	cleared.<BR>
	<BR>
	
	<LI>Local Edit: Used to specify how an edit control's memory is allocated. This check
	box is normally cleared, which means edit controls use memory outside the program's
	data segment.<BR>
	<BR>
	
	<LI>Visible: Used to specify that the dialog box should be visible when first displayed.
	This check box is usually checked. In the case of form views, this check box is cleared.
	Form views are discussed in Hour 23, &quot;Advanced Views.&quot;<BR>
	<BR>
	
	<LI>Disabled: Indicates that the dialog box should be disabled when initially displayed.
	This check box is usually cleared.<BR>
	<BR>
	
	<LI>3D-Look: Gives the dialog box a three-dimensional appearance. This check box
	is usually cleared.<BR>
	<BR>
	
	<LI>Set Foreground: Forces the dialog box to be placed into the foreground. This
	check box is usually cleared.<BR>
	<BR>
	
	<LI>No Fail Create: Tells Windows to create the dialog box even if an error occurs.
	This check box is usually cleared.<BR>
	<BR>
	
	<LI>Control: Creates a dialog box resource that can be used as a child control. This
	check box is usually cleared.<BR>
	<BR>
	
	<LI>Center: Causes the dialog box to be centered when it is initially displayed.
	This check box is usually cleared.<BR>
	<BR>
	
	<LI>Center Mouse: Places the mouse cursor in the center of the dialog box. This check
	box is usually cleared.<BR>
	<BR>
	
	<LI>Context Help: Adds a question mark icon for context-sensitive help in the title
	bar. This check box is usually cleared.
</UL>

<P>Advanced styles are located under the tab labeled Extended Styles. These styles
are rarely used and aren't discussed in this book.
<H3><FONT COLOR="#000077"><B>Adding a Static Text Control</B></FONT></H3>
<P>A simple control that you can add to the dialog box is a static text control.
The static text control requires no interaction with the dialog box; it is often
used as a plain text label for other controls contained by the dialog box. To add
a static text control, follow these steps:

<DL>
	<DD>1. Select the Static Text control icon on the control toolbar. The cursor changes
	shape to a plus sign when moved over the dialog box.<BR>
	<BR>
	2. Center the cursor over the dialog box, and click the left mouse button. A static
	text control is created and contains the label Static.<BR>
	<BR>
	3. Change the label of the static text control by right-clicking the control and
	selecting Properties from the shortcut menu; change the caption to &quot;Hello World&quot;.
</DL>

<P>The static text control is visible whenever the dialog box is displayed. Text
controls are an excellent choice for labeling controls or messages that are not likely
to change. Experiment with changing the size and position of the static text control
by dragging its edges with the mouse.
<H3><FONT COLOR="#000077"><B>Creating a Class for the Dialog Box</B></FONT></H3>
<P>You can use the <TT>CDialog</TT> class to manage most of the interaction with
a dialog box in your program. The <TT>CDialog</TT> class provides member functions
that make a dialog box easy to use. You should use ClassWizard to derive a class
from <TT>CDialog</TT> that is specifically tailored for your dialog box.</P>
<P>To start ClassWizard, use any of these methods:

<UL>
	<LI>Press Ctrl+W almost any time in Developer Studio.<BR>
	<BR>
	
	<LI>Select ClassWizard from the View menu.<BR>
	<BR>
	
	<LI>Right-click anywhere in the dialog box editor, and select ClassWizard from the
	pop-up menu.
</UL>

<P>If ClassWizard knows that a new resource has been added, such as <TT>IDD_HELLO</TT>,
a dialog box asks you to choose between two options for the new dialog box resource:

<UL>
	<LI>Create a new class.
	<LI>Select an existing class.
</UL>

<P>You should almost always choose to create a new dialog box class unless you are
reusing some existing code. A New Class dialog box is displayed, as shown in Figure
4.3.</P>
<P>Values provided to the New Class dialog box are used by ClassWizard to create
a class that will manage the new dialog box resource. Use the values from Table 4.4
to fill in the values for the <TT>IDD_HELLO</TT> dialog box.
<H4><FONT COLOR="#000077">Table 4.4. Sample values for the New Class dialog box.</FONT></H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Control</B></TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><B>Value</B></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">Name</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>CHelloDlg</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">File Name</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>HelloDlg.cpp</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">Base Class</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>CDialog</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">Dialog ID</TD>
		<TD ALIGN="LEFT" VALIGN="TOP"><TT>IDD_HELLO</TT></TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">OLE Automation</TD>
		<TD ALIGN="LEFT" VALIGN="TOP">None</TD>
	</TR>
</TABLE>
</P>
<P><A NAME="03"></A><A HREF="03.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch04/03.htm"><B>Figure 4.3.</B></A> <I><BR>
The New Class dialog box.</I></P>
<P>Click the button labeled OK. The <TT>CHelloDlg</TT> class is generated, and two

⌨️ 快捷键说明

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