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

📄 ch05.htm

📁 24小时精通vc
💻 HTM
📖 第 1 页 / 共 3 页
字号:
dialog box is displayed, nothing happens when the buttons are used because no button
events are handled by the dialog box class.</P>
<P>Pushbuttons are normally associated with button events in a dialog box class.
To add a button event for <TT>IDC_BTN_TEST</TT>, follow these steps:

<DL>
	<DD>1. Open ClassWizard.<BR>
	<BR>
	2. Select the tab labeled Message Maps.<BR>
	<BR>
	3. Select <TT>CButtonDlg</TT> as the class name.<BR>
	<BR>
	4. Select <TT>IDC_BTN_TEST</TT> as the object ID.<BR>
	<BR>
	5. Select <TT>BN_CLICKED</TT> from the Messages list box.<BR>
	<BR>
	6. Press the button labeled Add Function and accept the default name for the member
	function.<BR>
	<BR>
	7. Close ClassWizard.
</DL>

<P>Check boxes and radio buttons sometimes use <TT>BN_CLICKED</TT> messages, but
not as often as pushbuttons. Add the source code from Listing 5.6 to the <TT>CButtonDlg::OnBtnTest</TT>
function, then compile and run the project.
<H4><FONT COLOR="#000077">TYPE: Listing 5.6. The CButtonDlg::OnBtnTest member function.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CButtonDlg::OnBtnTest()</TT>
<TT>{</TT>
<TT>    AfxMessageBox( &quot;Test button pressed&quot; );</TT>

<TT>}</TT></FONT></PRE>
<H3><FONT COLOR="#000077"><B>Changing a Button's Label</B></FONT></H3>
<P>Like all controls, a button is a just a special type of window. For that reason,
the MFC class library uses the <TT>CWnd</TT> class as a base class for all control
classes. To change the label for a button, you can use the <TT>SetWindowText</TT>
function.</P>
<P>This function commonly is used to change the label for buttons after the dialog
box has been created. You can use the <TT>SetWindowText</TT> function to change the
Amplify button from the earlier example into a Record button. To do so, replace the
<TT>CButtonDlg::OnBtnTest</TT> function with the function provided in Listing 5.7.
<H4><FONT COLOR="#000077">TYPE: Listing 5.7. Changing the label for several buttons.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CButtonDlg::OnBtnTest()</TT>
<TT>{</TT>
<TT>    static BOOL bSetWaterLevel = TRUE;</TT>
<TT>    if( bSetWaterLevel == TRUE )</TT>
<TT>    {</TT>
<TT>        m_btnVolume.SetWindowText( &quot;&amp;Water Level&quot; );</TT>
<TT>        m_btnAmp.SetWindowText( &quot;&amp;Record&quot; );</TT>
<TT>        bSetWaterLevel = FALSE;</TT>
<TT>    }</TT>
<TT>    else</TT>
<TT>    {</TT>
<TT>        m_btnVolume.SetWindowText( &quot;&amp;Volume&quot; );</TT>
<TT>        m_btnAmp.SetWindowText( &quot;&amp;Amplify&quot; );</TT>
<TT>        bSetWaterLevel = TRUE;</TT>
<TT>    }</TT>
</FONT></PRE>
<PRE><FONT COLOR="#0066FF"><TT>}</TT> </FONT></PRE>
<P>After you build the Button example using the code from Listing 5.7, the radio
button group will alternate between Volume and Water Level.
<H3><FONT COLOR="#000077"><B>Enabling and Disabling Buttons</B></FONT></H3>
<P>Most controls are enabled by default, although a control can be initially disabled
by setting that attribute in its property list. A control can be selected only if
it is enabled. The <TT>CWnd</TT> class includes the <TT>EnableWindow</TT> member
function that allows a <TT>CWnd</TT> object to be enabled or disabled. Because <TT>CButton</TT>
and all other control classes are derived from <TT>CWnd</TT>, they include all the
member data and member functions from the <TT>CWnd</TT> class, and you can disable
a button like this:</P>
<PRE><FONT COLOR="#0066FF"><TT>pButton-&gt;EnableWindow( FALSE );  // Disables control</TT>
</FONT></PRE>
<P>The parameter for <TT>EnableWindow</TT> is <TT>TRUE</TT> if the window or control
should be enabled, and <TT>FALSE</TT> if it should be disabled. The default parameter
for <TT>EnableWindow</TT> sets the parameter to <TT>TRUE</TT> because no parameter
is needed to enable the control:</P>
<PRE><FONT COLOR="#0066FF"><TT>pButton-&gt;EnableWindow();  // Enables control</TT>
</FONT></PRE>
<P>It is common practice for buttons and other controls to be enabled or disabled
based on events that are received by the dialog box. As an example, pressing one
button can cause another button to be disabled or enabled. To disable a dialog box
control, replace the <TT>CButtonDlg::OnBtnTest</TT> function with the source code
provided in Listing 5.8.
<H4><FONT COLOR="#000077">TYPE: Listing 5.8. Using CWnd::EnableWindow to disable
a dialog box control.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CButtonDlg::OnBtnTest()</TT>
<TT>{</TT>
<TT>    static BOOL bEnableControl = FALSE;</TT>

<TT>     m_btnAmp.EnableWindow( bEnableControl );</TT>

<TT>    if( bEnableControl == TRUE )</TT>
<TT>        bEnableControl = FALSE;</TT>
<TT>    else</TT>
<TT>        bEnableControl = TRUE;</TT>
<TT>}</TT> </FONT></PRE>
<P>Now when you click the Test button, the Amplify check box is disabled. When you
click the Test button again, the check box is enabled.
<H3><FONT COLOR="#000077"><B>Hiding a Button</B></FONT></H3>
<P>It's not unusual to need to hide a button that is located in a dialog box. Often,
a button has its properties set to be hidden by default. Once again, the <TT>CWnd</TT>
class has a member function that can be used to hide or display a window as needed.
Use the <TT>CWnd::ShowWindow</TT> member function like this:</P>
<PRE><FONT COLOR="#0066FF"><TT>pButton-&gt;ShowWindow( SW_HIDE );  // Hide control</TT>
</FONT></PRE>
<P>This code hides the <TT>pButton</TT> window, which is a button control in this
case. To display a hidden window, the <TT>ShowWindow</TT> function is used with the
<TT>SW_SHOW</TT> parameter:</P>
<PRE><FONT COLOR="#0066FF"><TT>pButton-&gt;ShowWindow( SW_SHOW );  // Display control</TT>
</FONT></PRE>
<P>Listing 5.9 provides a function that uses <TT>CWnd::ShowWindow</TT> to alternately
hide and display some of the other buttons in the main dialog box.
<H4><FONT COLOR="#000077">TYPE: Listing 5.9. Using CWnd::ShowWindow to hide a dialog
box control.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CButtonDlg::OnBtnTest()</TT>
<TT>{</TT>
<TT>    static int nShowControl = SW_HIDE;</TT>

<TT>    m_btnAmp.ShowWindow( nShowControl );</TT>

<TT>    if( nShowControl == SW_SHOW )</TT>
<TT>        nShowControl = SW_HIDE;</TT>
<TT>    else</TT>
<TT>        nShowControl = SW_SHOW;</TT>
</FONT></PRE>
<PRE><FONT COLOR="#0066FF"><TT>}</TT></FONT></PRE>
<H2><FONT COLOR="#000077"><B>Defining and Setting Tab Order</B></FONT></H2>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>When a dialog box is presented
to the user, one control will have the <I>keyboard focus</I>, sometimes just called
the <I>focus</I>. The control that has the focus receives all input from the keyboard.
When a control has the focus, it has a dotted focus rectangle drawn around it.</P>
<P>A user can change the focus to a new control by pressing the Tab key on the keyboard.
Each time the Tab key is pressed, a new control receives the focus. If you aren't
familiar with how this works, you might want to experiment with a few dialog boxes
from Developer Studio.</P>
<P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>The controls are always selected
in a fixed order, known as the <I>tab order</I>. Tab order lets users select controls
without using the mouse. Although almost all Windows users have access to a mouse,
using the keyboard sometimes is more convenient. Also, because tabbing between controls
is a standard feature in Windows dialog boxes, you should use it correctly.


