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
|
//Main.cpp
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "WinTypes.h"
long fnWndProc_OnCreate(lpWndEventArgs Wea) //Offset What's Stored There
{ //================================
unsigned int iLineCount=0,i,iRet=0; //0 - 3 iLineCount
char szBuffer[176]; //4 - 7 ptrPtrBuffer
char** ptrPtrBuffer=NULL; //8 - 11 cyChar
HFONT hFont,hTmp;
FILE* fp1=NULL;
TEXTMETRIC tm;
HDC hDC;
Wea->hIns=((LPCREATESTRUCT)Wea->lParam)->hInstance;
fp1=fopen("Main.cpp","r");
if(fp1)
{
do
{
iRet=(int)fgets(szBuffer,176,fp1);
if(!iRet)
break;
else
iLineCount++;
}while(1);
rewind(fp1);
if(iLineCount)
{
SetWindowLong(Wea->hWnd,0,iLineCount);
ptrPtrBuffer = (char**)GlobalAlloc(GPTR, sizeof(char*) * iLineCount);
if(ptrPtrBuffer)
{
SetWindowLong(Wea->hWnd,4, (long)ptrPtrBuffer);
for(i=0; i<iLineCount; i++)
{
fgets(szBuffer,176,fp1);
ptrPtrBuffer[i]=(char*)GlobalAlloc(GPTR,sizeof(char)*(strlen(szBuffer)+1));
strcpy(ptrPtrBuffer[i],szBuffer);
}
}
hDC=GetDC(Wea->hWnd);
hFont=CreateFont(18,0,0,0,FW_BOLD,0,0,0,0,0,0,2,0,"Courier New");
hTmp=(HFONT)SelectObject(hDC,hFont);
GetTextMetrics(hDC,&tm);
SetWindowLong(Wea->hWnd,8,(long)tm.tmHeight);
DeleteObject(SelectObject(hDC,hTmp));
ReleaseDC(Wea->hWnd,hDC);
}
else
{
fclose(fp1);
return -1; //will cause CreateWindow() down in WinMain() to fail.
}
fclose(fp1);
}
else
return -1; //will cause CreateWindow() down in WinMain() to fail.
return 0;
}
long fnWndProc_OnSize(lpWndEventArgs Wea)
{
int iLinesVisible,iLineCount;
unsigned int cyChar;
SCROLLINFO si;
ZeroMemory(&si, sizeof(SCROLLINFO));
iLineCount=(unsigned int)GetWindowLong(Wea->hWnd,0);
cyChar=(unsigned int)GetWindowLong(Wea->hWnd,8);
iLinesVisible=HIWORD(Wea->lParam)/cyChar;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_RANGE|SIF_PAGE;
si.nMin = 0;
si.nMax = iLineCount-1;
si.nPage=iLinesVisible;
if(si.nMax<0)
si.nMax=0;
SetScrollInfo(Wea->hWnd,SB_VERT,&si,TRUE);
if(iLinesVisible<=iLineCount)
InvalidateRect(Wea->hWnd,NULL,TRUE);
return 0;
}
long fnWndProc_OnVScroll(lpWndEventArgs Wea)
{
int iVScrollPos;
SCROLLINFO si;
ZeroMemory(&si, sizeof(SCROLLINFO));
si.cbSize = sizeof(SCROLLINFO);
si.fMask=SIF_ALL;
GetScrollInfo(Wea->hWnd,SB_VERT,&si);
iVScrollPos=si.nPos;
switch(LOWORD(Wea->wParam))
{
case SB_LINEUP:
if(si.nPos>si.nMin)
si.nPos--;
break;
case SB_PAGEUP:
si.nPos = si.nPos - si.nPage;
break;
case SB_LINEDOWN:
if(si.nPos<si.nMax)
si.nPos++;
break;
case SB_PAGEDOWN:
si.nPos = si.nPos + si.nPage;
break;
case SB_THUMBTRACK:
si.nPos=si.nTrackPos;
break;
}
si.fMask = SIF_POS; // Set the position and then retrieve it. Due to adjustments
SetScrollInfo(Wea->hWnd, SB_VERT, &si, TRUE); // by Windows it may not be the same as the value set.
GetScrollInfo (Wea->hWnd, SB_VERT, &si);
if(si.nPos != iVScrollPos) // If the position has changed, scroll the window and update it
ScrollWindow(Wea->hWnd, 0,GetWindowLong(Wea->hWnd,8)*(iVScrollPos-si.nPos), NULL, NULL);
return 0;
}
long fnWndProc_OnPaint(lpWndEventArgs Wea)
{
unsigned int iLineCount,i,iStart,iFinish,iLine,iPos;
TCHAR** ptrPtrBuffer=NULL;
HFONT hFont,hTmp;
PAINTSTRUCT ps;
SCROLLINFO si;
long cyChar;
HDC hDC;
hDC=BeginPaint(Wea->hWnd,&ps);
iLineCount=(unsigned int)GetWindowLong(Wea->hWnd,0);
ptrPtrBuffer=(char**)GetWindowLong(Wea->hWnd,4);
cyChar=GetWindowLong(Wea->hWnd,8);
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_POS;
GetScrollInfo(Wea->hWnd, SB_VERT, &si);
iPos=si.nPos;
iStart=ps.rcPaint.top/cyChar;
iFinish=ps.rcPaint.bottom/cyChar;
hFont=CreateFont(18,0,0,0,FW_BOLD,0,0,0,0,0,0,2,0,"Courier New");
hTmp=(HFONT)SelectObject(hDC,hFont);
SetBkMode(hDC,TRANSPARENT);
for(i=iStart;i<=iFinish;i++)
{
iLine=iPos+i;
if(iLine<iLineCount)
TextOut(hDC,0,i*cyChar,ptrPtrBuffer[iLine],strlen(ptrPtrBuffer[iLine])-1);
}
DeleteObject(SelectObject(hDC,hTmp));
EndPaint(Wea->hWnd,&ps);
return 0;
}
long fnWndProc_OnClose(lpWndEventArgs Wea)
{
unsigned int iLineCount;
char** ptrPtrBuffer=NULL;
unsigned int i;
iLineCount=(unsigned int)GetWindowLong(Wea->hWnd,0);
ptrPtrBuffer=(char**)GetWindowLong(Wea->hWnd,4);
for(i=0; i<iLineCount; i++)
GlobalFree(ptrPtrBuffer[i]);
GlobalFree(ptrPtrBuffer);
DestroyWindow(Wea->hWnd);
PostQuitMessage(0);
return 0;
}
LRESULT CALLBACK fnWndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
WndEventArgs Wea;
for(unsigned int i=0; i<dim(EventHandler); i++)
{
if(EventHandler[i].Code==msg)
{
Wea.hWnd=hwnd, Wea.lParam=lParam, Wea.wParam=wParam;
return (*EventHandler[i].fnPtr)(&Wea);
}
}
return (DefWindowProc(hwnd,msg,wParam,lParam));
}
int WINAPI WinMain(HINSTANCE hIns,HINSTANCE hPrevIns,LPSTR lpszArgument,int iShow)
{
char szClassName[]="ScrollWindow";
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
wc.lpszClassName=szClassName; wc.lpfnWndProc=fnWndProc;
wc.cbSize=sizeof (WNDCLASSEX); wc.style=0;
wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hInstance=hIns;
wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION); wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wc.cbWndExtra=12;
wc.lpszMenuName=NULL; wc.cbClsExtra=0;
RegisterClassEx(&wc);
hWnd=CreateWindow(szClassName,szClassName,WS_OVERLAPPEDWINDOW|WS_VSCROLL,50,50,1200,900,HWND_DESKTOP,0,hIns,0);
ShowWindow(hWnd,iShow);
UpdateWindow(hWnd);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
|