按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
{
if (d_pFont) delete d_pFont;
if (d_pnTabs) delete '' d_pnTabs;
if (d_pOpenFile) delete d_pOpenFile;
if (d_pSetFont) delete d_pSetFont;
}
///////////////////////////////////////////////////////////////////////////////
// DMainFrame helper functions。
//…………………………………………………………………………………………………………………………………………………………………………………………………………
// BuildStringArray …Parses text file to create a CString array。
void DMainFrame::BuildStringArray()
{
// Scan buffer to calculate line count。
LPTSTR lpNext = d_lpTextBuffer;
LPTSTR lpEnd = d_lpTextBuffer + d_dwFileLength 1;
*lpEnd = 'n';
for (d_cLines = 0; lpNext 《 lpEnd; d_cLines++; lpNext++)
{
// Search for next character。
lpNext = strchr(lpNext; 'n');
// Discontinue if NULL encountered。
if (lpNext == NULL) break;
}
…………………………………………………………Page 478……………………………………………………………
// Set array size。
d_saTextInfo。SetSize(d_cLines);
// Scan buffer to build array of pointers & sizes。
STRING string;
lpNext = d_lpTextBuffer;
for (int iLine = 0; iLine 《 d_cLines; iLine++)
{
// Char count for current line。
string。ccLen = 0;
string。pText = lpNext;
// Loop to end…of…line。
while ((*lpNext != 'n') && (*lpNext != 'r'))
{
lpNext++; // Check next char。
string。ccLen++; // Increment length counter。
}
// Enter value in array。
d_saTextInfo'iLine' = string;
// Skip over
lpNext += (2 * sizeof(char));
}
}
//…………………………………………………………………………………………………………………………………………………………………………………………………………
// CreateNewFont …Creates new CFont for use in drawing text。
BOOL DMainFrame::CreateNewFont()
{
…………………………………………………………Page 479……………………………………………………………
// Delete any previous font。
if (d_pFont)
delete d_pFont;
// Create a new font
d_pFont = new CFont();
if (!d_pFont) return FALSE;
d_pFont…》CreateFontIndirect(&d_lf);
// Calculate font height
CClientDC dc(this);
TEXTMETRIC tm;
dc。SelectObject(d_pFont);
dc。GetTextMetrics(&tm);
d_cyLineHeight = tm。tmHeight + tm。tmExternalLeading;
// Calculate left margin。
d_cxLeftMargin = tm。tmAveCharWidth * 2;
// Rebuild tab setting table。
if (d_pnTabs) delete '' d_pnTabs;
d_pnTabs = new INT'g_nTabCount';
for (int i=0; iGetPathName();
// Open file in read…only mode。
CFile cfData;
if (!cfData。Open(csFile; CFile::modeRead))
{
AfxMessageBox(_T(〃Error Opening File〃));
return;
}
// Free previous buffer contents
if (d_lpTextBuffer)
{
free(d_lpTextBuffer);
…………………………………………………………Page 482……………………………………………………………
d_lpTextBuffer=0;
}
// Calculate file size & allocate buffer。
// Pad to avoid bad address reference。
d_dwFileLength = cfData。GetLength() + sizeof(char);
d_lpTextBuffer = (LPTSTR)malloc (d_dwFileLength);
if (!d_lpTextBuffer)
{
AfxMessageBox(_T(〃Cannot Allocate Memory For File〃));
return;
《 // Loop through client area coordinates to draw。
int cyLine = 0;
while (iLine 《 cLastLine)
{
// Draw a line of text。
LPTSTR lp = d_saTextInfo'iLine'。pText;
int cc = d_saTextInfo'iLine'。ccLen;
// Drawing function depends on 'respect tabs' setting。
if (d_bTabs && d_pnTabs != 0)
{
dc。TabbedTextOut(d_cxLeftMargin; cyLine; lp; cc;
g_nTabCount; d_pnTabs; 0);
}
else
{
dc。TextOut(d_cxLeftMargin; cyLine; lp; cc);
…………………………………………………………Page 483……………………………………………………………
}
// Increment various counts。
cyLine += d_cyLineHeight;
iLine++;
}
}
//…………………………………………………………………………………………………………………………………………………………………………………………………………
// WM_SIZE message handler。
void DMainFrame::OnSize(UINT nType; int cx; int cy)
{
// Notify base class of new window size。
CFrameWnd ::OnSize(nType; cx; cy);
// Save client area height。
d_cyClient = cy;
// Recalculate scroll bar info。
ResetScrollValues();
}
//…………………………………………………………………………………………………………………………………………………………………………………………………………
// WM_VSCROLL message handler。
void DMainFrame::OnVScroll(UINT nSBCode; UINT nPos; CScrollBar* pScrollBar)
{
// Temporary 〃new line〃 value。
int iTop = d_iTopLine;
// Based on particular scroll button clicked; modify top line index。
switch(nSBCode)
{
…………………………………………………………Page 484……………………………………………………………
case SB_BOTTOM:
iTop = d_cLines d_clHeight;
break;
case SB_ENDSCROLL:
break;
case SB_LINEDOWN:
iTop++;
break;
case SB_LINEUP:
iTop…;
break;
case SB_PAGEDOWN:
iTop += d_clHeight;
break;
case SB_PAGEUP:
iTop …= d_clHeight;
break;
case SB_THUMBPOSITION:
iTop = nPos;
break;
case SB_THUMBTRACK:
iTop = nPos;
break;
case SB_TOP:
iTop = 0;
break;
…………………………………………………………Page 485……………………………………………………………
}
// Check range of new index;
iTop = max (iTop; 0);
iTop = min (iTop; d_cLines d_clHeight);
// If no change; ignore。
if (iTop == d_iTopLine) return;
// Pixels to scroll = (lines to scroll) * height…per…line。
int cyScroll = (d_iTopLine iTop) * d_cyLineHeight;
// Define new top…line value。
d_iTopLine = iTop;
// Scroll pixels。
ScrollWindow(0; cyScroll);
// Adjust scroll thumb position。
SetScrollPos(SB_VERT; d_iTopLine; TRUE);
}
//…………………………………………………………………………………