<BLOCKQUOTE>
	<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>The tab order should
	follow a logical pattern through the dialog box. If the tab order follows a predictable
	pattern, users of the dialog box will find it much easier to navigate using the Tab
	key. Usually, the first editable control receives the focus when the dialog box is
	opened. After that, the focus should be passed to the next logical control in the
	dialog box. The buttons that control the dialog box--OK, Cancel, and Apply--should
	receive the focus last. 
<HR>


</BLOCKQUOTE>

<P>In a dialog box, the tab order follows the sequence in which controls were defined
in the resource script. As new controls are added, they are placed at the end of
the tab order. You can use the resource tools included in the Developer Studio to
change this sequence, thereby altering the tab order.


<BLOCKQUOTE>
	<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>To prevent a user
	from selecting a control using the Tab key, clear the Tab Stop property for the control.
	
<HR>


</BLOCKQUOTE>

<P>With the dialog box displayed in the Developer Studio, select Tab Order from the
Layout menu, or press Ctrl+D. Each control in the dialog box that has the <TT>tabstop</TT>
attribute is tagged with a number, as shown in Figure 5.3.</P>
<P><A NAME="03"></A><A HREF="03.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch05/03.htm"><B>Figure 5.3.</B> </A><I><BR>
Displaying the tab order for dialog box controls.</I></P>
<P>To change the tab order, just click the control that should be in tab position
1; the tag associated with that control changes to reflect its new tab order. Repeat
the process of clicking controls until the displayed tab order is correct.
<H2><FONT COLOR="#000077"><B>Summary</B></FONT></H2>
<P>In this hour you learned about the different types of button controls provided
by Windows and used the controls in a variety of ways. You also built a dialog box-based
project. Finally, you learned about control tab order and conditional expressions.
<H2><FONT COLOR="#000077"><B>Q&amp;A</B></FONT></H2>

