home *** CD-ROM | disk | FTP | other *** search
/ Kompuutteri Kaikille K-CD 2000 #12 / K-CD-2000-12.iso / Tools / diskspace / DISKSPACE.C next >
C/C++ Source or Header  |  1999-11-07  |  11KB  |  376 lines

  1. //Sorry for the poor documentation
  2. //next time I update my page it will be better
  3. #include <windows.h>
  4. #include <windowsx.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <shellapi.h>
  8. #include <stdio.h>
  9. #include "resource.h"
  10.  
  11. //if you want to use the english language
  12. //insert the following line in resource.h
  13. //#define ENGLISH
  14.  
  15. //#undef GetDiskFreeSpaceEx
  16.  
  17. //you should use the GetDiskFreeSpaceEx function on Win95B,Win98
  18. //instead of the GetDiskFreeSpace
  19.  
  20. UINT LOWEST_PERCENTAGE=20;
  21. UINT FLOPPY_PERCENTAGE=20;
  22. UINT HARD_PERCENTAGE=20;
  23. UINT CDROM_PERCENTAGE=0;
  24. UINT DEFAULT_PERCENTAGE=20;
  25. const UINT RefreshTime=1000;
  26. const UINT TimerID=100;
  27. const UINT identity=101;
  28.  
  29. DWORD DriveNo,CurrentDrive=2;
  30. BOOL CheckFloppynCD=FALSE;
  31. char drive[4]="C:\\",DriveString[256],Drives[32][4],TypeString[24]=
  32. #ifndef ENGLISH
  33. "(╙Ωδτ±ⁿ≥ Σ▀≤Ω∩≥)";
  34. #else
  35. "(Hard Disk)";
  36. #endif
  37.  
  38. const int eng=
  39. #ifdef ENGLISH
  40. 1;
  41. #else
  42. 0;
  43. #endif
  44.  
  45. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  46. BOOL Options(HWND);
  47. BOOL ChangeDrive(BOOL);
  48.  
  49. HINSTANCE ghInst;
  50. BOOL Win95OSR2=FALSE;
  51.  
  52. HWND TrackBarFloppy,TrackBarHD,TrackBarCD,CheckBox;
  53.  
  54. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  55.                     char * szCmdLine, int iCmdShow)
  56.     {
  57.     static char szAppName[] = "DiskSpace" ;
  58.     HWND        hwnd ;
  59.     MSG         msg ;
  60.     WNDCLASS     wndclass ;
  61.     OSVERSIONINFO osv;
  62.  
  63.     ghInst=hInstance;
  64.  
  65.     osv.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
  66.     GetVersionEx(&osv);
  67.     if(osv.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS&&LOWORD(osv.dwBuildNumber)>1000)
  68.         Win95OSR2=TRUE;
  69.  
  70.     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  71.     wndclass.lpfnWndProc   = WndProc ;
  72.     wndclass.cbClsExtra    = 0 ;
  73.     wndclass.cbWndExtra    = 0 ;
  74.     wndclass.hInstance     = hInstance ;
  75.     wndclass.hIcon         = LoadIcon(hInstance,MAKEINTRESOURCE(MY_ICON1));
  76.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  77.     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  78.     wndclass.lpszMenuName  = NULL;
  79.     wndclass.lpszClassName = szAppName ;
  80.  
  81.     RegisterClass (&wndclass) ;
  82.  
  83.     hwnd = CreateWindow(szAppName,         /* window class name */
  84.                     "Default Program",     /* window caption */
  85.                     WS_OVERLAPPEDWINDOW,     /* window style */
  86.                     CW_USEDEFAULT,           /* initial x position */
  87.                     CW_USEDEFAULT,           /* initial y position */
  88.                     CW_USEDEFAULT,           /* initial x size */
  89.                     CW_USEDEFAULT,           /* initial y size */
  90.                     NULL,                    /* parent window handle */
  91.                     NULL,                    /* window menu handle */
  92.                     hInstance,               /* program instance handle */
  93.                 NULL) ;
  94.  
  95.     SetPriorityClass(GetCurrentProcess(),IDLE_PRIORITY_CLASS);
  96.  
  97.     while (GetMessage (&msg, NULL, 0, 0))
  98.         {
  99.             TranslateMessage (&msg) ;
  100.             DispatchMessage (&msg) ;
  101.             }
  102.     return msg.wParam ;
  103. }
  104.  
  105. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  106. {
  107.     HICON hIcon;
  108.     HMENU hMenu;
  109.     POINT point;
  110.     BOOL isAvailable;
  111.     NOTIFYICONDATA traydata;
  112.     char buffer[sizeof(traydata.szTip)];
  113.     DWORD cluster,byte,free,total,percentage;
  114.     static UINT low=0,blink=0;
  115.     UINT iconno,res;
  116.     int i,j,k;
  117.     ULARGE_INTEGER fbc,tb,fb;
  118.     __int64 f,t;
  119.     static BOOL WINAPI (*GetDiskFreeSpaceEx)(LPCSTR,ULARGE_INTEGER *,ULARGE_INTEGER *,ULARGE_INTEGER *);
  120.  
  121.  
  122.     switch (iMsg)
  123.         {
  124.             case WM_CREATE :
  125.                 DriveNo=GetLogicalDriveStrings(sizeof(DriveString),DriveString)/4;
  126.                 for(i=0;i<DriveNo;i++)
  127.                     for(j=0;j<4;j++)
  128.                         Drives[i][j]=DriveString[4*i+j];
  129.                 for(i=0;i<DriveNo;i++){
  130.                     k=1;
  131.                     for(j=0;j<4;j++)
  132.                         k*=toupper(drive[j])==toupper(Drives[i][j]);
  133.                     if(k){
  134.                         CurrentDrive=i;
  135.                         break;
  136.                     }
  137.                 }
  138.                 if(Win95OSR2){
  139.                     GetDiskFreeSpaceEx=GetProcAddress(LoadLibrary("kernel32.dll"),"GetDiskFreeSpaceExA");
  140.                     isAvailable=GetDiskFreeSpaceEx(drive,&fbc,&tb,&fb);
  141.                 }
  142.                 else isAvailable=GetDiskFreeSpace(drive,&cluster,&byte,&free,&total);
  143.                 if(isAvailable==TRUE){
  144.                     if(Win95OSR2){
  145.                         f=fb.LowPart+0x100000000*fb.HighPart;
  146.                         t=tb.LowPart+0x100000000*tb.HighPart;
  147.                         percentage=100.0*(double)f/(double)t;
  148.                     }
  149.                     else percentage=(100.0*(double)free)/(double)total;
  150.                     iconno=MY_ICON9-(int)(percentage/12.5+0.5);
  151.                     if(percentage<LOWEST_PERCENTAGE)low=1;
  152.                     sprintf(buffer,eng==0?"╫■±∩≥ ≤⌠∩ %s %s: %i%% σδσ\xfdΦσ±∩":
  153.                     "Space in %s %s: %i%% free",drive,TypeString,percentage);
  154.                     traydata.cbSize=sizeof(NOTIFYICONDATA);
  155.                     traydata.hWnd=hwnd;
  156.                     traydata.uID=identity;
  157.                     traydata.uFlags=NIF_MESSAGE | NIF_TIP | NIF_ICON;
  158.                     traydata.uCallbackMessage=WM_MY_TRAY;
  159.                     strncpy(traydata.szTip,buffer,sizeof(traydata.szTip));
  160.                     hIcon=LoadIcon(ghInst,MAKEINTRESOURCE(iconno));
  161.                     traydata.hIcon=hIcon;
  162.                     Shell_NotifyIcon(NIM_ADD,&traydata);
  163.                     DestroyIcon(hIcon);
  164.                 }
  165.                 SetTimer(hwnd,TimerID,RefreshTime,NULL);
  166.                 return 0 ;
  167.  
  168.             case WM_MY_TRAY:
  169.                 switch(lParam){
  170.                     case WM_LBUTTONDBLCLK:
  171.                         SetForegroundWindow(hwnd);
  172.                         Options(hwnd);
  173.                         break;
  174.  
  175.                     case WM_MBUTTONDOWN:
  176.                         ChangeDrive(TRUE);
  177.                         break;
  178.  
  179.                     case WM_RBUTTONUP:
  180.                         GetCursorPos(&point);
  181.                         hMenu=CreatePopupMenu();
  182.                         AppendMenu(hMenu,MFS_DEFAULT,MENU_OPTIONS,eng==0?"┼&≡Θδ∩π▌≥":"Options");
  183.                         AppendMenu(hMenu,MF_SEPARATOR,0,"");
  184.                         AppendMenu(hMenu,MFS_ENABLED,MENU_EXIT,eng==0?"╕&ε∩Σ∩≥":"Exit");
  185.                         SetMenuDefaultItem(hMenu,MENU_OPTIONS,FALSE);
  186.                         res=TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_RETURNCMD | TPM_RIGHTBUTTON,
  187.                             point.x,point.y,0,hwnd,NULL);
  188.                         switch(res){
  189.                             case MENU_EXIT:
  190.                                 DestroyWindow(hwnd);
  191.                                 break;
  192.  
  193.                             case MENU_OPTIONS:
  194.                                 SetForegroundWindow(hwnd);
  195.                                 Options(hwnd);
  196.                                 break;
  197.                             }
  198.                         DestroyMenu(hMenu);
  199.                         break;
  200.                     }
  201.                 return 0;
  202.  
  203.             case WM_TIMER:
  204.                 if(Win95OSR2)isAvailable=GetDiskFreeSpaceEx(drive,&fbc,&tb,&fb);
  205.                 else isAvailable=GetDiskFreeSpace(drive,&cluster,&byte,&free,&total);
  206.                 if(isAvailable==TRUE){
  207.                     if(Win95OSR2){
  208.                         f=fb.LowPart+0x100000000*fb.HighPart;
  209.                         t=tb.LowPart+0x100000000*tb.HighPart;
  210.                         percentage=100.0*(double)f/(double)t;
  211.                     }
  212.                     else percentage=(100.0*(double)free)/(double)total;
  213.                     if(percentage<LOWEST_PERCENTAGE)low=1;
  214.                     else low=0;
  215.                     iconno=MY_ICON9-(int)(percentage/12.5+0.5);
  216.                     sprintf(buffer,eng==0?"╫■±∩≥ ≤⌠∩ %s %s : %i%% σδσ\xfdΦσ±∩":
  217.                         "Space in %s %s: %i%% free",drive,TypeString,percentage);
  218.                     traydata.cbSize=sizeof(NOTIFYICONDATA);
  219.                     traydata.hWnd=hwnd;
  220.                     traydata.uID=identity;
  221.                     traydata.uFlags=NIF_MESSAGE | NIF_TIP | NIF_ICON;
  222.                     traydata.uCallbackMessage=WM_MY_TRAY;
  223.                     strncpy(traydata.szTip,buffer,sizeof(traydata.szTip));
  224.                     hIcon=LoadIcon(ghInst,low==1&&blink==0?MAKEINTRESOURCE(iconno+9):
  225.                         MAKEINTRESOURCE(iconno));
  226.                     blink=blink==0&&low==1?1:0;
  227.                     traydata.hIcon=hIcon;
  228.                     Shell_NotifyIcon(NIM_MODIFY,&traydata);
  229.                     DestroyIcon(hIcon);
  230.                 }
  231.                 return 0;
  232.  
  233.  
  234.             case WM_DESTROY :
  235.                 traydata.cbSize=sizeof(NOTIFYICONDATA);
  236.                 traydata.hWnd=hwnd;
  237.                 traydata.uID=identity;
  238.                 traydata.uFlags=NIF_TIP;
  239.                 traydata.uCallbackMessage=WM_MY_TRAY;
  240.                 strncpy(traydata.szTip,"",sizeof(traydata.szTip));
  241.                 traydata.hIcon=NULL;
  242.                 Shell_NotifyIcon(NIM_DELETE,&traydata);
  243.                 KillTimer(hwnd,TimerID);
  244.                 PostQuitMessage (0) ;
  245.                 return 0 ;
  246.             }
  247.  
  248.     return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  249.     }
  250.  
  251. BOOL CALLBACK OptionsDialogProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam){
  252.     char buffer[8];
  253.     HWND ScrollBar;
  254.     UINT BarNo,PerCent;
  255.     UINT ID=WM_USER+1;
  256.  
  257.     switch(uMsg){
  258.  
  259.         case WM_INITDIALOG:
  260.             InitCommonControls();
  261.             TrackBarFloppy=CreateWindowEx(0,TRACKBAR_CLASS,NULL,
  262.                 WS_CHILD|WS_VISIBLE|TBS_AUTOTICKS,
  263.                 143, 75, 150, 28,hDlg,NULL,(HINSTANCE)GetWindowLong(hDlg,GWL_HINSTANCE),NULL);
  264.             TrackBarHD=CreateWindowEx(0,TRACKBAR_CLASS,NULL,WS_CHILD|WS_VISIBLE,
  265.                 143, 140, 150, 18,hDlg,NULL,(HINSTANCE)GetWindowLong(hDlg,GWL_HINSTANCE),NULL);
  266.             TrackBarCD=CreateWindowEx(0,TRACKBAR_CLASS,NULL,WS_CHILD|WS_VISIBLE,
  267.                 143, 205, 150, 18,hDlg,NULL,(HINSTANCE)GetWindowLong(hDlg,GWL_HINSTANCE),NULL);
  268.             CheckDlgButton(hDlg,120,(UINT)CheckFloppynCD);
  269.             SendMessage(TrackBarFloppy, TBM_SETRANGE,(WPARAM)TRUE,(LPARAM)MAKELONG(0,10));
  270.             SendMessage(TrackBarFloppy,TBM_SETPOS,(WPARAM)TRUE,(LPARAM)FLOPPY_PERCENTAGE/10);
  271.             SendMessage(TrackBarHD,TBM_SETPOS,(WPARAM)TRUE,(LPARAM)HARD_PERCENTAGE);
  272.             SendMessage(TrackBarCD,TBM_SETPOS,(WPARAM)TRUE,(LPARAM)CDROM_PERCENTAGE);
  273.             sprintf(buffer,"%i %%",FLOPPY_PERCENTAGE);
  274.             SetDlgItemText(hDlg,105,buffer);
  275.             sprintf(buffer,"%i %%",HARD_PERCENTAGE);
  276.             SetDlgItemText(hDlg,106,buffer);
  277.             sprintf(buffer,"%i %%",CDROM_PERCENTAGE);
  278.             SetDlgItemText(hDlg,107,buffer);
  279.             break;
  280.  
  281.             case WM_HSCROLL:
  282.                 ScrollBar=(HWND)lParam;
  283.                 if(ScrollBar==TrackBarFloppy)
  284.                     BarNo=105;
  285.                 else if(ScrollBar==TrackBarHD)
  286.                     BarNo=106;
  287.                 else if(ScrollBar==TrackBarCD)
  288.                     BarNo=107;
  289.                 PerCent=SendMessage(ScrollBar,TBM_GETPOS,0,0);
  290.                 sprintf(buffer,"%i %%",BarNo==105?PerCent*10:PerCent);
  291.                 SetDlgItemText(hDlg,BarNo,buffer);
  292.                 return FALSE;
  293.             break;
  294.  
  295.         case WM_COMMAND:
  296.             switch(LOWORD(wParam)){
  297.  
  298.                 case IDOK:
  299.                     FLOPPY_PERCENTAGE=10*SendMessage(TrackBarFloppy,TBM_GETPOS,0,0);
  300.                     HARD_PERCENTAGE=SendMessage(TrackBarHD,TBM_GETPOS,0,0);
  301.                     CDROM_PERCENTAGE=SendMessage(TrackBarCD,TBM_GETPOS,0,0);
  302.                     CheckFloppynCD=IsDlgButtonChecked(hDlg,120);
  303.                     ChangeDrive(FALSE);
  304.                     EndDialog(hDlg,TRUE);
  305.                     return TRUE;
  306.  
  307.                 case IDCANCEL:
  308.                     EndDialog(hDlg,TRUE);
  309.                     return TRUE;
  310.                     break;
  311.  
  312.                 case IDHELP:
  313.                     WinHelp(hDlg,NULL,HELP_HELPONHELP,0);
  314.                     return TRUE;
  315.                     break;
  316.                 }
  317.             break;
  318.  
  319.         case WM_DESTROY:
  320.             DestroyWindow(TrackBarFloppy);
  321.             DestroyWindow(TrackBarHD);
  322.             DestroyWindow(TrackBarCD);
  323.             break;
  324.  
  325.         default:
  326.             return FALSE;
  327.         }
  328.     return TRUE;
  329. }
  330.  
  331. BOOL Options(HWND hwnd){
  332.     DialogBox(ghInst,MAKEINTRESOURCE(OPTIONS_DIALOG),NULL,OptionsDialogProc);
  333.     return TRUE;
  334. }
  335.  
  336. BOOL ChangeDrive(BOOL Change){
  337.     BOOL available;
  338.     DWORD a,b,c,d;
  339.     UINT DriveType;
  340.     if(Change==TRUE)CurrentDrive=CurrentDrive==DriveNo-1?0:CurrentDrive+1;
  341.     strncpy(drive,Drives[CurrentDrive],sizeof(drive));
  342.     DriveType=GetDriveType(drive);
  343.     switch(DriveType){
  344.         case DRIVE_REMOVABLE:
  345.             if(CheckFloppynCD==TRUE){
  346.                 LOWEST_PERCENTAGE=FLOPPY_PERCENTAGE;
  347.                 strncpy(TypeString,eng==0?"(─Θ≤Ω▌⌠ß)":"(Floppy)",sizeof TypeString);
  348.             }
  349.             else
  350.                 ChangeDrive(TRUE);
  351.             break;
  352.  
  353.         case DRIVE_FIXED:
  354.             LOWEST_PERCENTAGE=HARD_PERCENTAGE;
  355.             strncpy(TypeString,eng==0?"(╙Ωδτ±ⁿ≥ Σ▀≤Ω∩≥)":"(Hard Disk)",sizeof TypeString);
  356.             break;
  357.  
  358.         case DRIVE_CDROM:
  359.             if(CheckFloppynCD==TRUE){
  360.                 LOWEST_PERCENTAGE=CDROM_PERCENTAGE;
  361.                 strncpy(TypeString,"(CD-ROM)",sizeof TypeString);
  362.             }
  363.             else
  364.                 ChangeDrive(TRUE);
  365.             break;
  366.  
  367.         default:
  368.             LOWEST_PERCENTAGE=DEFAULT_PERCENTAGE;
  369.             strncpy(TypeString,eng==0?"(óπφ∙≤⌠∩)":"Unknown",sizeof TypeString);
  370.             break;
  371.     }
  372.     available=GetDiskFreeSpace(drive,&a,&b,&c,&d);
  373.     if(available!=TRUE)ChangeDrive(TRUE);
  374.     return TRUE;
  375. }
  376.