Experience with VC development program (1)

zhaozj2021-02-16  55

This article combines some of the experience of reading the AutoCAD font file to explain some of the experience of the C development program. In this example, read the autocad font file, read its font outline into the custom structure, displayed on the screen.

Figure 1: Project operation results

1: Construction of the project

General small and medium programs that organize a few DLL plus a main program to place different submodules in different works, DLL's dynamic connection is not copied to copy the library code, but in the program records the entry of functions Points and interfaces, load the library code into memory when the program is executed. If there are multiple programs to use the same DLL, simply load the DLL in memory to save the overhead of memory. In addition, for a large, constantly updated application, many repetition functions can be written as a DLL, which is called with the main program, which can reduce the development of the work, and improve access speed.

In this example, the program is divided into processes the AutoCAD font file module (ReadShx project) and the main program module (SHAPE project), where the Shape project only adds a few lines of code, and the application of the Readshx project, the directory structure is as follows:

Figure 2: Project catalog structure

The implementation method of this project is described in detail below.

1: Establish a readshx directory, all code of this program, compiled binary, and the font files used by the AutoCAD in this directory and their subdirectory.

2: Use VC to establish an MFC AppWizard (EXE)-based Shape project, Engineering uses SDI (single document) organization, in the final step of the program, CshapeView is derived from CSCROLLVIEW, and the benefits of CSCROLVIEW derived VIEW class can be convenient Add a scroll bar for the window.

3: Establish a Win32 Dynamic-Link Liberry-based Readshx project, select Add to Current Workspace, select the Dependency of check box. After confirming, select A DLL That Exports Some Symbols. Click to complete. Set the Shape project to an active project.

Selecting the WIN32 instead of the MFC is considering the compatibility of the code in other occasions in the future, this, cannot use the class provided in the MFC, only the Win32API function can be used, the benefit is that the code can be applied in other cases where other MFC can be applied , I think the biggest convenience of using the DLL is that the code reuse is good, the modularity is clear, and the MFC is undoubtedly adding many obstacles to the transplantation of the program.

4: Delete other classes and functions in readshx.cpp, the variables only keep the DLLMAIN function, delete the readshx.h file in the project

5: Select New Class in the ClassView of Readshx Project ... to create a new class CshapeInfo, this class is to span engineering applications, so put it in the public file directory Inc (Figure 2)

6: Remember the readshx.h file that deleted in the project in the fourth step? Open it, copy the following statement to ShapeInfo.h:

#ifdef readshx_exports

#define readshx_api __declspec (dllexport)

#ELSE

#define readshx_api __declspec (dllimport)

#ENDIF

Add readshx_api export classes in the CSHAPEINDEX class:

Class Readshx_API CshapeIndex

{

PUBLIC:

CshapeIndex (); Virtual ~ cshapeIndex ();

}

The readshx_exports is a predefined macro defined in the Readshx Engineering C / C tab in Alt F7, so this macro has been defined in this project, and this macro in other projects will not be defined, so in this project In, readshx_api defines the __Declspec (DLLEXPORT) to export classes, functions, or variables, other engineering readshx_api defined as __DECLSPEC (DLLIMPORT) to import, functions, or variables, completion classes, functions, variables across engineering

Alt F7 opens the engineering settings, change the directory of Output Files in the generals of the two projects to ../debug, select Win32 Release, change the directory to ../release, select Shape Project, Object in the Link tab Addshx.lib add readshx.lib to use the current Readshx.dll project.

The last task adds readshx / debroug's path to Directories in the dialogs in Tools's Options menu pop-up, and pay attention to Readshx refers to the first level instead of the directory of the Readshx project.

You can now create an object in the main project, add statements in the main project's Shape.h:

#include "../inc/shapeinfo.h"

PUBLIC in the CSHAPEAPP class: member variable:

CshapeInfo M_ShapeInfo;

Re-compiling the entire project, there should be no error tips. At this point: the engineering environment is completed.

转载请注明原文地址:https://www.9cbs.com/read-22830.html

New Post(0)