ÄãµÄλÖãºÈí¼þÓë¹âÅÌ>
VC5´òÓ¡×ÖÌåµÄ¿ØÖÆ
2000-01-30 00:00:00¡¤ ¸ßÖÞÆ½¡¤CPCW

C5.0 ΪWindows µÄ³ÌÐòÔ±ÌṩÁËÒ»¸öºÜºÃµÄC++ ¿ª·¢»·¾³£¬¼õÉÙÁ˺ܶà±à³Ì¸ºµ££¬µ«Í¬Ê±Ò²ÎªÎÒÃÇÔÚ³ÌÐòÖмÓÈë×Ô¼ºµÄ˼ÏëÔö¼ÓÁËÄѶȡ£±¾ÈËÔÚÒ»Èí¼þ¿ª·¢ÖУ¬Ïë¿ØÖÆÎÄ×Ö´òӡʱµÄ×ÖÌ壬ʹ×ÖÌå´óСËõСһ±¶£¬ÒÔ½ÚÊ¡´òÓ¡Ö½¡£¾­¹ýÒ»¶Îʱ¼äµÄÃþË÷£¬ÖÕÓÚ½â¾öÁËÕâÒ»ÎÊÌ⣬ÏÂÃæ·Ö¼¸²½Ïò´ó¼Ò×öÒ»½éÉÜ¡£

---- Ò»¡¢¶ÔVC5 ×Ô¶¯Éú³ÉµÄ³ÌÐò¿ò¼Ü½øÐиĽø

---- ÕâÀïÓÃVC5 ×Ô¶¯´´½¨Ò»¸öÀý×Ó³ÌÐòTest£¬µ¥Îĵµ½çÃæ£¬×¢ÒâÔÚ×îºóÒ»²½ÐÞ¸Äview µÄ¼Ì³ÐÀàΪCEditView¡£

---- ÔÚview ÀàÖУ¬VC5 ÒѾ­×Ô¶¯´´½¨ÁËÈý¸öÓÃÓÚÖ§³Ö´òÓ¡µÄº¯Êý£ºOnPreparePrinting£¬OnBeginPrinting£¬OnEndPrinting¡£ÎªÁËʵÏÖÎÒÃǵŦÄÜ£¬ÐèÒªÔټ̳ÐÒÔϼ¸¸öº¯Êý£ºOnPrepareDC£¬OnPrint¡£²¢½«OnPrepareDC ºÍOnEndPrinting ¸ÄΪÈçÏÂʵÏÖ£º


// OnPrepareDC()
void CTestView::OnPrepareDC
(CDC* pDC, CPrintInfo* pInfo)
{
CView::OnPrepareDC(pDC, pInfo);
}

// OnEndPrinting()
void CTestView::OnEndPrinting
(CDC* pDC, CPrintInfo* pInfo)
{
CView::OnEndPrinting(pDC, pInfo);
}
---- ÓÃCView À´Ìæ´úÔ­À´µÄCEditView£¬ÓÃÒÔ±ÜÃâCEidtView ¶Ô´òÓ¡µÄ¿ØÖÆ¡£¿ØÖÆ×ÖÌå¼°Êä³öµÄ¹¦ÄÜÖ÷ÒªÔÚOnBeginPrinting ºÍOnPrint Á½¸öº¯ÊýÀ´ÊµÏÖ¡£

---- ¶þ¡¢ÊµÏÖOnBeginPrinting º¯Êý

---- ¸ù¾ÝVC5 ±à³Ì»úÖÆ£¬ÔÚOnBeginPrinting º¯ÊýʵÏÖ´òӡǰµÄ×¼±¸¹¤×÷£¬°üÀ¨ÉèÖôòÓ¡×ÖÌ壬¸ù¾Ý´òÓ¡»úµ±Ç°Ò³Ãæ³ß´ç¼ÆËãËùÐèÒ³ÊýµÈ¡£ÏÂÃæµÄ³ÌÐòÊǶԴòÓ¡×ÖÌåµÄÖØÐÂÉèÖúͼÆËãËùÐè´òÓ¡Ö½Ò³Êý¡£

