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

📄 ch22.htm

📁 Using Visual C++ 6.0 一本关于Visual C++ 6.0基本编程应用的书籍。
💻 HTM
📖 第 1 页 / 共 4 页
字号:
	to the menu, as shown in Figure 22.24.
	<P>
	<DT></DT>
	<DD><B>4. </B>Select the blank button again and draw a red minus sign, giving the
	button the <B>ID_RECORD_DELETE</B> ID, as you can see in Figure 22.25. Drag and drop
	the Add and Delete buttons to the left of the Help (question mark) button.
	<P>
</DL>

<P>Now that you have added the menu items and the toolbar buttons, you need to arrange
for code to catch the command message sent when the user clicks the button or chooses
the menu item. Background information on this process is in Chapter 3, &quot;Messages
and Commands,&quot; and in Chapter 8 and Chapter 9. Because it is the view that is
connected to the database, the view will catch these messages. Follow these steps:</P>

<DL>
	<DT></DT>
	<DD><B>1. </B>Open ClassWizard and select the Message Maps tab.
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc24.gif')"><B>FIG. 22.24</B></A><B> </B><I>Add
a button and connect it to the menu item.</I></P>
<P>

<DL>
	<DT><I></I></DT>
</DL>

<P><A HREF="javascript:popUp('22uvc25.gif')"><B>FIG. 22.25</B></A><B> </B><I>The
minus-sign button will control the Delete() function.</I></P>
<P>

<DL>
	<DD><B>2. </B>Set the Class Name box to CEmployeeView, click the ID_RECORD_ADD ID
	in the Object IDs box, and then double-click COMMAND in the Messages box. The Add
	Member Function dialog box appears, as shown in Figure 22.26.
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc26.gif')"><B>FIG. 22.26</B></A><B> </B><I>Add
a function to catch the message.</I></P>
<P>

<DL>
	<DT><I></I></DT>
	<DD><B>3. </B>Click the OK button to accept the default name for the new function.
	The function appears in the Member Functions box at the bottom of the ClassWizard
	dialog box.
	<P>
	<DT></DT>
	<DD><B>4. </B>Add a member function for the ID_RECORD_DELETE command in the same
	way. The list of functions should resemble Figure 22.27. Click OK to close ClassWizard.
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc27.gif')"><B>FIG. 22.27</B></A><B> </B><I>The
new functions appear in the Member Functions box.</I></P>
<P>

<DL>
	<DT><I></I></DT>
	<DD><B>5. </B>Open the EmployeeView.h file by double-clicking CEmployeeView in the
	ClassView pane. In the Attributes section of the class's declaration, add the following
	lines:
	<P>
</DL>



<BLOCKQUOTE>
	<PRE>protected:
 BOOL m_bAdding;</PRE>

</BLOCKQUOTE>

<PRE></PRE>

<DL>
	<DD><B>6. </B>Double-click the CEmployeeView constructor in ClassView to edit it,
	and add this line at the bottom of the function:
	<P>
</DL>



<BLOCKQUOTE>
	<PRE> m_bAdding = FALSE;</PRE>

</BLOCKQUOTE>

<PRE></PRE>

<DL>
	<DT></DT>
	<DD><B>7. </B>Double-click the OnRecordAdd() function and edit it so that it looks
	like Listing 22.1. This code is explained in the next section.
	<P>
</DL>

<H4>Listing 22.1&#160;&#160;CEmployeeView::OnRecordAdd()</H4>
<PRE>void CEmployeeView::OnRecordAdd()
{
    m_pSet-&gt;AddNew();
    m_bAdding = TRUE;
    CEdit* pCtrl = (CEdit*)GetDlgItem(IDC_EMPLOYEE_ID);
    int result = pCtrl-&gt;SetReadOnly(FALSE);
    UpdateData(FALSE);
</PRE>
<PRE>}
</PRE>

<DL>
	<DT></DT>
	<DD><B>8. </B>Right-click CEmployeeView in ClassView and choose Add Virtual Function.
	Select OnMove from the list on the left, as shown in Figure 22.28, and then click
	the Add and Edit button to add the function and to edit the skeleton code immediately.
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc28.gif')"><B>FIG. 22.28</B></A><B> </B><I>Override
the OnMove() function.</I></P>
<P>

