home *** CD-ROM | disk | FTP | other *** search
- //Sorry for the poor documentation
- //next time I update my page it will be better
- #include <windows.h>
- #include <windowsx.h>
- #include <string.h>
- #include <ctype.h>
- #include <shellapi.h>
- #include <stdio.h>
- #include "resource.h"
-
- //if you want to use the english language
- //insert the following line in resource.h
- //#define ENGLISH
-
- //#undef GetDiskFreeSpaceEx
-
- //you should use the GetDiskFreeSpaceEx function on Win95B,Win98
- //instead of the GetDiskFreeSpace
-
- UINT LOWEST_PERCENTAGE=20;
- UINT FLOPPY_PERCENTAGE=20;
- UINT HARD_PERCENTAGE=20;
- UINT CDROM_PERCENTAGE=0;
- UINT DEFAULT_PERCENTAGE=20;
- const UINT RefreshTime=1000;
- const UINT TimerID=100;
- const UINT identity=101;
-
- DWORD DriveNo,CurrentDrive=2;
- BOOL CheckFloppynCD=FALSE;
- char drive[4]="C:\\",DriveString[256],Drives[32][4],TypeString[24]=
- #ifndef ENGLISH
- "(╙Ωδτ±ⁿ≥ Σ▀≤Ω∩≥)";
- #else
- "(Hard Disk)";
- #endif
-
- const int eng=
- #ifdef ENGLISH
- 1;
- #else
- 0;
- #endif
-
- LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
- BOOL Options(HWND);
- BOOL ChangeDrive(BOOL);
-
- HINSTANCE ghInst;
- BOOL Win95OSR2=FALSE;
-
- HWND TrackBarFloppy,TrackBarHD,TrackBarCD,CheckBox;
-
- int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
- char * szCmdLine, int iCmdShow)
- {
- static char szAppName[] = "DiskSpace" ;
- HWND hwnd ;
- MSG msg ;
- WNDCLASS wndclass ;
- OSVERSIONINFO osv;
-
- ghInst=hInstance;
-
- osv.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
- GetVersionEx(&osv);
- if(osv.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS&&LOWORD(osv.dwBuildNumber)>1000)
- Win95OSR2=TRUE;
-
- wndclass.style = CS_HREDRAW | CS_VREDRAW ;
- wndclass.lpfnWndProc = WndProc ;
- wndclass.cbClsExtra = 0 ;
- wndclass.cbWndExtra = 0 ;
- wndclass.hInstance = hInstance ;
- wndclass.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(MY_ICON1));
- wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
- wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
- wndclass.lpszMenuName = NULL;
- wndclass.lpszClassName = szAppName ;
-
- RegisterClass (&wndclass) ;
-
- hwnd = CreateWindow(szAppName, /* window class name */
- "Default Program", /* window caption */
- WS_OVERLAPPEDWINDOW, /* window style */
- CW_USEDEFAULT, /* initial x position */
- CW_USEDEFAULT, /* initial y position */
- CW_USEDEFAULT, /* initial x size */
- CW_USEDEFAULT, /* initial y size */
- NULL, /* parent window handle */
- NULL, /* window menu handle */
- hInstance, /* program instance handle */
- NULL) ;
-
- SetPriorityClass(GetCurrentProcess(),IDLE_PRIORITY_CLASS);
-
- while (GetMessage (&msg, NULL, 0, 0))
- {
- TranslateMessage (&msg) ;
- DispatchMessage (&msg) ;
- }
- return msg.wParam ;
- }
-
- LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
- {
- HICON hIcon;
- HMENU hMenu;
- POINT point;
- BOOL isAvailable;
- NOTIFYICONDATA traydata;
- char buffer[sizeof(traydata.szTip)];
- DWORD cluster,byte,free,total,percentage;
- static UINT low=0,blink=0;
- UINT iconno,res;
- int i,j,k;
- ULARGE_INTEGER fbc,tb,fb;
- __int64 f,t;
- static BOOL WINAPI (*GetDiskFreeSpaceEx)(LPCSTR,ULARGE_INTEGER *,ULARGE_INTEGER *,ULARGE_INTEGER *);
-
-
- switch (iMsg)
- {
- case WM_CREATE :
- DriveNo=GetLogicalDriveStrings(sizeof(DriveString),DriveString)/4;
- for(i=0;i<DriveNo;i++)
- for(j=0;j<4;j++)
- Drives[i][j]=DriveString[4*i+j];
- for(i=0;i<DriveNo;i++){
- k=1;
- for(j=0;j<4;j++)
- k*=toupper(drive[j])==toupper(Drives[i][j]);
- if(k){
- CurrentDrive=i;
- break;
- }
- }
- if(Win95OSR2){
- GetDiskFreeSpaceEx=GetProcAddress(LoadLibrary("kernel32.dll"),"GetDiskFreeSpaceExA");
- isAvailable=GetDiskFreeSpaceEx(drive,&fbc,&tb,&fb);
- }
- else isAvailable=GetDiskFreeSpace(drive,&cluster,&byte,&free,&total);
- if(isAvailable==TRUE){
- if(Win95OSR2){
- f=fb.LowPart+0x100000000*fb.HighPart;
- t=tb.LowPart+0x100000000*tb.HighPart;
- percentage=100.0*(double)f/(double)t;
- }
- else percentage=(100.0*(double)free)/(double)total;
- iconno=MY_ICON9-(int)(percentage/12.5+0.5);
- if(percentage<LOWEST_PERCENTAGE)low=1;
- sprintf(buffer,eng==0?"╫■±∩≥ ≤⌠∩ %s %s: %i%% σδσ\xfdΦσ±∩":
- "Space in %s %s: %i%% free",drive,TypeString,percentage);
- traydata.cbSize=sizeof(NOTIFYICONDATA);
- traydata.hWnd=hwnd;
- traydata.uID=identity;
- traydata.uFlags=NIF_MESSAGE | NIF_TIP | NIF_ICON;
- traydata.uCallbackMessage=WM_MY_TRAY;
- strncpy(traydata.szTip,buffer,sizeof(traydata.szTip));
- hIcon=LoadIcon(ghInst,MAKEINTRESOURCE(iconno));
- traydata.hIcon=hIcon;
- Shell_NotifyIcon(NIM_ADD,&traydata);
- DestroyIcon(hIcon);
- }
- SetTimer(hwnd,TimerID,RefreshTime,NULL);
- return 0 ;
-
- case WM_MY_TRAY:
- switch(lParam){
- case WM_LBUTTONDBLCLK:
- SetForegroundWindow(hwnd);
- Options(hwnd);
- break;
-
- case WM_MBUTTONDOWN:
- ChangeDrive(TRUE);
- break;
-
- case WM_RBUTTONUP:
- GetCursorPos(&point);
- hMenu=CreatePopupMenu();
- AppendMenu(hMenu,MFS_DEFAULT,MENU_OPTIONS,eng==0?"┼&≡Θδ∩π▌≥":"Options");
- AppendMenu(hMenu,MF_SEPARATOR,0,"");
- AppendMenu(hMenu,MFS_ENABLED,MENU_EXIT,eng==0?"╕&ε∩Σ∩≥":"Exit");
- SetMenuDefaultItem(hMenu,MENU_OPTIONS,FALSE);
- res=TrackPopupMenu(hMenu,TPM_LEFTALIGN | TPM_RETURNCMD | TPM_RIGHTBUTTON,
- point.x,point.y,0,hwnd,NULL);
- switch(res){
- case MENU_EXIT:
- DestroyWindow(hwnd);
- break;
-
- case MENU_OPTIONS:
- SetForegroundWindow(hwnd);
- Options(hwnd);
- break;
- }
- DestroyMenu(hMenu);
- break;
- }
- return 0;
-
- case WM_TIMER:
- if(Win95OSR2)isAvailable=GetDiskFreeSpaceEx(drive,&fbc,&tb,&fb);
- else isAvailable=GetDiskFreeSpace(drive,&cluster,&byte,&free,&total);
- if(isAvailable==TRUE){
- if(Win95OSR2){
- f=fb.LowPart+0x100000000*fb.HighPart;
- t=tb.LowPart+0x100000000*tb.HighPart;
- percentage=100.0*(double)f/(double)t;
- }
- else percentage=(100.0*(double)free)/(double)total;
- if(percentage<LOWEST_PERCENTAGE)low=1;
- else low=0;
- iconno=MY_ICON9-(int)(percentage/12.5+0.5);
- sprintf(buffer,eng==0?"╫■±∩≥ ≤⌠∩ %s %s : %i%% σδσ\xfdΦσ±∩":
- "Space in %s %s: %i%% free",drive,TypeString,percentage);
- traydata.cbSize=sizeof(NOTIFYICONDATA);
- traydata.hWnd=hwnd;
- traydata.uID=identity;
- traydata.uFlags=NIF_MESSAGE | NIF_TIP | NIF_ICON;
- traydata.uCallbackMessage=WM_MY_TRAY;
- strncpy(traydata.szTip,buffer,sizeof(traydata.szTip));
- hIcon=LoadIcon(ghInst,low==1&&blink==0?MAKEINTRESOURCE(iconno+9):
- MAKEINTRESOURCE(iconno));
- blink=blink==0&&low==1?1:0;
- traydata.hIcon=hIcon;
- Shell_NotifyIcon(NIM_MODIFY,&traydata);
- DestroyIcon(hIcon);
- }
- return 0;
-
-
- case WM_DESTROY :
- traydata.cbSize=sizeof(NOTIFYICONDATA);
- traydata.hWnd=hwnd;
- traydata.uID=identity;
- traydata.uFlags=NIF_TIP;
- traydata.uCallbackMessage=WM_MY_TRAY;
- strncpy(traydata.szTip,"",sizeof(traydata.szTip));
- traydata.hIcon=NULL;
- Shell_NotifyIcon(NIM_DELETE,&traydata);
- KillTimer(hwnd,TimerID);
- PostQuitMessage (0) ;
- return 0 ;
- }
-
- return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
- }
-
- BOOL CALLBACK OptionsDialogProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam){
- char buffer[8];
- HWND ScrollBar;
- UINT BarNo,PerCent;
- UINT ID=WM_USER+1;
-
- switch(uMsg){
-
- case WM_INITDIALOG:
- InitCommonControls();
- TrackBarFloppy=CreateWindowEx(0,TRACKBAR_CLASS,NULL,
- WS_CHILD|WS_VISIBLE|TBS_AUTOTICKS,
- 143, 75, 150, 28,hDlg,NULL,(HINSTANCE)GetWindowLong(hDlg,GWL_HINSTANCE),NULL);
- TrackBarHD=CreateWindowEx(0,TRACKBAR_CLASS,NULL,WS_CHILD|WS_VISIBLE,
- 143, 140, 150, 18,hDlg,NULL,(HINSTANCE)GetWindowLong(hDlg,GWL_HINSTANCE),NULL);
- TrackBarCD=CreateWindowEx(0,TRACKBAR_CLASS,NULL,WS_CHILD|WS_VISIBLE,
- 143, 205, 150, 18,hDlg,NULL,(HINSTANCE)GetWindowLong(hDlg,GWL_HINSTANCE),NULL);
- CheckDlgButton(hDlg,120,(UINT)CheckFloppynCD);
- SendMessage(TrackBarFloppy, TBM_SETRANGE,(WPARAM)TRUE,(LPARAM)MAKELONG(0,10));
- SendMessage(TrackBarFloppy,TBM_SETPOS,(WPARAM)TRUE,(LPARAM)FLOPPY_PERCENTAGE/10);
- SendMessage(TrackBarHD,TBM_SETPOS,(WPARAM)TRUE,(LPARAM)HARD_PERCENTAGE);
- SendMessage(TrackBarCD,TBM_SETPOS,(WPARAM)TRUE,(LPARAM)CDROM_PERCENTAGE);
- sprintf(buffer,"%i %%",FLOPPY_PERCENTAGE);
- SetDlgItemText(hDlg,105,buffer);
- sprintf(buffer,"%i %%",HARD_PERCENTAGE);
- SetDlgItemText(hDlg,106,buffer);
- sprintf(buffer,"%i %%",CDROM_PERCENTAGE);
- SetDlgItemText(hDlg,107,buffer);
- break;
-
- case WM_HSCROLL:
- ScrollBar=(HWND)lParam;
- if(ScrollBar==TrackBarFloppy)
- BarNo=105;
- else if(ScrollBar==TrackBarHD)
- BarNo=106;
- else if(ScrollBar==TrackBarCD)
- BarNo=107;
- PerCent=SendMessage(ScrollBar,TBM_GETPOS,0,0);
- sprintf(buffer,"%i %%",BarNo==105?PerCent*10:PerCent);
- SetDlgItemText(hDlg,BarNo,buffer);
- return FALSE;
- break;
-
- case WM_COMMAND:
- switch(LOWORD(wParam)){
-
- case IDOK:
- FLOPPY_PERCENTAGE=10*SendMessage(TrackBarFloppy,TBM_GETPOS,0,0);
- HARD_PERCENTAGE=SendMessage(TrackBarHD,TBM_GETPOS,0,0);
- CDROM_PERCENTAGE=SendMessage(TrackBarCD,TBM_GETPOS,0,0);
- CheckFloppynCD=IsDlgButtonChecked(hDlg,120);
- ChangeDrive(FALSE);
- EndDialog(hDlg,TRUE);
- return TRUE;
-
- case IDCANCEL:
- EndDialog(hDlg,TRUE);
- return TRUE;
- break;
-
- case IDHELP:
- WinHelp(hDlg,NULL,HELP_HELPONHELP,0);
- return TRUE;
- break;
- }
- break;
-
- case WM_DESTROY:
- DestroyWindow(TrackBarFloppy);
- DestroyWindow(TrackBarHD);
- DestroyWindow(TrackBarCD);
- break;
-
- default:
- return FALSE;
- }
- return TRUE;
- }
-
- BOOL Options(HWND hwnd){
- DialogBox(ghInst,MAKEINTRESOURCE(OPTIONS_DIALOG),NULL,OptionsDialogProc);
- return TRUE;
- }
-
- BOOL ChangeDrive(BOOL Change){
- BOOL available;
- DWORD a,b,c,d;
- UINT DriveType;
- if(Change==TRUE)CurrentDrive=CurrentDrive==DriveNo-1?0:CurrentDrive+1;
- strncpy(drive,Drives[CurrentDrive],sizeof(drive));
- DriveType=GetDriveType(drive);
- switch(DriveType){
- case DRIVE_REMOVABLE:
- if(CheckFloppynCD==TRUE){
- LOWEST_PERCENTAGE=FLOPPY_PERCENTAGE;
- strncpy(TypeString,eng==0?"(─Θ≤Ω▌⌠ß)":"(Floppy)",sizeof TypeString);
- }
- else
- ChangeDrive(TRUE);
- break;
-
- case DRIVE_FIXED:
- LOWEST_PERCENTAGE=HARD_PERCENTAGE;
- strncpy(TypeString,eng==0?"(╙Ωδτ±ⁿ≥ Σ▀≤Ω∩≥)":"(Hard Disk)",sizeof TypeString);
- break;
-
- case DRIVE_CDROM:
- if(CheckFloppynCD==TRUE){
- LOWEST_PERCENTAGE=CDROM_PERCENTAGE;
- strncpy(TypeString,"(CD-ROM)",sizeof TypeString);
- }
- else
- ChangeDrive(TRUE);
- break;
-
- default:
- LOWEST_PERCENTAGE=DEFAULT_PERCENTAGE;
- strncpy(TypeString,eng==0?"(óπφ∙≤⌠∩)":"Unknown",sizeof TypeString);
- break;
- }
- available=GetDiskFreeSpace(drive,&a,&b,&c,&d);
- if(available!=TRUE)ChangeDrive(TRUE);
- return TRUE;
- }
-