---- ³ÌÐòÖÐÊ×ÏÈÈ¡µÃ´òÓ¡»úµÄºáÏòºÍ×ÝÏò·Ö±æÂÊ£¬Ôٵõ½µ±Ç°´òÓ¡×ÖÌåµÄ´óС£¬È»ºó¼ÆËã³öеÄ×ÖÌå´óС£¬ÎªÄ¬ÈÏ×ÖÌåµÄÒ»°ë¡£¶ÁÕß¿ÉÒÔ¸ù¾ÝÐèÒªÉ趨×Ô¼ºµÄ´òÓ¡×ÖÌå´óС¡£

---- ½Ó×Å£¬È¡µÃµ±Ç°´òÓ¡Ö½µÄ¿í¶ÈºÍ¸ß¶È£¬ÔÙ¸ù¾ÝÐÂ×ÖÌåµÄ¿í¶ÈºÍ¸ß¶È¼ÆËã³öÿÐеÄ×î´ó×Ö·ûÊýºÍÿҳµÄ×î´óÐÐÊý¡£

---- ÓÉÓÚ´òÓ¡ÎļþÖÐÓÐЩÐеĿí¶È¿ÉÄܳ¬¹ýÿÐеÄ×î´ó×Ö·ûÊý£¬ËùÒÔ³ÌÐòÖе÷Óú¯ÊýRedealTextData() ¶Ô´òÓ¡Îļþ½øÐÐÖØÐÂÕûÀí£¬º¯ÊýµÄʵÏÖÔÚÏÂÃæ½éÉÜ¡£

---- ×îºó£¬³ÌÐòÖмÆËã²¢ÉèÖÃËùÐèµÄ´òÓ¡Ò³Êý¡£

OnBeginPrinting()º¯ÊýʵÏÖÈçÏ£º
//====================================
// OnBeginPrinting
//====================================
void CTestView::OnBeginPrinting(CDC* pDC,
CPrintInfo* pInfo)
{
//ÉèÖÃеÄË ×ÖÌå////////////////

//È¡´òÓ¡»úµÄºá·½ÏòºÍ×Ý·½ÏòµÄ·Ö±æÂÊ
//¼´Ã¿Ó¢´çµãÊý
short cxInch = pDC- >GetDeviceCaps(LOGPIXELSX);
short cyInch = pDC- >GetDeviceCaps(LOGPIXELSY);

// È¡µ±Ç°×ÖÌå´óС
CFont *curFont = pDC- >GetCurrentFont();
LOGFONT curLogFont;
LOGFONT newLogFont;

curFont- >GetLogFont( &curLogFont );
long NewFontWidth = curLogFont.lfWidth;
long NewFontHeight = curLogFont.lfHeight;
newLogFont = curLogFont;

//¼ÆËãеÄ×ÖÌå´óС--ËõСһ±¶
newLogFont.lfWidth =(long)((float)NewFontWidth/2.0
* ((float)cxInch / 72.0));
newLogFont.lfHeight =(long)((float)NewFontHeight/2.0
* ((float)cyInch / 72.0));

//´´½¨²¢ÉèÖÃеÄ×ÖÌ壬±£ÁôÒÔǰµÄ×ÖÌå
CFont newFont;
CFont *oldFont;

newFont.CreateFontIndirect(&newLogFont);
oldFont = pDC- >SelectObject(&newFont );
/////////////////////////////////
//¸ù¾Ý×ÖÌå¿í¶È¡¢¸ß¶È¼ÆËã
//ÿÐÐ×î´ó×ÖÊý¼°Ã¿Ò³×î´óÐÐÊý

//È¡´òÓ¡Ö½ÕŸ߶ȺͿí¶È
int nPageHeight, nPageWidth;
nPageHeight = pDC- >GetDeviceCaps(VERTRES);
nPageWidth = pDC- >GetDeviceCaps(HORZRES);

TEXTMETRIC TextM;
pDC- >GetTextMetrics(&TextM);
//×ÖÌå¸ß¶È
m_LineHeight = (unsigned short)TextM.tmHeight;
//×ÖÌ寽¾ù¿í¶È
m_CharWidth=(unsigned short)
TextM.tmAveCharWidth;

//ÿÐÐ×î´ó×ÖÊý
m_MaxLineChar = nPageWidth / m_CharWidth - 8;
//ÿҳ×î´óÐÐÊý
m_LinesPerPage = nPageHeight/ m_LineHeight;
//¸ù¾ÝÿÐÐ×î´ó×ÖÊý¶ÔÎÄ×Ö½øÐÐÖØÐµ÷Õû
RedealTextData();
//////////////////////////////////////
//¼ÆËãËùÐè´òÓ¡Ö½ÕÅÊýÄ¿
int nPrintableLineCount = INT_MAX/m_LineHeight;

// m_linesΪÎļþ×ÜÐÐÊý
if (m_lines < nPrintableLineCount)
nPrintableLineCount = m_lines;
unsigned short MaxPage = (nPrintableLineCount
+ m_LinesPerPage - 1)
/ m_LinesPerPage;
//ÉèÖÃËùÐè´òÓ¡Ö½ÕÅÊýÄ¿
pInfo- >SetMaxPage(MaxPage);
pInfo- >m_nCurPage = 1;

//////////////////////////////////////////
//×îºó²»ÒªÍü¼Ç½«×ÖÌ廹ԭ£¬ÕâÒ»¾äÊDZØÐèµÄ
pDC- >SelectObject(oldFont );
}
---- RedealTextData º¯Êý¸ù¾ÝÿÐÐ×î´ó¿í¶È¶ÔÎļþ½øÐÐÖØÐµ÷Õû¡£Ö÷ÒªÊǼÆËãÎļþÖÐÿÐеĿí¶È£¬Èç¹û³¬¹ý×î´ó¿í¶ÈÔò¼ÓÈë»»Ðзû£¨0x0d,0x0a£©¡£º¯ÊýʵÏÖÈçÏ£º