<DL>
	<DD><B>9. </B>Edit the OnMove() function so that it has the code in Listing 22.2.
	This code is explained in the next section.
	<P>
</DL>

<H4>Listing 22.2&#160;&#160;CEmployeeView::OnMove()</H4>
<PRE>BOOL CEmployeeView::OnMove(UINT nIDMoveCommand)
{
     if (m_bAdding)
     {
         m_bAdding = FALSE;
         UpdateData(TRUE);
         if (m_pSet-&gt;CanUpdate())
             m_pSet-&gt;Update();
         m_pSet-&gt;Requery();
         UpdateData(FALSE);
         CEdit* pCtrl = (CEdit*)GetDlgItem(IDC_EMPLOYEE_ID);
         pCtrl-&gt;SetReadOnly(TRUE);
         return TRUE;
     }
     else
         return CRecordView::OnMove(nIDMoveCommand);
</PRE>
<PRE>}
</PRE>

<DL>
	<DT></DT>
	<DD><B>10. </B>Double-click the OnRecordDelete() function and edit it so that it
	looks like Listing 22.3. This code is explained in the next section.
	<P>
</DL>

<H4>Listing 22.3&#160;&#160;CEmployeeView::OnRecordDelete()</H4>
<PRE>void CEmployeeView::OnRecordDelete()
{
        m_pSet-&gt;Delete();
        m_pSet-&gt;MoveNext();
        if (m_pSet-&gt;IsEOF())
              m_pSet-&gt;MoveLast();
        if (m_pSet-&gt;IsBOF())
              m_pSet-&gt;SetFieldNull(NULL);
        UpdateData(FALSE);
</PRE>
<PRE>}
</PRE>
<P>You've now modified the Employee application so that it can add and delete, as
well as update, records. After compiling the application, run it by selecting the
Build, Execute command from Developer Studio's menu bar or by pressing Ctrl+F5. When
you do, you see the Employee application's main window, which doesn't look any different
than it did in the preceding section. Now, however, you can add new records by clicking
the Add button on the toolbar (or by selecting the Record, Add Record command on
the menu bar) and delete records by clicking the Delete button (or by clicking the
Record, Delete Record command).</P>
<P>When you click the Add button, the application displays a blank record. Fill in
the fields for the record; then when you move to another record, the application
automatically updates the database with the new record. To delete a record, just
click the Delete button. The current record (the one on the screen) vanishes and
is replaced by the next record in the database.</P>
<P>
<H3><A NAME="Heading11"></A>Examining the OnRecordAdd() Function</H3>
<P>You might be wondering how the C++ code you added to the application works. OnRecordAdd()
starts with a call to the AddNew() member function of CEmployeeSet, the class derived
from CRecordSet. This sets up a blank record for the user to fill in, but the new
blank record doesn't appear on the screen until the view window's UpdateData() function
is called. Before that happens, you have a few other things to tackle.</P>
<P>After the user has created a new record, the database will need to be updated.
By setting a flag in this routine, the move routine will be able to determine whether
the user is moving away from an ordinary database record or a newly added one. That's
why m_bAdding is set to TRUE here.</P>
<P>Now, because the user is entering a new record, it should be possible to change
the contents of the Employee ID field, which is currently set to read-only. To change
the read-only status of the control, the program first obtains a pointer to the control
with GetDlgItem() and then calls the control's SetReadOnly() member function to set
the read-only attribute to FALSE.</P>
<P>Finally, the call to UpdateData() will display the new blank record.</P>
<P>
<H3><A NAME="Heading12"></A>Examining the OnMove() Function</H3>
<P>Now that the user has a blank record on the screen, it's a simple matter to fill
in the edit controls with the necessary data. To add the new record to the database,
the user must move to a new record, an action that forces a call to the view window's
OnMove() member function. Normally, OnMove() does nothing more than display the next
record. Your override will save new records as well.</P>
<P>When OnMove() is called, the first thing the program does is check the Boolean
variable m_bAdding to see whether the user is in the process of adding a new record.
If m_bAdding is FALSE, the body of the if statement is skipped and the else clause
is executed. In the else clause, the program calls the base class (CRecordView) version
of OnMove(), which simply moves to the next record.</P>
<P>If m_bAdding is TRUE, the body of the if statement is executed. There, the program
first resets the m_bAdding flag and then calls UpdateData() to transfer data out
of the view window's controls and into the recordset class. A call to the recordset's
CanUpdate() method determines whether it's okay to update the data source, after
which a call to the recordset's Update() member function adds the new record to the
data source.</P>
<P>To rebuild the recordset, the program must call the recordset's Requery() member
function, and then a call to the view window's UpdateData() member function transfers
new data to the window's controls. Finally, the program sets the Employee ID field
back to read-only, with another call to GetDlgItem() and SetReadOnly().</P>
<P>
<H3><A NAME="Heading13"></A>Examining the OnRecordDelete() Function</H3>
<P>Deleting a record is simple. OnRecordDelete() just calls the recordset's Delete()
function. When the record is deleted, a call to the recordset's MoveNext() arranges
for the record that follows to be displayed.</P>
<P>A problem might arise, though, when the deleted record was in the last position
or when the deleted record was the only record in the recordset. A call to the recordset's
IsEOF() function will determine whether the recordset was at the end. If the call
to IsEOF() returns TRUE, the recordset needs to be repositioned on the last record.
The recordset's MoveLast() function takes care of this task.</P>
<P>When all records have been deleted from the recordset, the record pointer will
be at the beginning of the set. The program can test for this situation by calling
the recordset's IsBOF() function. If this function returns TRUE, the program sets
the current record's fields to NULL.</P>
<P>Finally, the last task is to update the view window's display with another call
to UpdateData().</P>
<P>
<H3><A NAME="Heading14"></A>Sorting and Filtering</H3>
<P>In many cases when you're accessing a database, you want to change the order in
which the records are presented, or you may even want to search for records that
fit certain criteria. MFC's ODBC database classes feature member functions that enable
you to sort a set of records on any field. You can also call member functions to
limit the records displayed to those whose fields contain given information, such
as a specific name or ID. This latter operation is called <I>filtering</I>. In this
section, you will add sorting and filtering to the Employee application. Just follow
these steps:</P>

