How to write a binary program with VC ++ 60

zhaozj2021-02-11  221

How to write a binary program with VC 60

Thunder Studio Han Yan

---- In computer applications, you often need to view the contents of binary files. Currently, there are many articles that view text files in various VC books, but there are fresh articles that view binary files. This article briefly introduces the functional design, program design, programming implementation, and technical points.

---- 1 Function Design

---- The display interface is shown in Figure 1 (omitted), dividing the window customer area into three parts, the left column is used to display the corresponding position of the file content in a 16-en-manner, and the middle column is used to display the file content in a 16-way method. , The right side is used to display the contents of the ASCII code corresponding to the file content. Designed for simplification, there is no print function.

---- 2 Scheme Design

---- The SDI (single document interface) using MFC. Since the content of the entire file is generally impossible in one screen, the base class of the view class is CScrollView. The readout and processing of binary files are completed in the document class, and the file display and scrolling are implemented by the view class.

---- 3 Programming Implementation

---- 3.1 Using the MFC AppWizard Wizard Generate an application framework in the "File" menu of VC , click New, and pop up a new dialog. Select "MFC AppWizard [EXE]" in the "Projects" page, fill in "HEXSHOW" in the "Project Name" edit box, press the "OK" button to exit the New dialog box. In the MFC AppWizard Step 1 dialog box, select the Single button "Single Document", press the "Next>" button to enter the MFC AppWizard Step 2 of 6 dialog, keep the default, press the "Next>" button, Go to the MFC AppWizard Step 3 of 6 dialog, keep the default selection, press the "Next>" button to enter the "Printing and Print Preview" option (see Figure 3 (slightly ), Press the "Next>" button to enter the "MFC AppWizard Step 5 of 6" dialog box, keep the default setting, continue to press the "Next>" button to enter the "MFC Appwizard Step 6 of 6" dialog box, "Base Select CScrollView in the Class group box (see Figure 4 (omitted), press the "Finish" button to complete the customization of the application framework.

---- 3.2 Add file reading and processing work in document class ChexShowdoc

---- 3.2.1 Define the member variables of the document, do a good job in initialization and cleanup work

---- Open HexShowdoc.h file, add 2 public variables:

CFILE * m_PHEXFILE;

Long M_LFileLength;

INT m_nbytesperline;

// How many Byte is displayed each line and then open the HexShowdoc.cpp file.

---- Add the following initialization code in the constructor of the class:

m_phexfile = NULL;

M_LFileLength = 0L;

m_nbytesperline = 16;

// Each line shows 16 Byte adds the following cleaning code in the destructor of the class:

IF (m_phexfile! = NULL)

{

m_phexfile-> close ();

delete m_phexfile;

m_phexfile = NULL;

}

---- 3.2.2 Open the document in Onoplendocument ()

---- First use the ClassWizard overload message member function onopendocument ().

---- Add the following code at the code adding the member function:

IF (m_phexfile! = NULL)

{

m_phexfile-> close ();

delete m_phexfile;

}

m_phexfile = new cfile (LPSZPATHNAME,

CFILE :: ModeRead | CFILE :: Typebinary;

IF (! m_phexfile)

{

AfxMessageBox ("This file is open");

Return False;

}

M_LFileLength = m_phexfile-> getLength ();

---- 3.2.3 Adding Member Functions for Reading Files and Output Formatting Processing for the ChexShowdoc class Add the member function as follows:

Bool ChexShowdoc :: ReadfileandProcess

(CSTRING & STRLINE, Long Loffset)

{

Long LPOS;

IF (LoffSet! = -1L)

LPOS = m_phexfile-> seek (loffset, cfile :: begin);

Else

LPOS = m_phexfile-> getPosition ();

UNSIGNED Char Szbuf [16];

INT nret = m_phexfile-> read (SZBUF, M_NBYTESPERLINE);

IF (NRET <= 0)

Return False;

CString STEMP;

Cstring schars;

STEMP.FORMAT (_T ("% 8.8lx:"), LPOS);

Strline = step;

For (int i = 0; i

{

IF (i == 0)

STEMP.FORMAT (_t ("% 2.2x"), SZBUF [I]);

ELSE IF (i% 16 == 0)

STEMP.FORMAT (_t ("=% 2.2x"), SZBUF [I]);

ELSE IF (i% 8 == 0)

STEMP.FORMAT (_t ("-% 2.2x"), szbuf [i]);

Else

STEMP.FORMAT (_t ("% 2.2x"), SZBUF [i]);

IF (_istprint (szbuf [i])))

Schars = SZBUF [I];

Else

Schars = _t ('.');

Strline = STEMP;

}

IF (NRET

{

CSTRING SPAD (_t (''),

2 3 * (m_nbytesperline-nret);

Strline = SPAD;

}

Strline = _t ("");

Strline = Schars;

Return True;

}

---- 3.3 Add display file content and handling scroll operations in the view

---- 3.3.1 Variable Definition and Initialization

---- a) Open the HexShowView.h file, adding the member variables as follows:

CFont * m_pfont; // Used to select font for display file content

---- b) Display the content display of the file content in the constructor to select a font called "fixedsys", which makes the characters neatly

Logfont M_LogFont;

MEMSET (& M_Logfont, 0, SizeOf (M_Logfont));

_TCSCPY (m_logfont.lffacename, _t ("fixedsys"));

CClientDC DC (NULL);

m_logfont.lfheight = :: muldiv

(120, DC.GetDeviceCaps (Logpixelsy), 720);

m_logfont.lfpitchandfamily = fixed_pitch;

m_pfont = new cfont;

M_pfont-> CreateFontIndirect (& M_LogFont);

c) Modify the code in ChexShowView :: OnInitialUpdate () to:

CscrollView :: OnInitialupdate ();

ChexShowdoc * pdoc = getDocument ();

Ask_VALID (PDOC);

CSIZE SIZETAl (0, PDOC-> M_LFileLength);

Setscrollsizes (mm_text, sizetotal);

---- c) Complete the deletion of font objects in the desective function, increase the code as follows:

