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

📄 vcg18.htm

📁 Visual C++与数据库的连接经典实例
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Converting Menu Macros</B></FONT></CENTER></H4>

<BR>

<P>Menu macros are the easiest of all Access macros to convert to Visual C++. Because you can't execute the AddMenu action with the Visual Basic for Applications DoCmd statement, converting Access menu macros to Visual C++ menus is discussed in this section of the chapter rather than in the next section, which deals directly with code conversion. To make the conversion, just duplicate the menu structure defined by the hierarchy of macros that employ the AddMenu action with entries in Visual C++'s menu resource editor window for each of the menu items of your Access application that you have reconstructed as Visual C++ forms. You then can use ClassWizard to add event handlers for the menu items.

<BR>

<P>With Visual C++, you typically have a single menu structure for the entire application rather than a menu for each form or MDI child window. If the Access application doesn't use its own set of macros, you can add a minimal menu structure to the MDI parent form. A minimal menu structure consists of the following elements, all of which are generated automatically when you use AppWizard to create your application shell:

<BR>

<UL>

<LI>A File menu with a single submenu, Exit. You can tell AppWizard to add print support; however, a form view application doesn't directly support printing, so you will have to create the print routines yourself.

<BR>

<BR>

<LI>An Edit menu with Undo, Cut, Copy, and Paste selections. If your application can't implement these actions (and that isn't good programming practice), you should delete whichever menu items in the Edit menu can't be implemented. If none can be implemented, eliminate the entire Edit menu.

<BR>

<BR>

<LI>A Record menu that lets the user navigate between records in the dataset. Virtually all database applications need record selection menu and toolbar support.

<BR>

<BR>

<LI>A View menu that lets the user display or hide the toolbar and status bar. If you don't include toolbars and status bars in the application, this menu selection won't be present.

<BR>

<BR>

<LI>A Window menu that provides a submenu with the MDI window arrangement commands (Tile, Cascade, and Arrange Icons), plus the WindowList option, which is separated from the arrangement commands with a horizontal bar. You need to display the Window menu only if you want to let the user choose between viewing more than one MDI child window at a time or to be able to select the active window from a menu rather than from a button bar. MFC does most of the work in creating and managing the MDI windows, so you will usually be able to rely on MFC's implementation of the Window menu.

<BR>

<BR>

<LI>A Help menu with at least one submenu choice, About, which displays the application's name, version number, and copyright information. For a professional application, you should include context-sensitive help using AppWizard's &quot;include context-sensitive help&quot; option.

<BR>

<BR>

</UL>

<BLOCKQUOTE>

<BLOCKQUOTE>

<HR ALIGN=CENTER>

<BR>

<NOTE><B>TIP</B>

<BR>

<BR>If a help file is available for your Access application and you duplicate the structure of the Access application with your Visual C++ derivative, you can use the same help file, making only minor edits to account for the somewhat different structure of the Visual C++ version. Like Access forms, each Visual C++ dialog box and control has its own HelpContextID property. Access lets you assign different values to the HelpFile property of each form.

<BR>

<BR>If your Access application uses multiple help files, you can call the WinHelp() API function in your Visual C++ code to display the specific help file for a dialog or situation. It's usually easier to call the WinHelp() function than to re-create a single help file from a collection. You might find that HelpContextID values are duplicated in the combined files if you combine a collection of help files.</NOTE>

<BR>

<HR ALIGN=CENTER>

</BLOCKQUOTE></BLOCKQUOTE>

<BR>

<A NAME="E68E95"></A>

<H3 ALIGN=CENTER>

<CENTER>

<FONT SIZE=5 COLOR="#FF0000"><B>Converting Macro Actions to Visual C++</B></FONT></CENTER></H3>

<BR>

<P>Access applications that rely primarily on macros for event handling require more effort to convert to Visual C++ than applications that use Visual Basic for Applications code for event handling. You need to translate macros into Visual C++ event-handling code. Most Visual Basic for Applications functions import into Visual C++ modules but must be rewritten to C/C++ code. It's possible to do a line-by-line conversion of Visual Basic for Applications (or, for that matter, Visual Basic) code. The following sections provide a cross-reference between Access and Visual C++ events and list the Visual C++ methods and properties that correspond to Access macro actions.

<BR>

<BR>

