1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
LRESULT fnWndProc_OnCreate(WndEventArgs& Wea)
{
ScrollData* pScrDta=NULL;
wchar_t szBuffer[512];
wchar_t* cRet=NULL;
HANDLE hHeap=NULL;
HFONT hFont=NULL;
FILE* fp1=NULL;
TEXTMETRIC tm;
int iLen=0,i;
HDC hdc;
hHeap=GetProcessHeap();
pScrDta=(ScrollData*)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,sizeof(ScrollData));
if(!pScrDta)
return -1;
SetWindowLongPtr(Wea.hWnd,0,(LONG_PTR)pScrDta);
hdc = GetDC(Wea.hWnd);
hFont=CreateFont
(
-1*(10*GetDeviceCaps(hdc,LOGPIXELSY))/72,0,0,0,FW_SEMIBOLD,0,0,0,ANSI_CHARSET,0,0,DEFAULT_QUALITY,0,(wchar_t*)L"Lucida Console"
);
if(!hFont)
return -1;
HFONT hTmp=(HFONT)SelectObject(hdc,hFont);
GetTextMetrics(hdc, &tm);
pScrDta->cxChar = tm.tmAveCharWidth;
pScrDta->cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * pScrDta->cxChar / 2;
pScrDta->cyChar = tm.tmHeight + tm.tmExternalLeading;
DeleteObject(SelectObject(hdc,hTmp));
ReleaseDC(Wea.hWnd, hdc);
fp1=_wfopen(L"Main.cpp",L"r");
if(fp1)
{
do
{
cRet=fgetws(szBuffer,512,fp1);
if(!cRet)
break;
else
pScrDta->iNumLines++;
} while(1);
rewind(fp1);
if(pScrDta->iNumLines)
{
pScrDta->pPtrs=(wchar_t**)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, sizeof(wchar_t*) * pScrDta->iNumLines);
if(pScrDta->pPtrs)
{
for(i=0; i<pScrDta->iNumLines; i++)
{
fgetws(szBuffer,512,fp1);
iLen=(int)wcslen(szBuffer);
if(iLen>pScrDta->iMaxWidth)
pScrDta->iMaxWidth=iLen;
pScrDta->pPtrs[i]=(wchar_t*)HeapAlloc(hHeap,HEAP_ZERO_MEMORY, sizeof(wchar_t)*iLen+1*sizeof(wchar_t));
wcscpy(pScrDta->pPtrs[i],szBuffer);
}
pScrDta->iMaxWidth=pScrDta->iMaxWidth*pScrDta->cxChar;
}
}
fclose(fp1);
}
else
return -1;
return 0;
}
LRESULT fnWndProc_OnSize(WndEventArgs& Wea)
{
ScrollData* pScrDta=NULL;
SCROLLINFO si;
pScrDta=(ScrollData*)GetWindowLongPtr(Wea.hWnd,0);
if(pScrDta)
{
pScrDta->cxClient = LOWORD(Wea.lParam);
pScrDta->cyClient = HIWORD(Wea.lParam);
si.cbSize = sizeof(si) ;
si.fMask = SIF_RANGE | SIF_PAGE;
si.nMin = 0;
si.nMax = pScrDta->iNumLines - 1;
si.nPage = pScrDta->cyClient / pScrDta->cyChar;
SetScrollInfo(Wea.hWnd, SB_VERT, &si, TRUE);
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE;
si.nMin = 0;
si.nMax = pScrDta->iMaxWidth / pScrDta->cxChar;
si.nPage = pScrDta->cxClient / pScrDta->cxChar;
SetScrollInfo(Wea.hWnd, SB_HORZ, &si, TRUE);
}
return 0;
}
LRESULT fnWndProc_OnVScroll(WndEventArgs& Wea)
{
ScrollData* pScrDta=NULL;
SCROLLINFO si;
pScrDta=(ScrollData*)GetWindowLongPtr(Wea.hWnd,0);
if(pScrDta)
{
si.cbSize = sizeof(si) ;// Get all the vertial scroll bar information
si.fMask = SIF_ALL ;
GetScrollInfo(Wea.hWnd, SB_VERT, &si);
int iVertPos = si.nPos; // Save the position for comparison later on
switch (LOWORD(Wea.wParam))
{
case SB_TOP:
si.nPos = si.nMin ;
break ;
case SB_BOTTOM:
si.nPos = si.nMax ;
break ;
case SB_LINEUP:
si.nPos -= 1 ;
break ;
case SB_LINEDOWN:
si.nPos += 1 ;
break ;
case SB_PAGEUP:
si.nPos -= si.nPage ;
break ;
case SB_PAGEDOWN:
si.nPos += si.nPage ;
break ;
case SB_THUMBTRACK:
si.nPos = si.nTrackPos ;
break ;
default:
break ;
}
si.fMask = SIF_POS ;
SetScrollInfo(Wea.hWnd, SB_VERT, &si, TRUE);
GetScrollInfo(Wea.hWnd, SB_VERT, &si);
if(si.nPos != iVertPos)
{
ScrollWindow(Wea.hWnd, 0, pScrDta->cyChar*(iVertPos-si.nPos), NULL, NULL);
UpdateWindow(Wea.hWnd);
}
}
return 0;
}
LRESULT fnWndProc_OnHScroll(WndEventArgs& Wea)
{
ScrollData* pScrDta=NULL;
SCROLLINFO si;
pScrDta=(ScrollData*)GetWindowLongPtr(Wea.hWnd,0);
if(pScrDta)
{
si.cbSize = sizeof (si);// Get all the horizontal scroll bar information
si.fMask = SIF_ALL;
GetScrollInfo(Wea.hWnd, SB_HORZ, &si) ;// Save the position for comparison later on
int iHorzPos = si.nPos;
switch (LOWORD(Wea.wParam))
{
case SB_LINELEFT:
si.nPos -= 1 ;
break ;
case SB_LINERIGHT:
si.nPos += 1 ;
break ;
case SB_PAGELEFT:
si.nPos -= si.nPage ;
break ;
case SB_PAGERIGHT:
si.nPos += si.nPage ;
break ;
case SB_THUMBTRACK:
si.nPos = si.nTrackPos ;
break ;
default :
break ;
}
si.fMask = SIF_POS;
SetScrollInfo(Wea.hWnd, SB_HORZ, &si, TRUE);
GetScrollInfo(Wea.hWnd, SB_HORZ, &si);
if(si.nPos != iHorzPos)
ScrollWindow(Wea.hWnd, pScrDta->cxChar*(iHorzPos-si.nPos), 0, NULL, NULL);
}
return 0;
}
LRESULT fnWndProc_OnPaint(WndEventArgs& Wea)
{
int x,y,iPaintBeg,iPaintEnd,iVertPos,iHorzPos;
ScrollData* pScrDta=NULL;
HFONT hFont=NULL;
PAINTSTRUCT ps;
SCROLLINFO si;
HDC hdc;
hdc = BeginPaint(Wea.hWnd, &ps);
pScrDta=(ScrollData*)GetWindowLongPtr(Wea.hWnd,0);
if(pScrDta)
{
hFont=CreateFont
(
-1*(10*GetDeviceCaps(hdc,LOGPIXELSY))/72,0,0,0,FW_SEMIBOLD,0,0,0,ANSI_CHARSET,0,0,DEFAULT_QUALITY,0,(wchar_t*)L"Lucida Console"
);
HFONT hTmp=(HFONT)SelectObject(hdc,hFont);
si.cbSize = sizeof (si) ;// Get vertical scroll bar position
si.fMask = SIF_POS ;
GetScrollInfo(Wea.hWnd, SB_VERT, &si), iVertPos = si.nPos;
GetScrollInfo(Wea.hWnd, SB_HORZ, &si), iHorzPos = si.nPos;
if(iVertPos+ps.rcPaint.top/pScrDta->cyChar>0)
iPaintBeg=iVertPos + ps.rcPaint.top / pScrDta->cyChar;
else
iPaintBeg=0;
if(iVertPos + ps.rcPaint.bottom / pScrDta->cyChar < pScrDta->iNumLines - 1)
iPaintEnd=iVertPos + ps.rcPaint.bottom / pScrDta->cyChar;
else
iPaintEnd=pScrDta->iNumLines-1;
for(int i = iPaintBeg; i<= iPaintEnd; i++)
{
x = pScrDta->cxChar * (1 - iHorzPos);
y = pScrDta->cyChar * (i - iVertPos);
TextOut(hdc, x, y, pScrDta->pPtrs[i], (int)wcslen(pScrDta->pPtrs[i]));
}
DeleteObject(SelectObject(hdc,hTmp));
}
EndPaint(Wea.hWnd, &ps);
return 0;
}
|