home *** CD-ROM | disk | FTP | other *** search
/ Windows Shareware GOLD / NuclearComputingVol3No1.cdr / utils / f1386 / memory.cpp next >
C/C++ Source or Header  |  1991-04-20  |  5KB  |  159 lines

  1. #include <windows.h>
  2. #include <string.h>
  3.  
  4. #define TIMERID     101
  5.  
  6. long FAR PASCAL MemoryWndProc(HWND,WORD,WORD,LONG);
  7. void NEAR PASCAL InsertCommas(char*,char*);
  8.  
  9. int PASCAL WinMain( HANDLE hInstance,   HANDLE hPrevInstance,
  10.                     LPSTR lpszCmdLine,  int cmdShow)
  11. {
  12.      HWND      hWnd;
  13.      MSG       msg;
  14.      WNDCLASS  wndClass;
  15.      int       xPos,yPos;
  16.      
  17.      if (!hPrevInstance)
  18.      {
  19.           wndClass.lpszClassName   = "MEMORY:MAIN";
  20.           wndClass.hInstance       = hInstance;
  21.           wndClass.lpfnWndProc     = MemoryWndProc;
  22.           wndClass.hCursor         = LoadCursor(NULL,IDC_ARROW);
  23.           wndClass.hIcon           = LoadIcon(hInstance,"iMemory");
  24.           wndClass.lpszMenuName    = NULL;
  25.           wndClass.hbrBackground   = COLOR_WINDOW+1;
  26.           wndClass.style           = NULL;
  27.           wndClass.cbClsExtra      = 0;
  28.           wndClass.cbWndExtra      = 0;
  29.           
  30.           RegisterClass(&wndClass);
  31.      }
  32.                
  33.      xPos=GetProfileInt("MemInfo","XPOS",0);
  34.      yPos=GetProfileInt("MemInfo","YPOS",0);
  35.  
  36.                     
  37.      hWnd=CreateWindow(  "MEMORY:MAIN",
  38.                          "Memory Available",
  39.                          WS_OVERLAPPED|WS_SYSMENU,
  40.                          xPos,
  41.                          yPos,
  42.                          255,
  43.                          GetSystemMetrics(SM_CYCAPTION)+1,
  44.                          NULL,
  45.                          NULL,
  46.                          hInstance,
  47.                          NULL);
  48.                          
  49.      ShowWindow(hWnd,SW_SHOWNORMAL);
  50.      
  51.      while (GetMessage(&msg,0,0,0))
  52.      {
  53.           TranslateMessage(&msg);
  54.           DispatchMessage(&msg);
  55.      }
  56.      
  57.      return 0L;
  58. }
  59.  
  60. long FAR PASCAL MemoryWndProc(     HWND hWnd,     WORD wMsg,
  61.                                    WORD wParam,   LONG lParam)
  62. {
  63.      static DWORD   dwMemFree;
  64.      static int     xPos,yPos;
  65.  
  66.      switch (wMsg)
  67.      {
  68.           case WM_CREATE:
  69.                SetTimer(hWnd,TIMERID,1000,NULL);
  70.                break;                                        
  71.  
  72.           case WM_MOVE:
  73.                xPos=LOWORD(lParam)-1;
  74.                yPos=HIWORD(lParam)-GetSystemMetrics(SM_CYCAPTION);
  75.                                                        
  76.                break;
  77.                
  78.           case WM_TIMER:
  79.                char      acBuff[80],acBuffA[80],acMess[80]="Memory Available: ";
  80.                DWORD     dwTemp;
  81.                
  82.                dwTemp=GetFreeSpace(NULL);
  83.  
  84.                if (dwTemp!=dwMemFree)
  85.                {
  86.                     wsprintf(acBuff,"%lu",dwTemp);
  87.                     InsertCommas(acBuff,acBuffA);
  88.                     
  89.                     strcat(acMess,acBuffA);
  90.                                               
  91.                     SetWindowText(hWnd,acMess);
  92.                     dwMemFree=dwTemp;                    
  93.                }               
  94.                break;
  95.                
  96.           case WM_DESTROY:
  97.                char acX[10],acY[10];
  98.                
  99.                KillTimer(hWnd,TIMERID);
  100.                
  101.                wsprintf(acX,"%d",xPos);
  102.                wsprintf(acY,"%d",yPos);
  103.                
  104.                WriteProfileString("MemInfo","XPOS",acX);
  105.                WriteProfileString("MemInfo","YPOS",acY);
  106.                               
  107.                PostQuitMessage(0);
  108.                break;
  109.                
  110.           default:
  111.                return (DefWindowProc(hWnd,wMsg,wParam,lParam));
  112.                break;
  113.      }
  114.      
  115.      return 0L;
  116. }
  117.  
  118. void NEAR PASCAL InsertCommas(char *acSource,char *acBuff)
  119. {
  120.      int  iCountA,iCountB,iCount,iNum;
  121.      
  122.      if (lstrlen(acSource)<4)
  123.      {
  124.           for (iCount=0;iCount<lstrlen(acSource);iCount++) acBuff[iCount]=acSource[iCount];
  125.           acBuff[iCount]=NULL;
  126.           return;
  127.      }               
  128.      
  129.      iNum=lstrlen(acSource)%3;
  130.      
  131.      for (iCount=0;iCount<iNum;iCount++) acBuff[iCount]=acSource[iCount];
  132.      
  133.      iCountA=iCountB=iCount;
  134.      
  135.      iCount=1;
  136.      
  137.      for (;iCount;)
  138.      {
  139.           if ((iCountA-iNum)%3)
  140.           {
  141.                acBuff[iCountB]=acSource[iCountA];
  142.                iCountB++;
  143.                iCountA++;
  144.           }
  145.           else
  146.           {
  147.                acBuff[iCountB]=',';
  148.                acBuff[iCountB+1]=acSource[iCountA];
  149.                iCountB+=2;
  150.                iCountA++;
  151.           }
  152.           
  153.           if (lstrlen(acSource)==iCountA) iCount=0;
  154.      }
  155.      
  156.      acBuff[iCountB]=NULL;
  157. }
  158.                                              
  159.