Listing 1 Main window procedure for Pulse

#include "pulse.h"
/*
   Routine:    MainWndProc
   Called By:  Windows
   Usage:      This is the window procedure for the main window
*/

long FAR PASCAL MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

       char        szBuffer[BUFMAX+1];
       char        szTitle[BUFMAX+1];
static DWORD       dwIn = 0,
                   dwOut = 0;
static DWORD       MaxTicks = 0,
                   MinTicks = 32767;
      DWORD        Ticks;
      HDC          hDC;
      HMENU        hPulseMenu;
      PAINTSTRUCT  ps;
      POINT        ptClick;
      RECT         rect;

   switch (message)
   {
   /*
   read INI file, reset window position to previous, floating over all other, start clock
   */
     case WM_CREATE:
       GetIni();
          SetWindowPos(hWnd, HWND_TOPMOST, nXPos, nYPos, 100, 50, 0);
       SetTimer(hWnd, ID_TIMER, (UINT)1000, NULL);
       break;
   /*
   save the last position (for restoring on restart)
   */
     case WM_MOVE:
       nXPos = LOWORD(lParam);
       nYPos = HIWORD(lParam);
       wsprintf(szBuffer, "%d", nXPos);
       WriteProfileString("Pulse", "XPos", szBuffer);
       wsprintf(szBuffer, "%d", nYPos);
       WriteProfileString("Pulse", "YPos", szBuffer);
       break;
   /*
   all we do on the timer is redraw our contents
   */
     case WM_TIMER:
       InvalidateRect(hWnd, NULL, TRUE);
       break;
  /*
  RMB creates context menu - selection processed in WM_COMMAND
  */
     case WM_RBUTTONDOWN:
       hPulseMenu = CreatePopupMenu();
       InsertMenu(hPulseMenu, 0, MF_BYPOSITION, MI_PULSE, "&Current");
       InsertMenu(hPulseMenu, 1, MF_BYPOSITION, MI_MAXPULSE, "Ma&x Pulse");
       InsertMenu(hPulseMenu, 2, MF_BYPOSITION, MI_MINPULSE, "Mi&n Pulse");
       ptClick = MAKEPOINT(lParam);
       ClientToScreen(hWnd, &ptClick);
       TrackPopupMenu(hPulseMenu, TPM_LEFTALIGN, ptClick.x, ptClick.y, 0, hWnd, NULL);
       InvalidateRect(hWnd, NULL, TRUE);
       DestroyMenu(hPulseMenu);
       break;

     case WM_COMMAND:
       Mode = wParam;
       wsprintf(szBuffer, "%d", Mode);
       WriteProfileString("Pulse", "Mode", szBuffer);
       break;

     case WM_PAINT:
       dwIn = GetTickCount();
       hDC = BeginPaint(hWnd, &ps);
       GetClientRect(hWnd, &rect);
       if (dwOut)
       {
         SetTextColor(hDC, RGB(0, 0, 255));
         Ticks = dwIn - dwOut;
         switch (Mode)
         {
           case MI_PULSE:
             lstrcpy(szTitle, (LPSTR) "Pulse");
             wsprintf(szBuffer, "%lu Ticks", (DWORD)Ticks);
             break;

           case MI_MAXPULSE:
             lstrcpy(szTitle, (LPSTR)"Max Pulse");
             wsprintf(szBuffer, "%lu Ticks", (DWORD)MaxTicks);
             break;

           case MI_MINPULSE:
             lstrcpy(szTitle, (LPSTR)"Min Pulse");
             wsprintf(szBuffer, "%lu Ticks", (DWORD)MinTicks);
             break;
         }
   /*
   we draw the text centered in the client area
   */
         GetClientRect(hWnd, &rect);
         DrawText(hDC, szBuffer, -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
         SetWindowText(hWnd, szTitle);
   /*
   reset global counters if necessary
   */
         if (Ticks > MaxTicks)
           MaxTicks = Ticks;
         if (Ticks < MinTicks)
           MinTicks = Ticks;
       }
       EndPaint(hWnd, &ps);
       dwOut = GetTickCount();
       break;
   /*
   pressing the home key emulates clicking the RMB
   */
     case WM_KEYDOWN:
       if (wParam == VK_HOME)
         PostMessage(hWnd, WM_RBUTTONDOWN, (WPARAM)0, MAKELPARAM(0, 0));
       break;

     case WM_CLOSE:
       PutIni();
       DestroyWindow(hWnd);
       break;

     case WM_DESTROY:
       PostQuitMessage(0);
       break;

     default:
       return (DefWindowProc(hWnd, message, wParam, lParam));
       break;
   }

   return (NULL);
}