<DL>
	<DD><B>Q What is the difference between <TT>BOOL</TT> and <TT>bool</TT>?</B><BR>
	<BR>
	<B>A</B> The <TT>bool</TT> type is defined by the C++ standard, whereas the <TT>BOOL</TT>
	type is defined deep inside the Windows header files. In practice, they work very
	much alike, and you can interchange them without any problem. The reason for the
	<TT>BOOL</TT> type is historical; <TT>bool</TT> was only recently added to the C++
	standard, and the <TT>BOOL</TT> type has been used for Windows programming for many
	years. In fact, <TT>BOOL</TT> was used for Windows programming before C++ was invented.<BR>
	<BR>
	<B>Q When is it more appropriate to hide a control instead of disabling it?</B><BR>
	<BR>
	<B>A</B> If a control is unavailable or doesn't make sense for a temporary period,
	it should be disabled. If the control is unavailable for a long period of time, it
	should be hidden. In general, the user should be presented with as few options as
	possible, especially if those options cannot be selected.
</DL>

<H2><FONT COLOR="#000077"><B>Workshop</B></FONT></H2>
<P>The Workshop is designed to help you anticipate possible questions, review what
you've learned, and begin thinking ahead to putting your knowledge into practice.
The answers to the quiz are in Appendix B, &quot;Quiz Answers.&quot;
<H3><FONT COLOR="#000077"><B>Quiz</B></FONT></H3>

<DL>
	<DD>1. What is the difference between the Cancel and Close buttons?<BR>
	<BR>
	2. What is the difference between the OK and Apply buttons?<BR>
	<BR>
	3. What MFC class is used to manage button controls?<BR>
	<BR>
	4. What are the five types of button controls?<BR>
	<BR>
	5. How do you prevent the Tab key from being used to select a control?<BR>
	<BR>
	6. What function is used to disable a control at runtime?<BR>
	<BR>
	7. What function is used to hide a control at runtime?<BR>
	<BR>
	8. What is the <TT>default</TT> label used for in a <TT>switch</TT> statement?<BR>
	<BR>
	9. What is the difference between the <TT>=</TT> and <TT>==</TT> operators?<BR>
	<BR>
	10. What function is used to change the label on a button?
</DL>

<H3><FONT COLOR="#000077"><B>Exercises</B></FONT></H3>

<DL>
	<DD>1. Modify the Button project so that the Amplify check box is removed from the
	tab order.<BR>
	<BR>
	2. Modify the Button project so that the source code from Listing 5.7 is used, except
	that the Amplify check box is hidden when the group box caption is set to Water Level.
</DL>

<CENTER>
<P>
<HR>
<A HREF="ch04.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch04/ch04.htm"><IMG SRC="previous.gif" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="ch06.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch06/ch06.htm"><IMG
SRC="next.gif" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="index-1.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/index.htm"><IMG SRC="contents.gif" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR>
<BR>
<BR>
<IMG SRC="corp.gif" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/button/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"
BORDER="0"></P>

<P>&#169; <A HREF="copy.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/copy.htm">Copyright</A>, Macmillan Computer Publishing. All
rights reserved.
</CENTER>


</BODY>

</HTML>

⌨️ 快捷键说明

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