<A NAME="E69E231"></A>

<H4 ALIGN=CENTER>

<CENTER>

<FONT SIZE=4 COLOR="#FF0000"><B>Events That Trigger Macro Actions</B></FONT></CENTER></H4>

<BR>

<P>Visual C++ has a much broader repertoire of general-purpose events than Access, but Access offers finer granularity in events that are related to forms and controls bound to database objects. <I>Granularity</I> is a term used by programmers to define the degree of precision of control (sometimes called <I>resolution</I>) that you can achieve with a language's code. (Granularity is similar to the term <I>graininess,</I> which is used in photography to describe the size of the grains of silver halide that form the image. The grain size determines the potential sharpness or acuity of an image.) In the documentation that accompanies Access, Microsoft refers to Access events as <I>properties,</I> presumably because events are included in Access's Properties window list for forms and controls. This book uses the term <I>event</I> rather than <I>property</I>.

<BR>

<P>An example of the fine granularity of Access events triggered by bound controls is the sequence of OnEnter, BeforeUpdate, AfterUpdate, and OnExit events that occur when you update the value of a field of the current record. Table 18.1 lists Access events and their equivalent Visual C++ events (where an equivalent exists). Table 18.1 shows that Visual C++ offers the WM_INITDIALOG, CRecordset::Update(), and WM_CLOSE (or WM_EXIT) events, which correspond to Access's OnEnter, BeforeUpdate, and OnExit events, respectively. However, there is no Visual C++ event that directly corresponds to Access's AfterUpdate event, and the CRecordSet::Update() function performs a number of functions. The OnCurrent event also isn't available in Visual C++. There is no direct substitute or universal workaround for the OnCurrent event.

<BR>

<BR>

<P ALIGN=CENTER>

<CENTER>

<FONT COLOR="#000080"><B>Table 18.1. Visual Basic for Applications events and corresponding Visual C++ events.</B></FONT></CENTER>

<BR>



<CENTER><TABLE  BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 CELLPADDING=3 >

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Event</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Object It </I><I>Applies To</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Occurrence </I><I>in Access</I>

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

<I>Visual C++ </I><I>Event</I>

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

AfterDelConfirm

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Forms

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

After the user confirms a deletion and the record is actually deleted or the deletion is canceled.

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Use CRecordset member functions to manage this event.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

AfterInsert

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Forms

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

After a new record is added.

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Use CRecordset member functions to manage this event.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

AfterUpdate

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Forms and controls

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

After a change to a record is made permanent.

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Use CRecordset::Update() to manage updating.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

BeforeDelConfirm

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Forms

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

After the user deletes one or more records, and before Access displays a confirmation dialog box.

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Write a custom handler for CRecordset.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

BeforeInsert

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Forms

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

When the user types the first character in a new record.

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Write a custom handler for CRecordset.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

BeforeUpdate

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Forms and controls

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Before a change to a record is made permanent.

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Use CRecordset::Update() to manage updating.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

OnActivate

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Forms and reports

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

When a form or report receives focus and becomes the active window.

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Write and use the WM_INITDIALOG handler.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

OnApplyFilter

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Forms

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

When the user chooses Records | Filter | Apply Filter/Sort or clicks the Apply Filter button on the toolbar.

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

No equivalent.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

OnChange

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Controls (combo box, text box) on a form

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

When the contents of a text box or the text portion of a combo box change.

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Use a CComboBox or CListBox object and an ON_LBN_SELCHANGE handler.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

OnClick

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Forms and controls

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

When the user presses and then releases the mouse button over an object.

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Use the appropriate control object.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

OnClose

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Forms and reports

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

When a form is closed, but before it disappears.

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Create and code a handler for the WM_CLOSE message.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

OnCurrent

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Forms

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Before a record becomes the current record.

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Use the CRecordset::OnMove() function.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

OnDblClick*

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Forms and controls

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

When the user double-clicks an object.

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Use the appropriate control object.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

OnDeactivate

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Forms and reports

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

When a form or report loses focus.

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Write a WM_CLOSE handler for the dialog box.

</FONT>

<TR>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

OnDelete

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Forms

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

Before deleting a record.

</FONT>

<TD VALIGN=top  BGCOLOR=#80FFFF ><FONT COLOR=#000080>

⌨️ 快捷键说明

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