IF (m_pfont! = NULL)

Delete m_pfont;

---- 3.3.2 Add the following code in onDraw () in the view

ChexShowdoc * pdoc = getDocument ();

Ask_VALID (PDOC);

CFont * PoldFont;

CString sline; // for displayed text

CSIZE SCROLLEDSIZE; // Range of the customer area of ​​the window

Int iStartLine; // The index number of the line displayed in the first line of the screen

INT NHEIGHT; / / Outputs the height of the text line

CRECT SCROLLRECT;

// Get the position of the scroll bar

Cpoint scrolledpos = getScrollPosition ();

CRECT RECLIENT;

GetClienceRect (& RectClient);

// Search the height of each row (unit: number of pixels)

TextMetricTM; //TM is used to store parameters of the inventory font;

PDC-> GetTextMetrics; & TM

NHEIGHT = TM.TMHEIGHT;

PoldFont = PDC-> selectObject (m_pfont);

/ / According to scrolling, ask for a start

ScrolledSize = CSIZE (RectClient.width (),

RectClient.height ());

ScrollRect = CRECT (RectClient.Left, ScrolledPos.y,

RectClient.right, ScrolledSize.cy

ScrolledPOS.Y);

iStartLine = scrolledpos.y / 16;

// Make Sure We Are Drawing Where We Should

ScrollRect.top = iStartLine * NHEIGHT;

IF (pdoc-> m_phexfile! = null)

{

INT NLINE;

for (nLINE = istartline; scrollRect.top

IF (! PDOC-> ReadfileandProcess

(Sline, NLINE * 16)))

Break;

NHEIGHT = PDC-> DrawText (sline, -1,

& ScrollRect, DT_TOP |

DT_NOPREFIX | DT_SIINGLINE);

SCROLLRECT.TOP = NHEIGHT;

}

}

PDC-> SELECTOBJECT (POLDFONT);

---- 3.4 Compile, connects to the project, forming operation, actual test, good use.

---- 4 Technical Key

---- Introduction to the above, it is known that the program is not complicated. Its technical critical key is designed.

---- A) Using document / viewing can effectively reduce the complexity of software, make documents focus on processing data, while the text is displayed and scrolled by inheriting from CScrollView;

---- b) Select a suitable font very important, otherwise, there may be chaos;

---- C) Choosing a correct member function can often play a half-time effect, for example, when performing text output, use CDC :: DrawText (...), it is much more than using regular cdc :: textout (...) Big advantage;

---- D) No matter what the scroll bar is in any position, it will only display the text of the text involved.

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

New Post(0)