//=======================================
// RedealTextData
//×¢£º
//pDoc- >bufferΪÎļþ»º³åÇø
//pDoc- >file_lengthΪÎļþ×Ö½Ú³¤¶È
//pDoc- >TextLinesΪÎļþÔ­ÐÐÊý
//pDoc- >MaxLineLengthΪÎļþÔ­×î´óÐÐ×Ö½Ú¿í¶È
//=======================================
void CTextView::RedealTextData()
{
CDocViewDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

short LineLengthMax = m_MaxLineChar;
unsigned short lines=0;
unsigned long i,j;

//ÉêÇëÐµĻº³åÇø±£´æµ÷ÕûºóµÄÎļþ
long size = pDoc- >file_length + pDoc- >TextLines*
(pDoc- >MaxLineLength/m_MaxLineChar+1);
m_newBuffer = new char [size ];
LPSTR newTempPtr = m_newBuffer;
m_file_length =pDoc- >file_length;
//±£´æÎļþеÄÐÐÊý
m_lines = 1;
i = 0;
//¼Ç¼µ±Ç°ÐеĿí¶È
short theLineLength=0;
//¼Ç¼µ±Ç°ÐÐÖкº×Ö×Ö½ÚÊý£¬
//ÒÔ·ÀÖ¹½«Ò»°ëºº×Ö·ÖΪÁ½ÐÐ
unsigned short halfChinese=0;

while(i < pDoc- >file_length)
{
*newTempPtr++ = pDoc- >buffer[i];

j=i+1;
if( (pDoc- >buffer[i] == 0x0d && pDoc- >buffer[j] == 0x0a))
{
m_lines++;
theLineLength = 0;
}
else
{
//Èç¹ûÊÇTAB×Ö·û£¬¿í¶È¼Ó8
if(pDoc- >buffer[i] == VK_TAB)
theLineLength += 8;
else
{
//´óÓÚ0xa1µÄ×Ö½ÚΪºº×Ö×Ö½Ú
if((unsigned char)pDoc- >buffer[i] >= 0xa1)
halfChinese++;
theLineLength++;
}
//Èç¹ûÐпí´óÓÚÿÐÐ×î´ó¿í¶È£¬½øÐÐÌØÊâ´¦Àí
if(theLineLength > LineLengthMax)
{
char buff[256];
short m=255;
newTempPtr--;
if((unsigned char )*newTempPtr < 0xa1)
{
//Èç¹ûµ±Ç°×Ö·ûµÄǰһ¸ö×Ö·ûÊÇÊý×Ö¡¢
//×Öĸ»òÒ»Ð©ÌØÊâµÄǰÖ÷ûºÅʱ£¬
//Ö¸ÕëÑ­»·Ïòǰȡ£¬
//ÒÔ·ÀÖ¹½«Ò»¸öµ¥´Ê·ÖΪÁ½ÐС£
while((*newTempPtr >=0 && *newTempPtr< =9)||
(*newTempPtr >=a && *newTempPtr < = z) ||
(*newTempPtr >=A && *newTempPtr < = Z) ||
*newTempPtr == _ || *newTempPtr == * ||
*newTempPtr == ^ || *newTempPtr == ~ )
buff[m--] = *newTempPtr--;
}
else //ºº×Ö
{
//·ÀÖ¹½«Ò»¸öºº×Ö·ÖΪÁ½ÐС£
if(halfChinese%2)
buff[m--] = *newTempPtr--;
}
newTempPtr++;
//¼ÓÈë»»Ðзû£¬·ÖΪÁ½ÐÐ
*newTempPtr++ = 0x0d;
*newTempPtr++ = 0x0a;
for(short k=m+1; k< 256; k++)
*newTempPtr++ = buff[k];

m_lines++;
theLineLength = 0;
m_file_length += 2;
}
}
i++;
}
}
---- Èý¡¢ÊµÏÖOnPrint º¯Êý

