📄 vcg18.htm
字号:
<HTML>
<HEAD>
<TITLE>vcg18.htm </TITLE>
<LINK REL="ToC" HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/index.htm">
<LINK REL="Index" HREF="htindex.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/htindex.htm">
<LINK REL="Next" HREF="vcgp5.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcgp5.htm">
<LINK REL="Previous" HREF="vcg17.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg17.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<A NAME="I0"></A><P ALIGN=CENTER>
<A HREF="vcg17.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcg17.htm" TARGET="_self"><IMG SRC="blanprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blanprev.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/index.htm" TARGET="_self"><IMG SRC="blantoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blantoc.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="TOC"></A>
<A HREF="vcgp5.htm" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/vcgp5.htm" TARGET="_self"><IMG SRC="blannext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/blannext.gif" WIDTH = 37 HEIGHT = 37 BORDER = 0 ALT="Next Page"></A>
<HR ALIGN=CENTER>
<P>
<UL>
<UL>
<UL>
<LI>
<A HREF="#E68E93" >Converting Visual Basic Forms to Visual C++ Dialog Boxes</A>
<LI>
<A HREF="#E68E94" >Deciding on the Structure of Your Converted Visual C++ Application</A>
<UL>
<LI>
<A HREF="#E69E228" >Emulating Access Forms with Visual C++ MDI Windows</A>
<LI>
<A HREF="#E69E229" >Handling Access Object Names in Visual C++</A>
<LI>
<A HREF="#E69E230" >Converting Menu Macros</A></UL>
<LI>
<A HREF="#E68E95" >Converting Macro Actions to Visual C++</A>
<UL>
<LI>
<A HREF="#E69E231" >Events That Trigger Macro Actions</A>
<LI>
<A HREF="#E69E232" >Actions That Manipulate Forms, Reports, and Controls</A>
<LI>
<A HREF="#E69E233" >Actions That Manipulate Database Objects</A>
<LI>
<A HREF="#E69E234" >Actions That Translate Directly to Visual C++ or Windows Functions</A>
<LI>
<A HREF="#E69E235" >Actions with No Direct Counterpart in Visual C++</A></UL>
<LI>
<A HREF="#E68E96" >Importing Visual Basic for Applications Code into Visual C++ Modules</A>
<UL>
<LI>
<A HREF="#E69E236" >User-Defined Access Functions</A>
<LI>
<A HREF="#E69E237" >Access Event-Handling Functions</A>
<UL>
<LI>
<A HREF="#E70E101" >Importing Visual Basic for Applications Code to Visual C++ Modules</A>
<LI>
<A HREF="#E70E102" >Replacing Access DoCmd Statements</A>
<LI>
<A HREF="#E70E103" >Handling Visual Basic for Applications Keywords That Are Missing from or Differ in Visual C++</A></UL></UL>
<LI>
<A HREF="#E68E97" >Summary</A></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E45"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>- 18 -</B>
<BR><B>Translating Visual Basic and Visual Basic for Applications Code to Visual C++</B></FONT></CENTER></H1>
<BR>
<P>Until the advent of Visual C++ 4, the premier database front-end generator was Visual Basic. Visual Basic offered programmers the necessary database access tools. Although earlier versions of Visual C++ did offer ODBC (both with MFC and call-level interfaces), access to the very powerful Microsoft Jet database engine was missing from Visual C++ until version 4 was released. This resulted in many database front-end applications being written in Visual Basic.
<BR>
<P>Some database developers will want to convert from Visual Basic to the more advanced object-oriented programming language that Visual C++ offers. Not all Visual Basic programmers will convert (which is why the book <I>Database Developer's Guide with Visual Basic 4</I> exists), but for database developers who need to convert applications from Visual Basic for Applications or Visual Basic to Visual C++, this chapter might help.
<BR>
<P>If I don't sound too positive about converting database applications that are written using Visual Basic, it's because there are major differences between Basic and C/C++ programming. There is no simple, easy way to do a conversion. No set of global changes, no magic incantations, no secret words exist to make the conversion easier. Hard work and a thorough understanding of Basic programming are the tools that will make for a successful conversion. Generally, this chapter concentrates on Visual Basic for Applications. If you're writing Visual Basic applications (using either Visual Basic 3 or 4), you'll see differences, but nothing too extensive. Generally, this chapter uses the terms <I>Visual </I><I>Basic</I> and <I>Visual Basic for Applications</I> interchangeably. For most conversions, the flavor of Visual Basic isn't a crucial issue.
<BR>
<P>Fortunately, Visual C++ 4 offers some interesting tools to help you do the conversions. First, you can convert a Visual Basic form into a dialog box by using the Resource Editor and importing the Visual Basic form as a dialog box. Generally, many of the controls that are part of the Visual Basic form will also be imported, but there are some notable exceptions. You can't convert VBX controls in a form if the VBX control wasn't installed with the project. (Because VBX controls are 16-bit, they won't work in a 32-bit Visual C++ 4 application.) Also, some controls that are native to Visual Basic are noted for causing problems when forms are converted. Don't expect Visual Basic nested controls to be converted or to perform correctly, either. You will end up with a dialog box that has many of the standard controls correctly placed, but you will need to add or simulate any specialized controls.
<BR>
<P>The following is a list of some Visual Basic controls that won't be fully converted when a Visual Basic form is imported into Visual C++ 4.0:
<BR>
<UL>
<LI>Drive and directory list boxes. These controls will be converted to standard Windows list boxes that your code can then fill as needed. You probably will want to use the file open common dialog boxes for most filename management.
<BR>
<BR>
<LI>MDI forms will be imported as normal dialog boxes. The menu will be ignored, but you can have a menu in a dialog box if you implement it yourself. (For an example of a menu in a dialog box, refer to page 677 of <I>Programming Windows 95 Unleashed</I> (Sams Publishing, 1995).)
<BR>
<BR>
<LI>Data controls aren't supported.
<BR>
<BR>
<LI>Line and shape controls aren't supported.
<BR>
<BR>
<LI>Horizontal and vertical scroll bars. You can use standard scroll bars if needed.
<BR>
<BR>
<LI>Timers aren't supported. Use a custom implementation or an OLE custom control.
<BR>
<BR>
<LI>Printers aren't supported.
<BR>
<BR>
<LI>Screens aren't supported.
<BR>
<BR>
<LI>Clipboards aren't supported.
<BR>
<BR>
<LI>Queries aren't supported.
<BR>
<BR>
<LI>Applications aren't supported.
<BR>
<BR>
</UL>
<P>It's also possible to convert a Visual Basic VBX control into a OLE Custom Control using the VBX Template Tool, which is part of Visual C++ 1.5x and Visual C++ 2.x. In the long run, it would probably be easier to simply fully rewrite the VBX as an OLE control.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>The VBX Template Tool is no longer part of Visual C++ as of version 4.0. If you need to convert a VBX control, you must either use Visual C++ 2.x or perform the conversion by hand. Thanks to Microsoft's subscription program, many Visual C++ 4.x programmers still have old copies of Visual C++ 2.x on hand.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>There are a variety of reasons for migrating (also called <I>porting</I>) an Access database application written in Visual Basic for Applications to Visual C++—reasons that are discussed in earlier chapters of this book. Whatever the motive for migrating the application, you need to convert Access macro actions to Visual C++ event-handling functions and deal with reserved words, such as DoCmd, that are recognized by Access's interpreter but not Visual C++.
<BR>
<P>The most common method of responding to events in Access applications is to use one or more of Access's 40-plus macro commands. Macros, rather than Visual Basic for Applications Basic functions, are the default event handlers of Access. Microsoft added an application-specific macro language to Access at the same time that the company's programmers were working hard to supplant specialized macro languages in Microsoft's other mainstream applications with dialects of Object Basic. The final result of this process is Visual Basic for Applications, which is part of most Microsoft Office applications (Access, Word, and Excel all have Visual Basic for Applications). Access also has its own specialized macro language. The goal of adding macros to Access was to create a desktop relational database development environment for Windows that didn't require writing code. Microsoft almost achieved its goal, at least if you don't consider complex macros with conditional execution and looping statements to be "code." The missing element is the IsLoaded("<I>FormName</I>") Visual Basic for Applications function that almost every Access application needs.
<BR>
<P>This chapter begins by showing you how to convert a Visual Basic form to a Visual C++ dialog box. This is followed by an explanation of how to convert applications written with Access's Visual Basic for Applications that are based primarily on macros as event handlers. Most Access applications use macros to control the execution of Access applications in response to user- or application-generated events. All of the code used in this chapter is from Access 7's NorthWind database example. The NorthWind database application is basically written using Access macros, with supporting routines written in Visual Basic for Applications.
<BR>
<BR>
<A NAME="E68E93"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Converting Visual Basic Forms to Visual C++ Dialog Boxes</B></FONT></CENTER></H3>
<BR>
<P>Wow, you mean we can really do that? Short answer: Yes. For Visual Basic code, however, Access's Visual Basic for Applications is a different story. Forms are stored in the database and generally aren't accessible by external applications such as Visual C++ 4. Long answer: You will have problems with Visual Basic, and you will have to do substantial work by hand for any but the most trivial form. With Visual Basic for Applications, you might as well start from scratch: You will have to lay out a dialog box by hand, probably using a screen print of the original Access Visual Basic for Applications form. Because only Visual Basic forms (not Visual Basic for Applications forms) can be converted, this section applies only to Visual Basic.
<BR>
<P>Follow these steps to convert a Visual Basic form to a Visual C++ dialog box:
<BR>
<OL>
<LI>In Visual C++, select Insert | Resource. You see the Insert Resource dialog box, shown in Figure 18.1.
<BR>
<BR><B><A HREF="18vcg01.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/18vcg01.gif">Figure 18.1. Visual C++'s Insert Resource dialog box.</A></B>
<BR>
<BR>
<LI>Click the Import button on the right side of the Insert Resource dialog. Visual C++ will display the Import Resource dialog box, shown in Figure 18.2.
<BR>
<BR><B><A HREF="18vcg02.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/18vcg02.gif">Figure 18.2. Visual C++'s Import Resource dialog box.</A></B>
<BR>
<BR>
<LI>Select the Visual Basic Forms (*.frm) file type in the list box. Then select the Visual Basic form to be imported.
<BR>
<BR>
</OL>
<P>You will usually get a series of messages from Visual C++ when you import Visual Basic controls. Typically, these messages will include the following:
<BR>
<UL>
<LI>The OLE control "<I>control</I>" is not installed in the project.
<BR>
<BR>
<LI>Syntax error in VB form description.
<BR>
<BR>
<LI>The form's binary data file "<I>data</I>.FRX" cannot be opened.
<BR>
<BR>
</UL>
<P>For the first two errors, you will be given the opportunity to edit the VBX source. Be aware that it will take a substantial amount of work to convert the form when you receive these errors.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>What is a Visual Basic .FRM file? Basically (pun intended), a .FRM file is the equivalent of both a dialog box definition and the handlers for the controls in the dialog box. You could look at it as a mixture of a dialog resource and the CDialog object implementation.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E68E94"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>Deciding on the Structure of Your Converted Visual C++ Application</B></FONT></CENTER></H3>
<BR>
<P>Before you begin converting Visual Basic for Applications code to Visual C++, you need to determine how to structure your application. Although this chapter is devoted primarily to importing Visual Basic for Applications code into C and C++ modules and Visual C++ functions that serve as event handlers, your migration strategy can affect how you go through the importation process. The following sections explain the use of Visual C++ MDI forms and dialog boxes to duplicate the windowing environment of Access and how to duplicate Access menus and forms.
<BR>
<BR>
<A NAME="E69E228"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Emulating Access Forms with Visual C++ MDI Windows</B></FONT></CENTER></H4>
<BR>
<P>Access database applications use MDI child windows to display
<BR>
<UL>
<LI>Data tables
<BR>
<BR>
<LI>All Access forms
<BR>
<BR>
<LI>Other visible objects, such as the Database window and datasheet views of Table objects
<BR>
<BR>
<LI>The query result sets returned by QueryDef or <I>ad hoc</I> queries
<BR>
<BR>
<LI>The design views of all Access database objects
<BR>
<BR>
</UL>
<P>You open the MDI parent window by executing Access and opening a database. Many modern Windows applications with any degree of complexity use an MDI parent window together with as many MDI child windows as are necessary to accomplish the objectives of the application and the user.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>With the more document-centric model of Windows 95, Microsoft recommends not using the MDI interface for applications. For applications that simply provide multiple views of a single object, the MDI interface is acceptable. Microsoft recommends that a single instance of an application not open more than one document at a time.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>You don't need to use MDI forms for a simple data-entry application. MDI is overkill for single-purpose, transaction-processing applications that might employ a dialog view, a form view (CFormView), or a few modal dialog boxes for input prompting or list selection. However, unless you have a compelling reason not to do so, you can use Visual C++'s MDI form for multiform Access applications. Create an MDI parent form and then add MDI child windows that correspond to each of the forms of your Access application. Using form views and MDI in the same application is an accepted practice. Microsoft includes several examples of this, including the CheckBook program in the \MSDEV\SAMPLES\MFC\ADVANCED\CHKBOOK folder on the Visual C++ 4 CD.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>A second method of creating a form-based application that has more than one view of the data is to use an SDI CFormView class and use simple dialog boxes to emulate the other forms in the application. You can have multiple dialog boxes open at a time by making the dialog boxes modeless. A number of applications use modeless dialog boxes as status windows. Visual C++'s properties boxes are one example.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>You can use the Database Documentor add-in tool supplied with Access to create a table that lists the forms in your Access application, together with supplemental tables that list the control objects contained in each of the forms. Figure 18.3 shows a page from Access's Database Documentor for the Categories form. The full report is included on the CD that comes with this book in the CHAPTR18 folder as CATFORM.TXT. Typically, this approach will quickly lead to a severe case of information overload. This report contains a massive amount of information, as a quick perusal of CATFORM.TXT will show.
<BR>
<P><B><A HREF="18vcg03.gif" tppabs="http://202.113.16.101/%7eeb%7e/Database%20Developer's%20Guide%20with%20Visual%20C++%204,%20Second%20Edition/18vcg03.gif">Figure 18.3. Access's Database Documentor showing the Categories form.</A></B>
<BR>
<P>Another approach is to use FMS Inc.'s Total Access documenting application to create printed listings of all the database objects of your Access application. Total Access is described in Chapter 22, "Documenting Your Database Applications."
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>TIP</B>
<BR>
<BR>Whether you choose to use Database Documentor, acquire Total Access, create and print screen shots, or use a yellow pad and pencil, you need to create a written inventory of the objects of the Access application, especially objects that won't be accessible with Visual C++'s data-access classes. You can't open Access forms, reports, macros, or modules in Visual C++ applications.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E229"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Handling Access Object Names in Visual C++</B></FONT></CENTER></H4>
<BR>
<P>Visual C++ uses dialog boxes to emulate Access forms. You can identify a dialog box with either an identifier (which is actually an integer) or a string. The best choice is to use an identifier (which is the Visual C++ default); it offers slightly better performance and better conforms to the Visual C++ standards for dialog box names. The documentation that accompanies Access encourages the use of spaces in form and report names, as well as in the names of other Access database objects. Visual C++ programmers usually use mixed-case names (such as StockReportDialog), because spaces aren't allowed in C and C++ names. Some programmers use underscores (such as stock_report_dialog) or a mixture of underscores and mixed case. Names with spaces or illegal punctuation can be referenced as literal strings enclosed in double quotation marks ("Table Name") but can't be used to name a Visual C++ object. Visual C++ can handle spaces in database object names, but not in dialog box template names. You need to change Access form names that include spaces or special punctuation symbols to create proper Visual C++ object variable names.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE><B>NOTE</B>
<BR>
<BR>If you or the author of the application have complied with the Leszinsky-Reddick (L-R) naming conventions proposed for Access variable and object names, you might want to rename your forms. With few exceptions, the L-R naming convention for Access variable and object names doesn't correspond to the Microsoft object-naming conventions for Visual C++ objects (commonly referred to as <I>Hungarian Notation</I>). Naming conventions for Access objects, based on the L-R conventions, appear in Chapter 3 of the <I>Visual Basic Programmer's Guide</I>. Naming conventions for Visual C++ objects appear in Appendix B, "Naming and Formatting Conventions for Visual C++ Objects and Variables."</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E69E230"></A>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -