Code:
#include <windows.h>
#include <stdio.h>
/* number of lines to print */
#define LINES 30
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain(HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_HREDRAW | CS_VREDRAW ;
wincl.cbSize = sizeof(WNDCLASSEX);
wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx(&wincl)) return(0);
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx(
0,
szClassName,
"Matt's Scrollbar Window",
WS_OVERLAPPEDWINDOW | WS_VSCROLL, /* default + scrollbar window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
500, /* The programs width */
350, /* and height in pixels */
NULL,
NULL,
hThisInstance,
NULL);
/* Make the window visible on the screen */
ShowWindow(hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage(&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return(messages.wParam);
}
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int cxChar, cxCaps, cyChar, cyClient, VscrollPos;
int i;
PAINTSTRUCT ps;
char buffer[15];
TEXTMETRIC tm;
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage(0); /* send a WM_QUIT to the message queue */
break;
case WM_CREATE:
{
HDC hdc = GetDC(hwnd);
/* Get the sizes of the system font, for formatting */
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd, hdc);
SetScrollRange(hwnd, SB_VERT, 0, LINES - 1, FALSE);
SetScrollPos(hwnd, SB_VERT, VscrollPos, TRUE);
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
SetTextColor(hdc, RGB(0, 255, 0)); /* set text colour to green */
SetBkColor(hdc, RGB(0, 0, 0)); /* set background colour to black */
/* display the lines of text */
for (i = 0; i < LINES; i++) {
sprintf(buffer, "Line number: %d", i);
TextOut(hdc, 0, cyChar * i, buffer, strlen(buffer));
}
EndPaint(hwnd, &ps);
break;
}
/* taken from 'Programming Windows - Fifth Edition' */
case WM_VSCROLL:
{
switch (LOWORD(wParam))
{
case SB_LINEUP:
VscrollPos -= 1;
break;
case SB_LINEDOWN:
VscrollPos += 1;
break;
case SB_PAGEUP:
VscrollPos -= cyClient / cyChar;
break;
case SB_PAGEDOWN:
VscrollPos += cyClient / cyChar;
break;
case SB_THUMBPOSITION:
VscrollPos = HIWORD(wParam);
break;
default:
break;
}
VscrollPos = max(0, min(VscrollPos, LINES - 1));
if (VscrollPos != GetScrollPos(hwnd, SB_VERT)) {
SetScrollPos(hwnd, SB_VERT, VscrollPos, TRUE);
InvalidateRect(hwnd, NULL, TRUE);
}
break;
}
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return(0);
}
Any help would be much appreciated, I'd like to move onto the next chapter