home *** CD-ROM | disk | FTP | other *** search
/ Shareware Breakthrough: Utilities & Productivity / SharewearBreakthroughUtil_ProdCol.cdr / util / arrange / arrange.c next >
Encoding:
C/C++ Source or Header  |  1990-09-13  |  12.1 KB  |  294 lines

  1.  
  2. #include "windows.h"
  3. #include "arrange.h"
  4. #include "string.h"
  5. #include "time.h"
  6. #include "dos.h"
  7. #include "io.h"
  8. #include "math.h"
  9. #include "stdlib.h"
  10.  
  11. HANDLE hInst; HWND hWnd; int TimerID = 1;
  12. HWND hWnds[200]; char inifile[144]; int moa=0,soe=0,aoa=0;
  13.  
  14. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  15. HANDLE hInstance; HANDLE hPrevInstance; LPSTR lpCmdLine; int nCmdShow;
  16. {
  17.     MSG msg;
  18.     GetWindowsDirectory(inifile,144);
  19.     strcat(inifile,"\\arrange.ini");
  20.     if (!hPrevInstance) if (!InitApplication(hInstance)) return (FALSE);
  21.     if (!InitInstance(hInstance, nCmdShow)) return (FALSE);
  22.     if (moa == 1) DestroyWindow(hWnd);
  23.     while (GetMessage(&msg, NULL, NULL, NULL)) {
  24.         TranslateMessage(&msg); DispatchMessage(&msg); }
  25.     return (msg.wParam);
  26. }
  27.  
  28. BOOL FileExists(PSTR pch)
  29. {
  30.         int fh;
  31.         if ((fh = _lopen((LPSTR) pch, 0)) < 0) return(FALSE); _lclose(fh);
  32.         return(TRUE);
  33. }
  34.  
  35. int SaveLoc(sWnd,head)
  36. HWND sWnd; char *head;
  37. {
  38.         char szText[100];
  39.         RECT lpRect; int yw;
  40.         if (strnicmp(head,"vp_win",6) == 0) return(0);
  41.         GetWindowRect(sWnd,&lpRect);
  42.         sprintf(szText,"%i",lpRect.left);
  43.         WritePrivateProfileString(head,"left",szText,inifile);
  44.         sprintf(szText,"%i",lpRect.right);
  45.         WritePrivateProfileString(head,"right",szText,inifile);
  46.         sprintf(szText,"%i",lpRect.top);
  47.         WritePrivateProfileString(head,"top",szText,inifile);
  48.         sprintf(szText,"%i",lpRect.bottom);
  49.         WritePrivateProfileString(head,"bottom",szText,inifile);
  50.         return(0);
  51. }
  52.  
  53. int RestoreLoc(sWnd,head)
  54. HWND sWnd; char *head;
  55. {
  56.         RECT lpRect; char str[100]; int xw,yw,xs,ys;
  57.         GetPrivateProfileString(head,"left","-1",str,100,inifile);
  58.         if ((xs = atoi(str)) == -1) return(0);
  59.         GetPrivateProfileString(head,"top","-1",str,100,inifile);
  60.         if ((ys = atoi(str)) == -1) return(0);
  61.         GetPrivateProfileString(head,"right","-1",str,100,inifile);
  62.         if ((xw = atoi(str)) == -1) return(0); xw = xw - xs;
  63.         GetPrivateProfileString(head,"bottom","-1",str,100,inifile);
  64.         if ((yw = atoi(str)) == -1) return(0); yw = yw - ys;
  65.         SetWindowPos(sWnd,(HWND) NULL,xs,ys,xw,yw,SWP_NOZORDER);
  66.         ShowWindow(sWnd, SW_SHOWNOACTIVATE);
  67.         UpdateWindow(sWnd);
  68.         return(1);
  69. }
  70.  
  71. ResetFile()
  72. {
  73.        int hFile;
  74.        hFile = _lcreat(inifile,0); _lclose(hFile);
  75.        WritePrivateProfileString("arrange","SaveOnExit","no",inifile);
  76.        WritePrivateProfileString("arrange","ArrangeOnActivation","no",inifile);
  77.        WritePrivateProfileString("arrange","ExitOnArrange","no",inifile);
  78.        soe = 0; aoa = 0; moa = 0;
  79. }
  80.  
  81.  
  82. BOOL InitApplication(hInstance)
  83. HANDLE hInstance;
  84. {
  85.     WNDCLASS  wc;
  86.     if (!FileExists(inifile)) ResetFile();
  87.     wc.style = NULL; wc.lpfnWndProc = MainWndProc;
  88.     wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance;
  89.     wc.hIcon = LoadIcon(hInstance,"ArrangeIcon");
  90.     wc.hCursor = LoadCursor(hInstance,"ArrangeIcon");
  91.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  92.     wc.lpszMenuName =  "ArrangeMenu";
  93.     wc.lpszClassName = "ArrangeWClass";
  94.     return (RegisterClass(&wc));
  95. }
  96.  
  97. BOOL InitInstance(hInstance, nCmdShow)
  98.     HANDLE hInstance; int nCmdShow;
  99. {
  100.     TEXTMETRIC txm; HDC hDC; int xs,ys,xw,yw,j,i,nwnd; HWND sWnd;
  101.     char szText[100],dumstr[100];
  102.     hInst = hInstance;
  103.     hWnd = CreateWindow( "ArrangeWClass", "Arrange",
  104.     WS_OVERLAPPEDWINDOW, 0, 0, 10, 10, NULL, NULL, hInstance, NULL );
  105.     SetTimer(hWnd, TimerID, TLEN , (FARPROC) NULL);
  106.     if (!hWnd) return (FALSE);
  107.     if (RestoreLoc(hWnd,"arrange") == 0) {
  108.             hDC = GetDC(hWnd);
  109.             GetTextMetrics (hDC, &txm); ReleaseDC(hWnd, hDC);
  110.             xs = 0; ys = 0;
  111.             xw = xs+17*(txm.tmMaxCharWidth);
  112.             yw = ys+11*(txm.tmExternalLeading + txm.tmHeight);
  113.             SetWindowPos(hWnd,(HWND) NULL,xs,ys,xw,yw,SWP_NOZORDER);
  114.             ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); }
  115.  
  116.     GetPrivateProfileString("arrange","ArrangeOnActivation","no",
  117.         dumstr,100,inifile);
  118.     if (strnicmp(dumstr,"yes",3) == 0) {
  119.     sWnd = hWnd; nwnd = 0;
  120.     while(1) {
  121.     sWnd = GetNextWindow(sWnd,GW_HWNDNEXT);
  122.     if (sWnd == (HWND)NULL) break;
  123.     j = GetWindowText(sWnd,szText,100);
  124.     if (j != 0) { hWnds[nwnd] = sWnd; ++nwnd; }}
  125.     for (i = 0; i < nwnd; ++i) {
  126.             szText[GetWindowText(hWnds[i],szText,100)] = 0;
  127.             RestoreLoc(hWnds[i],szText); }}
  128.     ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd);
  129.  
  130.     GetPrivateProfileString("arrange","ArrangeOnActivation","no",
  131.         dumstr,100,inifile);
  132.     if (strnicmp(dumstr,"yes",3) == 0) aoa = 1; else aoa = 0;
  133.     GetPrivateProfileString("arrange","ExitOnArrange","no",
  134.         dumstr,100,inifile);
  135.     if (strnicmp(dumstr,"yes",3) == 0) moa = 1; else moa = 0;
  136.     GetPrivateProfileString("arrange","SaveOnExit","no",
  137.         dumstr,100,inifile);
  138.     if (strnicmp(dumstr,"yes",3) == 0) soe = 1; else soe = 0;
  139.     return (TRUE);
  140. }
  141.  
  142. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  143. HWND hWnd; unsigned message; WORD wParam; LONG lParam;
  144. {
  145.     FARPROC lpProcAbout;
  146.     char szText[100],dumstr[100];
  147.     HDC hDC; PAINTSTRUCT ps; HWND sWnd;
  148.     int nDrawX,nDrawY,dy,i,j,nwnd; TEXTMETRIC textmetric;
  149.     int xs,ys,xw,yw;
  150.     RECT lpRect; LPOFSTRUCT lpf;
  151.     switch (message) {
  152.         case WM_COMMAND:
  153.             switch (wParam) {
  154.             case IDM_ABOUT:
  155.                 lpProcAbout = MakeProcInstance(About, hInst);
  156.                 DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
  157.                 FreeProcInstance(lpProcAbout); break;
  158.             case IDM_INITIALIZE: ResetFile(); break;
  159.             case IDM_SAVEPREV:
  160.                 sWnd = hWnd; i = 0;
  161.                 while(1) { ++i;
  162.                 sWnd = GetNextWindow(sWnd,GW_HWNDNEXT);
  163.                 if (sWnd == (HWND)NULL) break;
  164.                 j = GetWindowText(sWnd,szText,100);
  165.                 if (j != 0) {
  166.                         szText[GetWindowText(sWnd,szText,100)] = 0;
  167.                         if (!IsIconic(sWnd)) SaveLoc(sWnd,szText); break;
  168.                 }}
  169.                 SaveLoc(hWnd,"arrange");
  170.                 break;
  171.             case IDM_SAVEONEXIT : soe = 1;
  172.                     WritePrivateProfileString("arrange",
  173.                         "ArrangeOnExit","yes",inifile); break;
  174.             case IDM_ARRANGEONACTIVATION: aoa = 1;
  175.                     WritePrivateProfileString("arrange",
  176.                         "ArrangeOnActivation","yes",inifile); break;
  177.             case IDM_EXITONARRANGE: moa = 1;
  178.                     WritePrivateProfileString("arrange",
  179.                         "ExitOnArrange","yes",inifile); break;
  180.             case IDM_NOEXITONARRANGE: moa = 0;
  181.                     WritePrivateProfileString("arrange",
  182.                         "ExitOnArrange","no",inifile); break;
  183.             case IDM_NOSAVEONEXIT : soe = 0;
  184.                     WritePrivateProfileString("arrange",
  185.                         "ArrangeOnExit","no",inifile); break;
  186.             case IDM_NOARRANGEONACTIVATION: aoa = 0;
  187.                     WritePrivateProfileString("arrange",
  188.                         "ArrangeOnActivation","no",inifile); break;
  189.             case IDM_SAVE:
  190.                     SaveLoc(hWnd,"arrange");
  191.                     sWnd = hWnd; i = 0;
  192.                     while(1) { ++i; sWnd = GetNextWindow(sWnd,GW_HWNDNEXT);
  193.                     if (sWnd == (HWND)NULL) break;
  194.                     j = GetWindowText(sWnd,szText,100); if (j != 0) {
  195.                             szText[GetWindowText(sWnd,szText,100)] = 0;
  196.                             if (!IsIconic(sWnd)) SaveLoc(sWnd,szText); }} break;
  197.             case IDM_RESTORE:
  198.                      sWnd = hWnd; nwnd = 0;
  199.                      while(1) { sWnd = GetNextWindow(sWnd,GW_HWNDNEXT);
  200.                      if (sWnd == (HWND)NULL) break;
  201.                      j = GetWindowText(sWnd,szText,100);
  202.                      if (j != 0) { hWnds[nwnd] = sWnd; ++nwnd; }}
  203.                      for (i = 0; i < nwnd; ++i) {
  204.                              szText[GetWindowText(hWnds[i],szText,100)] = 0;
  205.                              RestoreLoc(hWnds[i],szText); }
  206.                      RestoreLoc(hWnd,"arrange");
  207.                      if (moa == 1) DestroyWindow(hWnd);
  208.                      break;
  209.             case IDM_RESTOREPREV:
  210.                      sWnd = hWnd; nwnd = 0;
  211.                      while(1) { sWnd = GetNextWindow(sWnd,GW_HWNDNEXT);
  212.                      if (sWnd == (HWND)NULL) break;
  213.                      j = GetWindowText(sWnd,szText,100);
  214.                      if (j != 0) { hWnds[nwnd] = sWnd; ++nwnd; }}
  215.                      for (i = 0; i < nwnd; ++i) {
  216.                              szText[GetWindowText(hWnds[i],szText,100)] = 0;
  217.                              RestoreLoc(hWnds[i],szText); break; }
  218.                      RestoreLoc(hWnd,"arrange");
  219.                      if (moa == 1) DestroyWindow(hWnd);
  220.                      break;
  221.             case IDM_EXIT:
  222.                 DestroyWindow(hWnd); break; }
  223.             break;
  224.         case WM_TIMER: get_stats(hWnd);
  225.             break;
  226.         case WM_DESTROY:
  227.             if (soe == 1) {
  228.                     sWnd = hWnd; i = 0;
  229.                     while(1) { ++i; sWnd = GetNextWindow(sWnd,GW_HWNDNEXT);
  230.                     if (sWnd == (HWND)NULL) break;
  231.                     j = GetWindowText(sWnd,szText,100); if (j != 0) {
  232.                             szText[GetWindowText(sWnd,szText,100)] = 0;
  233.                             if (!IsIconic(sWnd)) SaveLoc(sWnd,szText); }}}
  234.             SaveLoc(hWnd,"arrange"); PostQuitMessage(0); break;
  235.         case WM_CREATE:
  236.             break;
  237.         default: return (DefWindowProc(hWnd, message, wParam, lParam));
  238.     } return (NULL);
  239. }
  240.  
  241. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  242. HWND hDlg; unsigned message; WORD wParam; LONG lParam;
  243. {
  244.     switch (message) { case WM_INITDIALOG: return (TRUE);
  245.         case WM_COMMAND: if (wParam == IDOK || wParam == IDCANCEL) {
  246.                 EndDialog(hDlg, TRUE); return (TRUE); } break; }
  247.     return (FALSE);
  248. }
  249.  
  250. int get_stats(sWnd)
  251. HWND sWnd;
  252. {
  253.         unsigned int cur_drive; struct diskfree_t dfree;
  254.         char str[80],timestr[80]; long tot,i1,i2,i3,i4;
  255.         int nx,ny,yinc; HFONT hFont,hOldFont;
  256.         struct tm *newtime; time_t aclock;
  257.         HDC hDC; TEXTMETRIC txm;
  258.         hDC = GetDC(sWnd);
  259. /*      hFont = GetStockObject(ANSI_VAR_FONT);
  260.         hOldFont = SelectObject(hDC,hFont); */
  261.         GetTextMetrics (hDC, &txm);
  262.         time(&aclock); newtime = localtime(&aclock);
  263.         strcpy(timestr,asctime(newtime)); timestr[strlen(timestr)-1]=0;
  264.         nx = GetDeviceCaps (hDC, LOGPIXELSX) / 16;
  265.         ny = GetDeviceCaps (hDC, LOGPIXELSY) / 16;
  266.         yinc = txm.tmExternalLeading + txm.tmHeight;
  267.         sprintf(str,"%s",timestr);
  268.         TextOut (hDC, nx, ny, str, strlen (str));
  269.  
  270.         _dos_getdrive(&cur_drive); _dos_getdiskfree(cur_drive, &dfree);
  271.         getcwd(str,60);
  272.         ny += yinc; TextOut (hDC, nx, ny, str, strlen (str));
  273.         i1 = dfree.total_clusters; i2 = dfree.sectors_per_cluster;
  274.         i3 = dfree.bytes_per_sector; i4 = dfree.avail_clusters;
  275.         sprintf(str," %li total kBytes disk space",i1*i2*i3/1000);
  276.         ny += yinc; TextOut (hDC, nx, ny, str, strlen (str));
  277.         sprintf(str," %li kBytes disk space free",i4*i2*i3/1000);
  278.         ny += yinc; TextOut (hDC, nx, ny, str, strlen (str));
  279.         i1 = GetWinFlags();
  280.         if ((i1 & WF_ENHANCED)==WF_ENHANCED)
  281.                 strcpy(str,"Running in 386 Enhanced Mode");
  282.         else if ((i1 & WF_STANDARD)==WF_STANDARD)
  283.                 strcpy(str,"Running in Standard Mode");
  284.         else strcpy(str,"Running in Real Mode");
  285.         ny += yinc; TextOut (hDC, nx, ny, str, strlen (str));
  286.         i1 = GetFreeSpace(0); sprintf(str," %li kBytes Global RAM free",i1/1000);
  287.         ny += yinc; TextOut (hDC, nx, ny, str, strlen (str));
  288.         i1 = GlobalCompact(0); sprintf(str," %li kBytes in Largest Block",i1/1000);
  289.         ny += yinc; TextOut (hDC, nx, ny, str, strlen (str));
  290.  
  291.         ReleaseDC(sWnd, hDC);
  292. }
  293.  
  294.