#include <windows.h>
#include <string.h>

#define TIMERID     101

long FAR PASCAL MemoryWndProc(HWND,WORD,WORD,LONG);
void NEAR PASCAL InsertCommas(char*,char*);

int PASCAL WinMain( HANDLE hInstance,   HANDLE hPrevInstance,
                    LPSTR lpszCmdLine,  int cmdShow)
{
     HWND      hWnd;
     MSG       msg;
     WNDCLASS  wndClass;
     int       xPos,yPos;
     
     if (!hPrevInstance)
     {
          wndClass.lpszClassName   = "MEMORY:MAIN";
          wndClass.hInstance       = hInstance;
          wndClass.lpfnWndProc     = MemoryWndProc;
          wndClass.hCursor         = LoadCursor(NULL,IDC_ARROW);
          wndClass.hIcon           = LoadIcon(hInstance,"iMemory");
          wndClass.lpszMenuName    = NULL;
          wndClass.hbrBackground   = COLOR_WINDOW+1;
          wndClass.style           = NULL;
          wndClass.cbClsExtra      = 0;
          wndClass.cbWndExtra      = 0;
          
          RegisterClass(&wndClass);
     }
               
     xPos=GetProfileInt("MemInfo","XPOS",0);
     yPos=GetProfileInt("MemInfo","YPOS",0);

                    
     hWnd=CreateWindow(  "MEMORY:MAIN",
                         "Memory Available",
                         WS_OVERLAPPED|WS_SYSMENU,
                         xPos,
                         yPos,
                         255,
                         GetSystemMetrics(SM_CYCAPTION)+1,
                         NULL,
                         NULL,
                         hInstance,
                         NULL);
                         
     ShowWindow(hWnd,SW_SHOWNORMAL);
     
     while (GetMessage(&msg,0,0,0))
     {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
     }
     
     return 0L;
}

long FAR PASCAL MemoryWndProc(     HWND hWnd,     WORD wMsg,
                                   WORD wParam,   LONG lParam)
{
     static DWORD   dwMemFree;
     static int     xPos,yPos;

     switch (wMsg)
     {
          case WM_CREATE:
               SetTimer(hWnd,TIMERID,1000,NULL);
               break;                                        

          case WM_MOVE:
               xPos=LOWORD(lParam)-1;
               yPos=HIWORD(lParam)-GetSystemMetrics(SM_CYCAPTION);
                                                       
               break;
               
          case WM_TIMER:
               char      acBuff[80],acBuffA[80],acMess[80]="Memory Available: ";
               DWORD     dwTemp;
               
               dwTemp=GetFreeSpace(NULL);

               if (dwTemp!=dwMemFree)
               {
                    wsprintf(acBuff,"%lu",dwTemp);
                    InsertCommas(acBuff,acBuffA);
                    
                    strcat(acMess,acBuffA);
                                              
                    SetWindowText(hWnd,acMess);
                    dwMemFree=dwTemp;                    
               }               
               break;
               
          case WM_DESTROY:
               char acX[10],acY[10];
               
               KillTimer(hWnd,TIMERID);
               
               wsprintf(acX,"%d",xPos);
               wsprintf(acY,"%d",yPos);
               
               WriteProfileString("MemInfo","XPOS",acX);
               WriteProfileString("MemInfo","YPOS",acY);
                              
               PostQuitMessage(0);
               break;
               
          default:
               return (DefWindowProc(hWnd,wMsg,wParam,lParam));
               break;
     }
     
     return 0L;
}

void NEAR PASCAL InsertCommas(char *acSource,char *acBuff)
{
     int  iCountA,iCountB,iCount,iNum;
     
     if (lstrlen(acSource)<4)
     {
          for (iCount=0;iCount<lstrlen(acSource);iCount++) acBuff[iCount]=acSource[iCount];
          acBuff[iCount]=NULL;
          return;
     }               
     
     iNum=lstrlen(acSource)%3;
     
     for (iCount=0;iCount<iNum;iCount++) acBuff[iCount]=acSource[iCount];
     
     iCountA=iCountB=iCount;
     
     iCount=1;
     
     for (;iCount;)
     {
          if ((iCountA-iNum)%3)
          {
               acBuff[iCountB]=acSource[iCountA];
               iCountB++;
               iCountA++;
          }
          else
          {
               acBuff[iCountB]=',';
               acBuff[iCountB+1]=acSource[iCountA];
               iCountB+=2;
               iCountA++;
          }
          
          if (lstrlen(acSource)==iCountA) iCount=0;
     }
     
     acBuff[iCountB]=NULL;
}
                                             
