home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 December / simtel1292_SIMTEL_1292_Walnut_Creek.iso / msdos / mswindws / freedi.arc / FREEDISK.C < prev    next >
C/C++ Source or Header  |  1987-12-30  |  6KB  |  149 lines

  1. /* FreeDisk.C -- WinApp to display free disk space on default drive     */
  2. /* Adapted from "FreeMem.C" (Charles Petzold) by Harold Long.           */
  3. /* Originally printed in Microsoft Systems Journal.                     */
  4.  
  5. /* Assembly & runtime environment:                                      */
  6. /*      MAKE 4.02                                                       */
  7. /*      MSC 4.00                                                        */
  8. /*      LINK4 5.00.12                                                   */
  9. /*      MAP 4.00                                                        */
  10. /*      MS-DOS 3.10                                                     */
  11. /*      Windows 1.03.04                                                 */
  12.  
  13. /* Libraries required:                                                  */
  14. /*      SLIBW.LIB                                                       */
  15. /*      EM.LIB                                                          */
  16. /*      SLIBFP.LIB                                                      */
  17. /*      SLIBC.LIB                                                       */
  18. /*      LIBH.LIB                                                        */
  19.  
  20. #include <windows.h>    /* all Windows functions */
  21. #include <stdlib.h>    /* itoa */
  22. #include <string.h>    /* strcat & strlen */
  23. #include <dos.h>        /* DOS registers */
  24.  
  25. long FAR PASCAL WndProc (HWND, unsigned, WORD, LONG);
  26.  
  27. /* WinMain - Main loop for all Windows applications */
  28. int PASCAL WinMain ( hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  29. HANDLE  hInstance;
  30. LPSTR   lpszCmdLine;
  31. int     nCmdShow;
  32. {
  33.     static   char szAppName [] = "FreeDisk";
  34.     WNDCLASS WndClass;
  35.     HWND     hWnd;
  36.     MSG      msg;
  37.  
  38.     /* allow multiple instances                                     */
  39.  
  40.     /* define Window class */
  41.     WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  42.     WndClass.hIcon         = NULL;
  43.     WndClass.cbClsExtra    = 0;
  44.     WndClass.cbWndExtra    = 0;
  45.     WndClass.lpszMenuName  = NULL;
  46.     WndClass.lpszClassName = (LPSTR) szAppName;
  47.     WndClass.hbrBackground = (HBRUSH) GetStockObject( WHITE_BRUSH );
  48.     WndClass.hInstance     = hInstance;
  49.     WndClass.style         = CS_VREDRAW | CS_HREDRAW;
  50.     WndClass.lpfnWndProc   = WndProc;
  51.  
  52.     /* register this new class with WINDOWS */
  53.     if (!RegisterClass((LPWNDCLASS)&WndClass))
  54.        return FALSE;   /* Initialization failed */
  55.  
  56.     /* create window */
  57.     hWnd = CreateWindow((LPSTR) szAppName,
  58.             (LPSTR)  szAppName,
  59.             WS_TILEDWINDOW,
  60.             0, 0, 0, 0,
  61.             (HWND)   NULL,
  62.             (HMENU)  NULL,
  63.             (HANDLE) hInstance,
  64.             (LPSTR)  NULL);
  65.  
  66.         /* set up timer (1 update/sec) */
  67.     if (!SetTimer (hWnd, 1, 1000, NULL) )
  68.        return FALSE;
  69.  
  70.     /* show window (start in ICONIC state) */
  71.     ShowWindow( hWnd, SHOW_ICONWINDOW );
  72.     UpdateWindow( hWnd );
  73.  
  74.     /* main program loop */
  75.     while (GetMessage ((LPMSG) &msg, NULL, 0, 0)) {
  76.        TranslateMessage((LPMSG) &msg );
  77.        DispatchMessage((LPMSG) &msg );
  78.     } /* end while GetMessage */
  79.  
  80.     /* exit back to Windows */
  81.     return (int) msg.wParam;
  82.  
  83. } /* end WinMain */
  84.  
  85. /* WndProc - Window procedure for main window */
  86. long FAR PASCAL WndProc (hWnd, message, wParam, lParam)
  87. HWND    hWnd;
  88. unsigned message;
  89. WORD    wParam;
  90. LONG    lParam;
  91. {
  92.     static    int lastdisk, byterept;         /* DOS register values  */
  93.         static  char intno, doscall;            /* Interrrupt constants */
  94.         static  long bytecount;                 /* 32 bit precision     */
  95.     char    buffer[20];
  96.     PAINTSTRUCT ps;
  97.     RECT    rect;
  98.         union   REGS inregs;                    /* DOS.H registers      */
  99.         union   REGS outregs;
  100.         
  101.     /* switch on message type sent to window by Windows dispatcher */
  102.     switch(message) {
  103.        case WM_TIMER:
  104.               doscall = 0x36;                   /* get free disk (36H)  */
  105.               intno = 0x21;                     /* system function call */
  106.               inregs.h.ah = doscall;            /* set up AH register   */
  107.               inregs.h.dl = 0;                  /* "default" drive      */
  108.               int86(intno,&inregs,&outregs);    /* interrupt DOS        */
  109.               
  110.               bytecount = outregs.x.bx;         /* available clusters   */
  111.               bytecount = bytecount * outregs.x.ax; /* avail sectors    */
  112.               bytecount = bytecount * outregs.x.cx; /* bytes/sector     */
  113.               bytecount = (bytecount / 1000);   /* avail K bytes        */
  114.  
  115.           if (bytecount != lastdisk)
  116.              InvalidateRect( hWnd, NULL, TRUE );
  117.           lastdisk = (int) bytecount;
  118.           break;
  119.        case WM_PAINT:
  120.           BeginPaint( hWnd, (LPPAINTSTRUCT) &ps );
  121.           GetClientRect( hWnd, (LPRECT) &rect );
  122.               if (bytecount > 9999)             /* more than 10 Mbyte ? */
  123.                 {
  124.                 byterept = (int) (bytecount /1000); /* yes, reset scale */
  125.             DrawText( ps.hdc, (LPSTR) buffer,
  126.                strlen (strcat (itoa (byterept, buffer, 10), " Mbyt")),
  127.                (LPRECT) &rect, DT_WORDBREAK);
  128.                  }
  129.               else
  130.                  {
  131.                  byterept = (int) bytecount;
  132.              DrawText( ps.hdc, (LPSTR) buffer,
  133.                strlen (strcat (itoa (byterept, buffer, 10), " Kbyt")),
  134.                (LPRECT) &rect, DT_WORDBREAK);
  135.                  }
  136.           EndPaint( hWnd, (LPPAINTSTRUCT) &ps );
  137.           break;
  138.        case WM_DESTROY:
  139.           KillTimer (hWnd, 1);
  140.           PostQuitMessage( 0 );
  141.           break;
  142.        default:
  143.           return DefWindowProc( hWnd, message, wParam, lParam);
  144.     } /* end switch */
  145.  
  146.     return (long) 0;
  147.  
  148. } /* end WndProc */
  149.