/* IconAnm.C -- Windows application that displays 
 * an animated icon by Tony Vollmer for SDK 2.00 */

#include <windows.h>	/* all Windows functions */

long FAR PASCAL WndProc (HWND, unsigned, WORD, LONG);

HANDLE hInst; /* save for SetClassWord and GetClassWord */
int nIcon;   /* save current icon resource number */

/* WinMain - Main loop for all Windows applications */
int PASCAL WinMain (hInstance,
                    hPrevInstance,
                    lpszCmdLine, 
                    nCmdShow)
HANDLE  hInstance, hPrevInstance;
LPSTR   lpszCmdLine;
int     nCmdShow;
{
	static   char szAppName [] = "IconAnm";
	WNDCLASS WndClass;
	HWND     hWnd;
	MSG      msg;

	/* allow only one instance */
	if (hPrevInstance)
	   return FALSE;

	/* Save hInstance */
	hInst = hInstance;
	nIcon = 1000;
	/* define Window class */
	WndClass.hCursor       = NULL;
	WndClass.hIcon         = LoadIcon(hInstance,
	                         MAKEINTRESOURCE(nIcon));
	WndClass.cbClsExtra    = 0;
	WndClass.cbWndExtra    = 0;
	WndClass.lpszMenuName  = NULL;
	WndClass.lpszClassName = (LPSTR) szAppName;
	WndClass.hbrBackground = COLOR_WINDOW + 1;
	WndClass.hInstance     = hInstance;
	WndClass.style         = CS_VREDRAW | CS_HREDRAW;
	WndClass.lpfnWndProc   = WndProc;

	/* register this new class with WINDOWS */
	if (!RegisterClass((LPWNDCLASS)&WndClass))
	   return FALSE;   /* Initialization failed */

	/* create window */
	hWnd = CreateWindow((LPSTR) szAppName,
			(LPSTR)  NULL,
			WS_OVERLAPPEDWINDOW,
			CW_USEDEFAULT,
			0,
			0,
			CW_USEDEFAULT,
			(HWND)   NULL,
			(HMENU)  NULL,
			hInstance,
			(LPSTR)  NULL);

	/* set up timer (10 updates/sec) */
	if (!SetTimer (hWnd, 1, 100, NULL) )
	{
	   MessageBox(hWnd,
	              (LPSTR)"Too Many Timers!",
	              (LPSTR)szAppName,
	              MB_ICONEXCLAMATION | MB_OK);
	   return FALSE;
	}

	/* show window (start in ICONIC state) */
	ShowWindow( hWnd, SW_SHOWMINNOACTIVE );
	UpdateWindow( hWnd );

	/* main program loop */
	while (GetMessage ((LPMSG) &msg, NULL, 0, 0)) {
	   TranslateMessage((LPMSG) &msg );
	   DispatchMessage((LPMSG) &msg );
	} /* end while GetMessage */

	/* 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;
{

	/* switch on message type sent to window by Windows dispatcher */
	switch(message) {
	   case WM_TIMER:
	      /* change icon between 1000, 1001, 1002, 1003 */
	      nIcon = ((++nIcon-1000) % 4) + 1000;
	      SetClassWord(hWnd,
			   GCW_HICON,
			   LoadIcon(hInst,MAKEINTRESOURCE(nIcon)));
	      InvalidateRect(hWnd,NULL,TRUE);
	      UpdateWindow(hWnd);
	      break;
	   case WM_QUERYOPEN:
	      /* prevent opening of icon */
	      break;
	   case WM_DESTROY:
	      KillTimer (hWnd, 1);
	      PostQuitMessage( 0 );
	      break;
	   default:
	      return DefWindowProc( hWnd, message, wParam, lParam);
	} /* end switch */

	return (long) 0;

} /* end WndProc */