---- ÔÚOnPrint º¯ÊýÖÐʵÏÖÕæÕýµÄÎÄ×ÖÊä³ö£¬Ö÷Òª¹¦ÄܰüÀ¨ÉèÖôòÓ¡×ÖÌå´óС£¬¼ÆË㵱ǰҳºÅÎÄ×ÖÊä³öλÖã¬ÒÔ¼°ÎÄ×ÖµÄÊä³ö´òÓ¡¡£

---- ³ÌÐòÖÐÊ×ÏȼÆËã´òÓ¡ÇøÓò£¬ÎÄ×ÖÔÚÕâ¸ö´òÓ¡ÇøÓòÄÚÊä³ö¡£È»ºóÉèÖÃеĴòÓ¡×ÖÌå¡£

---- ÓÉÓÚOnPrint º¯ÊýÊÇÿ´òÓ¡Ò»Ò³±»µ÷ÓÃÒ»´Î£¬ËùÒÔÐèÒª¸ù¾Ýµ±Ç°´òÓ¡Ò³ºÅ¼ÆËã³öµ±Ç°Ò³µÄÎÄ×ÖÔÚÕû¸öÎÄ×Ö»º³åÇøµÄÆðÊ¼Æ«ÒÆÁ¿ºÍÖÕÖ¹Æ«ÒÆÁ¿¡£ÕâÀï³ÌÐòÖе÷ÓÃÁ˺¯ÊýGetOffset()£¬´Ëº¯ÊýÔÚÏÂÃæ½éÉÜ¡£

---- ×îºóµ÷ÓÃWindows µÄDrawText() º¯ÊýʵÏÖÎÄ×ÖµÄÊä³ö¡£