<DL>
	<DT></DT>
	<DD><B>1. </B>Add a Sort menu to the application's menu bar, as shown in Figure 22.29.
	Let Developer Studio set the command IDs.
	<P>
	<DT></DT>
	<DD><B>2. </B>Use ClassWizard to arrange for CEmployeeView to catch the four new
	sorting commands, using the function names suggested by ClassWizard. Figure 22.30
	shows the resultant ClassWizard property sheet.
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc29.gif')"><B>FIG. 22.29</B></A><B> </B><I>The
Sort menu has four commands for sorting the database.</I></P>
<P><A HREF="javascript:popUp('22uvc30.gif')"><B>FIG. 22.30</B></A><B> </B><I>After
you add the four new functions, ClassWizard looks like this.</I></P>
<P>

<DL>
	<DD><B>3. </B>Add a Filter menu to the application's menu bar, as shown in Figure
	22.31. Let Developer Studio set the command IDs.
	<P>
	<DT></DT>
	<DD><B>4. </B>Use ClassWizard to arrange for CEmployeeView to catch the four new
	filtering commands, using the function names suggested by ClassWizard.
	<P>
	<DT></DT>
	<DD><B>5. </B>Create a new dialog box by choosing Insert, Resource and double-clicking
	Dialog; then edit the dialog so that it resembles the dialog box shown in Figure
	22.32. Give the edit control the ID <B>IDC_FILTERVALUE</B>. Give the entire dialog
	the ID <B>IDD_FILTER</B>.
	<P>
</DL>

<P><A HREF="javascript:popUp('22uvc31.gif')"><B>FIG. 22.31</B></A><B> </B><I>The
Filter menu has four commands.</I></P>
<P><A HREF="javascript:popUp('22uvc32.gif')"><B>FIG. 22.32</B></A><B> </B><I>Create
a filter dialog box.</I></P>
<P>

<DL>
	<DD><B>6. </B>Start ClassWizard while the new dialog box is on the screen. The Adding
	a Class dialog box appears. Select the Create a New Class option and click OK.
	<P>
	<DT></DT>
	<DD><B>7. </B>The New Class dialog box appears. In the Name box, type <B>CFilterDlg</B>,
	as shown in Figure 22.33. Click OK to add the class.
	<P>
</DL>

⌨️ 快捷键说明

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