/* SysQueue.C -- Windows application that displays system load*/
/* by Barry Press, adapted from FreeMem written               */
/* by Charles Petzold as printed in Microsoft Systems Journal */

#include <windows.h>	/* all Windows functions */
#include <stdlib.h>	/* ltoa */
#include <string.h>	/* strcat & strlen */

long lLoopCount;

long FAR PASCAL WndProc (HWND, unsigned, WORD, LONG);

/* WinMain - Main loop for all Windows applications */
int PASCAL WinMain ( hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
HANDLE  hInstance, hPrevInstance;
LPSTR   lpszCmdLine;
int     nCmdShow;
{
	static   char szAppName [] = "SysQueue";
	WNDCLASS WndClass;
	HWND     hWnd;
	MSG      msg;

	/* allow only one instance */
	if (hPrevInstance)
	   return FALSE;

	/* define Window class */
	WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
	WndClass.hIcon         = NULL;
	WndClass.cbClsExtra    = 0;
	WndClass.cbWndExtra    = 0;
	WndClass.lpszMenuName  = NULL;
	WndClass.lpszClassName = szAppName;
	WndClass.hbrBackground = GetStockObject( WHITE_BRUSH );
	WndClass.hInstance     = hInstance;
	WndClass.style         = CS_VREDRAW | CS_HREDRAW;
	WndClass.lpfnWndProc   = WndProc;

	/* register this new class with WINDOWS */
	if (!RegisterClass( &WndClass ))
	   return FALSE;   /* Initialization failed */

	/* create window */
	hWnd = CreateWindow( szAppName,
			szAppName,
			WS_TILEDWINDOW,
			0, 0, 0, 0,
			(HWND)   NULL,
			(HMENU)  NULL,
			hInstance,
			NULL);

	/* set up timer (1 update/sec) */
	if (!SetTimer (hWnd, 1, 1000, NULL) )
	   return FALSE;

	/* show window (start in ICONIC state) */
	ShowWindow( hWnd, SHOW_ICONWINDOW );
	UpdateWindow( hWnd );

	/* initialize loop count which is bumped in loop */
	lLoopCount = 0L;

	/* main program loop */
	while (TRUE) {
		lLoopCount++;
		if (PeekMessage( &msg, NULL, 0, 0, TRUE )) {
			if (msg.message == WM_QUIT)
				break;
			TranslateMessage( &msg );
			DispatchMessage( &msg );
		}
	}

	/* exit back to Windows */
	return (int) msg.wParam;

} /* end WinMain */

/* WndProc - Window procedure for main window */
long FAR PASCAL WndProc (hWnd, message, wParam, lParam)
HWND	hWnd;
unsigned message;
WORD	wParam;
LONG	lParam;
{
	static	long lastcount = 0L, count = 0L;
	char	buffer[20];
	PAINTSTRUCT ps;
	RECT	rect;

	/* switch on message type sent to window by Windows dispatcher */
	switch(message) {

	   case WM_TIMER:
	      count = lLoopCount;
	      lLoopCount = 0L;
	      if (count != lastcount)
	         InvalidateRect( hWnd, NULL, TRUE );
	      lastcount = count;
	      break;

	   case WM_PAINT:
	      BeginPaint( hWnd, &ps );
	      GetClientRect( hWnd, &rect );
	      DrawText( ps.hdc, buffer,
	         strlen (strcat (ltoa (count, buffer, 10), " Polls")),
	         &rect, DT_WORDBREAK);
	      EndPaint( hWnd, &ps );
	      break;

	   case WM_DESTROY:
	      KillTimer (hWnd, 1);
	      PostQuitMessage( 0 );
	      break;

	   default:
	      return DefWindowProc( hWnd, message, wParam, lParam);
	}

	return (long) 0;

} /* end WndProc */
