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
|
//Main.cpp
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <string.h>
#include "WinTypes.h"
long fnWndProc_OnCreate(lpWndEventArgs Wea) //Offset What's Stored There
{ //================================
unsigned int iLineCount=50,i; //0 - 3 iLineCount
TCHAR szBuffer[80], szNum[16]; //4 - 7 ptrPtrBuffer
TCHAR** ptrPtrBuffer=NULL; //8 - 11 cyChar
TEXTMETRIC tm;
HDC hDC;
Wea->hIns=((LPCREATESTRUCT)Wea->lParam)->hInstance;
SetWindowLong(Wea->hWnd,0,iLineCount);
ptrPtrBuffer=(TCHAR**)GlobalAlloc(GPTR,sizeof(TCHAR*)*iLineCount);
SetWindowLong(Wea->hWnd,4,(long)ptrPtrBuffer);
for(i=0;i<iLineCount;i++)
{
_tcscpy(szBuffer,_T(" "));
_stprintf(szNum,_T("%u"),i);
_tcscat(szBuffer,szNum);
_tcscat(szBuffer,_T(" This Is Line #"));
_tcscat(szBuffer,szNum);
ptrPtrBuffer[i]=(TCHAR*)GlobalAlloc(GPTR,sizeof(TCHAR)*(_tcslen(szBuffer)+1));
_tcscpy(ptrPtrBuffer[i],szBuffer);
}
hDC=GetDC(Wea->hWnd);
GetTextMetrics(hDC,&tm);
SetWindowLong(Wea->hWnd,8,(long)tm.tmHeight);
ReleaseDC(Wea->hWnd,hDC);
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;
PAINTSTRUCT ps;
SCROLLINFO si;
long cyChar;
HDC hDC;
hDC=BeginPaint(Wea->hWnd,&ps);
iLineCount=(unsigned int)GetWindowLong(Wea->hWnd,0);
ptrPtrBuffer=(TCHAR**)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;
for(i=iStart;i<=iFinish;i++)
{
iLine=iPos+i;
if(iLine<iLineCount)
TextOut(hDC,0,i*cyChar,ptrPtrBuffer[iLine],_tcslen(ptrPtrBuffer[iLine]));
}
EndPaint(Wea->hWnd,&ps);
return 0;
}
long fnWndProc_OnClose(lpWndEventArgs Wea)
{
unsigned int iLineCount;
TCHAR** ptrPtrBuffer=NULL;
unsigned int i;
iLineCount=(unsigned int)GetWindowLong(Wea->hWnd,0);
ptrPtrBuffer=(TCHAR**)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)
{
TCHAR szClassName[]=TEXT("ScrollWindow");
WNDCLASSEX wc;
MSG messages;
HWND hWnd;
wc.lpszClassName=szClassName; wc.lpfnWndProc=fnWndProc;
wc.cbSize=sizeof (WNDCLASSEX); wc.style=CS_DBLCLKS;
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,200,100,300,228,HWND_DESKTOP,0,hIns,0);
ShowWindow(hWnd,iShow);
UpdateWindow(hWnd);
while(GetMessage(&messages,NULL,0,0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
|