OnPrint()º¯ÊýʵÏÖÈçÏ£º
//====================================
// OnPrint
//========================================
void CTestView::OnPrint(CDC* pDC,
CPrintInfo* pInfo)
{
//¼ÆËã´òÓ¡ÇøÓò//////////////////
long yTopOfPage =(pInfo- >m_nCurPage -1) *
m_LinesPerPage * m_LineHeight;

//×ó±ß¿Õ³öÁ½¸ö×Ö·û¿í¶È
pDC- >SetViewportOrg(m_CharWidth * 2,
-yTopOfPage);

int nPageWidth = pDC- >GetDeviceCaps(HORZRES);
CRect rectClip = CRect(0,
yTopOfPage,
nPageWidth,
yTopOfPage + m_LinesPerPage *
m_LineHeight);

/////ÉèÖÃËõС×ÖÌå///////////////////
//È¡´òÓ¡»úµÄºá·½ÏòºÍ×Ý·½ÏòµÄ·Ö±æÂÊ
//¼´Ã¿Ó¢´çµãÊý
short cxInch=pDC- >GetDeviceCaps(LOGPIXELSX);
short cyInch= DC- >GetDeviceCaps(LOGPIXELSY);

//È¡µ±Ç°×ÖÌå´óС
CFont *curFont = pDC- >GetCurrentFont();
LOGFONT curLogFont;
LOGFONT newLogFont;

curFont- >GetLogFont( &curLogFont );
long NewFontWidth = curLogFont.lfWidth;
long NewFontHeight = curLogFont.lfHeight;
newLogFont = curLogFont;

//¼ÆËãеÄ×ÖÌå´óС--ËõСһ±¶
newLogFont.lfWidth = (long)((float)NewFontWidth/2.0
* ((float)cxInch / 72.0));
newLogFont.lfHeight = (long)((float)NewFontHeight/2.0
* ((float)cyInch / 72.0));

//´´½¨²¢ÉèÖÃеÄ×ÖÌ壬±£ÁôÒÔǰµÄ×ÖÌå
CFont newFont;
CFont *oldFont;
newFont.CreateFontIndirect(&newLogFont);
oldFont = pDC- >SelectObject(&newFont );

/////ÎÄ×Ö´òÓ¡Êä³ö/////////////////
unsigned short CurrentStartLine ,
CurrentEndLine;
long StartPrintOffset,
EndPrintOffset,
PrintSize;
LPSTR tempPtr;
RECT rect1,rect2;
//¸ù¾Ýµ±Ç°´òÓ¡Ò³ºÅ¼ÆËãÎÄ×ÖÆðʼÐÐ
CurrentStartLine=(pInfo- >m_nCurPage-1) * m_LinesPerPage;
//ÎÄ×ÖÖÕÖ¹ÐÐ
CurrentEndLine = CurrentStartLine+m_LinesPerPage;

if(CurrentEndLine > m_lines)
CurrentEndLine = m_lines;
//¼ÆËã´òÓ¡ÎÄ×ֵįðʼλÖúÍÖÕֹλÖÃ
StartPrintOffset=GetOffset(m_newBuffer,
m_file_length, CurrentStartLine);
EndPrintOffset = GetOffset(m_newBuffer,
m_file_length,CurrentEndLine);

PrintSize = EndPrintOffset - StartPrintOffset;

tempPtr = m_newBuffer + StartPrintOffset;
//ÎÄ×ÖÊä³ö
pDC- >DrawText(tempPtr, PrintSize,
&rectClip,
DT_NOCLIP |DT_NOPREFIX
|DT_EXPANDTABS);

//»¹Ô­¾ÉµÄ´òÓ¡×ÖÌå
pDC- >SelectObject(oldFont );
}
---- ³ÌÐòÖеÄGetOffset º¯ÊýÊǸù¾Ý¸ø¶¨µÄÐкżÆËãÎÄ×ÖµÄλÖã¬ÆäʵÏÖÈçÏ£º


//========================================
// GetOffset ()
//========================================
long CTestView::GetOffset(LPSTR buffer,
long buffer_length,
unsigned short StartLine)
{
if(StartLine == 0) return 0;

unsigned short lines=0;
long i,j;

i = 0;
while(i < buffer_length)
{
j=i+1;
if( (buffer[i++] == 0x0d && buffer[j] == 0x0a))
{
lines++;
if(lines == StartLine)
return i;
}
}
return buffer_length;
}
---- ÒÔÉÏÊDZ¾ÈËÔÚ±à³ÌÖеÄÒ»µãÐĵ㬻¶Ó­ºÍ´ó¼Ò¹²Í¬½»Á÷¡£


¡¾·¢±íÆÀÂÛ¡¿¡¾¹Ø±Õ´°¿Ú¡¿