home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / utility / v12n02.zip / CHANGER.C < prev    next >
Text File  |  1992-11-02  |  36KB  |  1,050 lines

  1. //=======================================================================
  2. // 
  3. //  CHANGER.C -- A program to randomly change the look of the
  4. //               Windows desktop
  5. //
  6. //  (c) Douglas Boling, 1992
  7. //
  8. //=======================================================================
  9.                                                                                                 
  10. #include <windows.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <time.h>
  14. #include <dos.h> 
  15.  
  16. #define    NAMELEN        32
  17. #define    ININAMELEN    13
  18.  
  19. #include "changer.h"
  20.  
  21. typedef struct {
  22.     DWORD    lSize;
  23.     WORD    wDate;
  24.     WORD    wTime;
  25.     WORD    wAttr;
  26.     char    szName[13];
  27. } FILEINFO;
  28.  
  29. typedef struct find_t FIND_T;
  30.  
  31. char    szBuffer[1024];
  32. char    szNextData[512];
  33. char     szColorNames[21][18] = {"Background","AppWorkspace","Window",
  34.                                   "WindowText","Menu","MenuText",
  35.                                   "ActiveTitle","InactiveTitle",
  36.                                   "TitleText","ActiveBorder",
  37.                                   "InactiveBorder","WindowFrame",
  38.                                   "Scrollbar","ButtonFace", "ButtonShadow",
  39.                                   "ButtonText","GrayText","Hilight",
  40.                                   "HilightText","InactiveTitleText",
  41.                                   "ButtonHilight"};
  42.  
  43. int    sColorIDs [21] = {COLOR_BACKGROUND, COLOR_APPWORKSPACE, COLOR_WINDOW,
  44.                         COLOR_WINDOWTEXT, COLOR_MENU, COLOR_MENUTEXT,
  45.                         COLOR_ACTIVECAPTION, COLOR_INACTIVECAPTION, 
  46.                         COLOR_CAPTIONTEXT, COLOR_ACTIVEBORDER,
  47.                         COLOR_INACTIVEBORDER, COLOR_WINDOWFRAME, 
  48.                         COLOR_SCROLLBAR, COLOR_BTNFACE,COLOR_BTNSHADOW, 
  49.                         COLOR_BTNTEXT, COLOR_GRAYTEXT,COLOR_HIGHLIGHT, 
  50.                         COLOR_HIGHLIGHTTEXT, COLOR_INACTIVECAPTIONTEXT, 
  51.                         COLOR_BTNHIGHLIGHT};
  52. DWORD    rgbColors[21];                        
  53.         
  54. #define    MAXFOCUS    8
  55. int    sCurrFocus;
  56. int    sFocusArray[8] = {IDD_COLORFRM, IDD_BGROUNDFRM, IDD_SSAVEFRM,
  57.                         IDD_PATTERNFRM, IDD_EDITCAT, IDD_CHENABLED,
  58.                         IDOK, IDD_ABOUT};
  59.                                                   
  60. //-----------------------------------------------------------------------
  61. //Function prototypes
  62. //-----------------------------------------------------------------------
  63. int    PASCAL         WinMain (HANDLE, HANDLE, LPSTR, int);
  64. long    FAR PASCAL    WndProc (HWND, WORD, WORD, LONG);
  65. BOOL     FAR PASCAL     AboutDlgProc (HWND, WORD, WORD, LONG);
  66. BOOL     FAR PASCAL     ConfigDlgProc (HWND, WORD, WORD, LONG);
  67.  
  68. void ChangeDesktop (void);
  69.  
  70. char        szAppName[] = "Changer";
  71. char        szProfileName[] = "CHANGER.INI";
  72. HANDLE    hInst;
  73.  
  74. char    szCurcategory[32];
  75.  
  76. //=======================================================================
  77. //
  78. // Program Entry Point (WinMain)
  79. //
  80. //=======================================================================
  81. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  82.                 LPSTR lpszCmdParam, int nCmdShow) {
  83.  
  84.    HWND            hwnd;
  85.     HANDLE        hAccel;
  86.    MSG            msg;
  87.    WNDCLASS        wndclass;
  88.     int            i, j;
  89.     DWORD            dwSeconds;
  90.  
  91.      srand ((UINT) time(&dwSeconds));
  92.      hInst = hInstance;
  93.      //
  94.      // Check for Win 3.1
  95.      //
  96.      i = LOWORD (GetVersion());
  97.      j = LOBYTE (i) * 256 + HIBYTE (i);
  98.      if (j < 0x30A) {
  99.         MessageBox (0, "Changer requires Windows 3.1", "Changer", 
  100.                     MB_ICONSTOP | MB_OK);
  101.         return (1);
  102.      }        
  103.     //
  104.     // If the /c parameter is used, change the desktop and terminate
  105.     //
  106.       if (GetPrivateProfileInt ("categories", "Colors", -1, szProfileName) != -1) {
  107.         if (lpszCmdParam[0] == '/' || lpszCmdParam[0] == '-') 
  108.             if (lpszCmdParam[1] == 'c' || lpszCmdParam[1] == 'C') {
  109.                ChangeDesktop();               
  110.                 return 0;
  111.             }        
  112.     }
  113.     //
  114.     // No /c, so create a nonmodal dialog box as the main window.
  115.     //
  116.     if (!hPrevInstance) {
  117.       wndclass.style          = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  118.       wndclass.lpfnWndProc    = WndProc;
  119.       wndclass.cbClsExtra     = 0;
  120.       wndclass.cbWndExtra     = DLGWINDOWEXTRA;
  121.       wndclass.hInstance      = hInstance;
  122.       wndclass.hIcon          = LoadIcon (hInstance, szAppName);
  123.       wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW);
  124.       wndclass.hbrBackground  = COLOR_WINDOW + 1;
  125.       wndclass.lpszMenuName   = 0;
  126.       wndclass.lpszClassName  = szAppName;
  127.  
  128.       RegisterClass (&wndclass);
  129.    }
  130.     i = GetPrivateProfileInt (szAppName, "x", 300, szProfileName);
  131.     j = GetPrivateProfileInt (szAppName, "y", 100, szProfileName);
  132.  
  133.     hwnd = CreateDialog (hInstance, szAppName, 0, NULL);
  134.  
  135.     SetWindowPos (hwnd, NULL, i, j, 0, 0, SWP_NOSIZE);
  136.     ShowWindow (hwnd, nCmdShow);
  137.     hAccel = LoadAccelerators (hInstance, szAppName);
  138.  
  139.     while (GetMessage (&msg, NULL, 0, 0)) {
  140.        if (!TranslateAccelerator (hwnd, hAccel, &msg)) 
  141.          if (hwnd == 0 || !IsDialogMessage (hwnd, &msg)) {
  142.                TranslateMessage (&msg);
  143.                DispatchMessage (&msg);
  144.            }    
  145.     }
  146.    return msg.wParam;
  147. }
  148.  
  149. //----------------------------------------------------------------------
  150. // ahtodw - ASCII hex to double word conversion
  151. //----------------------------------------------------------------------
  152. DWORD ahtodw (char * szInStr) {
  153.    char *    pszDigit;
  154.    DWORD        dwResult;
  155.    int        sDigit;
  156.    
  157.    pszDigit = szInStr;
  158.    dwResult = 0;   
  159.    for (; *pszDigit != 0; pszDigit++) {
  160.         sDigit = (int) *pszDigit;
  161.         sDigit -= 48;
  162.         if (sDigit > 9) sDigit -= 7;
  163.         dwResult *= 16;
  164.         dwResult += (DWORD) sDigit;
  165.     }
  166.     return dwResult;
  167. }
  168.  
  169. //----------------------------------------------------------------------
  170. // GetNextActive - Querys the INI file for the next active selection
  171. //----------------------------------------------------------------------
  172. BOOL GetNextActive (char *szCat, char *szSel, char *szData) {
  173.     char        szActiveList[64];
  174.     char *    pszNextData;
  175.     int        i, sCount, sActive, sChangeType;
  176.  
  177.     sChangeType = GetPrivateProfileInt ("categories", szCat, 2,
  178.                                          szProfileName);
  179.     if (sChangeType == 2) return FALSE;
  180.     
  181.     strcpy (szActiveList, szCat);
  182.     strcat (szActiveList, " Active");
  183.     
  184.     sCount = GetPrivateProfileInt (szCat, "Count", 0, szProfileName);
  185.     sActive = GetPrivateProfileInt (szCat, "Active", 0, szProfileName);
  186.     if (sCount == 0) return FALSE;
  187.     
  188.     if (sChangeType == 1) {
  189.        sActive++;
  190.        if (sActive > sCount - 1) sActive = 0;
  191.     } else {
  192.        i = rand();
  193.        sActive = i;
  194.         sActive %= sCount;
  195.     }    
  196.  
  197.     itoa (sActive, szNextData, 10);
  198.     WritePrivateProfileString (szCat, "Active", szNextData, 
  199.                                 szProfileName);    
  200.    GetPrivateProfileString (szActiveList, 0, "None", szNextData, 
  201.                              sizeof (szNextData), szProfileName);
  202.     pszNextData = szNextData;
  203.     i = 0;
  204.     for (i = 0; i < sActive; i++) {
  205.         for (; *pszNextData != 0; pszNextData++)
  206.             ;
  207.         pszNextData++;
  208.     }
  209.     strcpy (szSel, pszNextData);
  210.     GetPrivateProfileString (szActiveList, szSel, szSel, szNextData, 
  211.                              sizeof (szNextData), szProfileName);
  212.     strcpy (szData, szNextData);
  213.         return TRUE;    
  214. }
  215.  
  216. //----------------------------------------------------------------------
  217. // ChangeColors - Changes the desktop colors
  218. //----------------------------------------------------------------------
  219. void ChangeColors (char *szcategory) {
  220.     char         szSelection[32], szColors[256], szNewColor[32], szTemp[12];
  221.     char *    pszCurColor;
  222.     int        i, j;
  223.     BYTE        ucRed, ucGreen, ucBlue;
  224.     DWORD        dwColor;
  225.     
  226.     if (GetNextActive (szcategory, szSelection, szColors) == FALSE) return;
  227.     WritePrivateProfileString ("Current", "Color Schemes", szSelection, 
  228.                                "CONTROL.INI");
  229.     
  230.    pszCurColor = strtok (szColors, ",");
  231.     i = 0;    
  232.     while (*pszCurColor != 0) {
  233.         dwColor = ahtodw (pszCurColor);
  234.         ucRed = GetRValue (dwColor);
  235.         ucGreen = GetGValue (dwColor);
  236.         ucBlue = GetBValue (dwColor);
  237.         rgbColors[i] = RGB (ucRed, ucGreen, ucBlue);
  238.  
  239.         itoa (ucRed, szNewColor, 10);
  240.         strcat (szNewColor, " ");
  241.         itoa (ucGreen, szTemp, 10);
  242.         strcat (szNewColor, szTemp);
  243.         itoa (ucBlue, szTemp, 10);
  244.         strcat (szNewColor, " ");
  245.         strcat (szNewColor, szTemp);
  246.         WriteProfileString ("Colors", szColorNames[j], szNewColor);
  247.         
  248.        pszCurColor = strtok (NULL, ",");
  249.        i++;
  250.     }
  251.     SetSysColors (i, sColorIDs, rgbColors);
  252.    return;
  253. }
  254.    
  255. //----------------------------------------------------------------------
  256. // ChangeGeneric - Changes a parameter in an INI file
  257. //----------------------------------------------------------------------
  258. void ChangeGeneric (char * szcategory) {
  259.    char    szSelection[32], szDestCat[32], szDestItem[32], szDestIni[32];
  260.    char    szData[128];
  261.  
  262.     GetPrivateProfileString (szcategory, "DestINI", "None", szDestIni, 
  263.                              sizeof (szDestIni), szProfileName);
  264.     GetPrivateProfileString (szcategory, "DestCat", "None", szDestCat,
  265.                              sizeof (szDestCat), szProfileName);
  266.     GetPrivateProfileString (szcategory, "DestItem", "None", szDestItem,
  267.                              sizeof (szDestItem), szProfileName);
  268.  
  269.     if (!GetNextActive (szcategory, szSelection, szData)) return;
  270.    
  271.     WritePrivateProfileString (szDestCat, szDestItem, szData,
  272.                                szDestIni);
  273.    
  274.     return;   
  275. }   
  276.  
  277. //----------------------------------------------------------------------
  278. // ChangeDesk - Changes the desktop parameters
  279. //----------------------------------------------------------------------
  280. void ChangeDesktop (void) {
  281.  
  282.    char        szSelection[32], szData[128];
  283.     char *    pszBuffer;
  284.     int        i;
  285.  
  286.     i = (int) GetPrivateProfileString ("categories", 0, "None", szBuffer, 
  287.                                        sizeof (szBuffer), szProfileName);
  288.  
  289.     if (i == 0 || strcmp (szBuffer, "None") == 0) 
  290.        return;    
  291.  
  292.     pszBuffer = szBuffer;
  293.     i = 0;
  294.     while (*pszBuffer != 0) {
  295.  
  296.         switch (GetPrivateProfileInt (pszBuffer, "Type", 
  297.                                       -1, szProfileName)) {
  298.            case 0:
  299.                 ChangeColors (pszBuffer);
  300.                break;        
  301.  
  302.            //Screen Saver
  303.            case 2:
  304.                ChangeGeneric (pszBuffer);
  305.                break;
  306.                    
  307.            //Pattern
  308.            case 3:
  309.                 if (!GetNextActive (pszBuffer, szSelection, szData)) break;
  310.                 SystemParametersInfo (SPI_SETDESKPATTERN, 0, szData,
  311.                                       SPIF_UPDATEINIFILE | 
  312.                                       SPIF_SENDWININICHANGE);
  313.  
  314.                break;
  315.                
  316.            //Background
  317.            case 1:
  318.                 if (!GetNextActive (pszBuffer, szSelection, szData)) break;
  319.                 SystemParametersInfo (SPI_SETDESKWALLPAPER, 0, szData,
  320.                                       SPIF_UPDATEINIFILE | 
  321.                                       SPIF_SENDWININICHANGE);
  322.                 break;
  323.  
  324.            //Others
  325.            default:
  326.                ChangeGeneric (pszBuffer);
  327.                break;
  328.         }                                                            
  329.         for (; *pszBuffer != 0; pszBuffer++)
  330.             ;
  331.         pszBuffer++;
  332.     }
  333.    return;
  334. }
  335.  
  336. //======================================================================
  337. // Routines used by WndProc
  338. //======================================================================
  339. //----------------------------------------------------------------------
  340. // Setcategories - Initializes the Ini file
  341. //----------------------------------------------------------------------
  342. void Setcategories (void) {
  343.  
  344.     if (GetPrivateProfileInt ("categories", "Colors", -1, 
  345.                               szProfileName) != -1) return;
  346.    //
  347.    //Colors, Wallpaper and Screen Saver have custom setups
  348.    //
  349.     WritePrivateProfileString ("categories", "Colors", "0", 
  350.                                szProfileName);
  351.     WritePrivateProfileString ("Colors", "Type", "0", 
  352.                                szProfileName);
  353.     WritePrivateProfileString ("Colors", "SrcINI","CONTROL.INI", 
  354.                                szProfileName);                                   
  355.     WritePrivateProfileString ("Colors", "SrcCat","color schemes", 
  356.                                szProfileName);                                   
  357.                                
  358.     WritePrivateProfileString ("categories", "Background", "0", 
  359.                                szProfileName);                                   
  360.     WritePrivateProfileString ("Background", "Type", "1", 
  361.                                szProfileName);
  362.     WritePrivateProfileString ("Background", "DestINI","WIN.INI", 
  363.                                szProfileName);
  364.     WritePrivateProfileString ("Background", "DestCat","Desktop", 
  365.                                szProfileName);
  366.     WritePrivateProfileString ("Background", "DestItem","Wallpaper", 
  367.                                szProfileName);
  368.                                
  369.     WritePrivateProfileString ("categories", "Screen Saver", "0", 
  370.                                szProfileName);
  371.     WritePrivateProfileString ("Screen Saver", "Type", "2", 
  372.                                szProfileName);
  373.     WritePrivateProfileString ("Screen Saver", "DestINI","SYSTEM.INI", 
  374.                                szProfileName);
  375.     WritePrivateProfileString ("Screen Saver", "DestCat","boot", 
  376.                                szProfileName);
  377.     WritePrivateProfileString ("Screen Saver", "DestItem","SCRNSAVE.EXE", 
  378.                                szProfileName);
  379.     //
  380.     // Remainder have standard setups
  381.     //                           
  382.     WritePrivateProfileString ("categories", "Pattern", "0", 
  383.                                szProfileName);
  384.     WritePrivateProfileString ("Pattern", "Type", "3", 
  385.                                szProfileName);
  386.     WritePrivateProfileString ("Pattern", "SrcINI","CONTROL.INI", 
  387.                                szProfileName);                                   
  388.     WritePrivateProfileString ("Pattern", "SrcCat","Patterns", 
  389.                                szProfileName);                                   
  390.     WritePrivateProfileString ("Pattern", "DestINI","WIN.INI", 
  391.                                szProfileName);
  392.     WritePrivateProfileString ("Pattern", "DestCat","Desktop", 
  393.                                szProfileName);
  394.     WritePrivateProfileString ("Pattern", "DestItem","Pattern", 
  395.                                szProfileName);
  396.     return;
  397. }
  398.  
  399. //========================================================================
  400. //
  401. // Main Window Procedure.
  402. //
  403. //========================================================================
  404. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam) {
  405.  
  406.     static    BOOL     bFirst;
  407.     static    HANDLE    hInst;
  408.     static    int        sActiveCat;
  409.  
  410.     int        i;
  411.     char        szTemp[128];
  412.     RECT        rect;
  413.     POINT        pt;
  414.  
  415.    switch (message) {
  416.     
  417.       case WM_CREATE:
  418.             hInst = ((LPCREATESTRUCT) lParam)->hInstance;
  419.             bFirst = TRUE;
  420.             return 0; 
  421.  
  422.         case WM_SHOWWINDOW:
  423.             if (bFirst) {
  424.                 bFirst = FALSE;
  425.                 Setcategories ();                
  426.                 sActiveCat = IDD_COLORFRM;
  427.                 sCurrFocus = 0;
  428.                 ShowWindow (GetDlgItem (hwnd, IDD_COLORFRM), SW_SHOW);
  429.                 ShowWindow (GetDlgItem (hwnd, IDD_SSAVEFRM), SW_HIDE);
  430.                 ShowWindow (GetDlgItem (hwnd, IDD_BGROUNDFRM), SW_HIDE);
  431.                 ShowWindow (GetDlgItem (hwnd, IDD_PATTERNFRM), SW_HIDE);
  432.             }            
  433.             break;
  434.  
  435.         case WM_LBUTTONDBLCLK:
  436.           pt.x = LOWORD (lParam);
  437.           pt.y = HIWORD (lParam);              
  438.           i = GetDlgCtrlID (ChildWindowFromPoint (hwnd, pt));
  439.           if (i) {
  440.                 PostMessage (hwnd, WM_COMMAND, IDD_EDITCAT, 
  441.                              MAKELONG (0, BN_CLICKED));
  442.             }
  443.             break;
  444.  
  445.         case WM_LBUTTONDOWN:
  446.           pt.x = LOWORD (lParam);
  447.           pt.y = HIWORD (lParam);              
  448.           i = GetDlgCtrlID (ChildWindowFromPoint (hwnd, pt));
  449.  
  450.             switch (i) {
  451.                 case IDD_COLORTXT:
  452.                 case IDD_COLORFRM:
  453.                     ShowWindow (GetDlgItem (hwnd, sActiveCat), SW_HIDE);
  454.                     ShowWindow (GetDlgItem (hwnd, IDD_COLORFRM), SW_SHOW);
  455.                     sActiveCat = IDD_COLORFRM;
  456.                     break;
  457.                 case IDD_SSAVETXT:
  458.                 case IDD_SSAVEFRM:
  459.                     ShowWindow (GetDlgItem (hwnd, sActiveCat), SW_HIDE);
  460.                     ShowWindow (GetDlgItem (hwnd, IDD_SSAVEFRM), SW_SHOW);
  461.                     sActiveCat = IDD_SSAVEFRM;
  462.                     break;
  463.                 case IDD_BGROUNDTXT:
  464.                 case IDD_BGROUNDFRM:
  465.                     ShowWindow (GetDlgItem (hwnd, sActiveCat), SW_HIDE);
  466.                     ShowWindow (GetDlgItem (hwnd, IDD_BGROUNDFRM), SW_SHOW);
  467.                     sActiveCat = IDD_BGROUNDFRM;
  468.                     break;
  469.                 case IDD_PATTERNTXT:
  470.                 case IDD_PATTERNFRM:
  471.                     ShowWindow (GetDlgItem (hwnd, sActiveCat), SW_HIDE);
  472.                     ShowWindow (GetDlgItem (hwnd, IDD_PATTERNFRM), SW_SHOW);
  473.                     sActiveCat = IDD_PATTERNFRM;
  474.                     break;
  475.             }    
  476.             break;
  477.  
  478.         case WM_COMMAND:
  479.             switch (wParam) {
  480.                 //
  481.                 // Menu messages
  482.                 //
  483.                 case IDM_COLOR:
  484.                     ShowWindow (GetDlgItem (hwnd, sActiveCat), SW_HIDE);
  485.                     ShowWindow (GetDlgItem (hwnd, IDD_COLORFRM), SW_SHOW);
  486.                     sActiveCat = IDD_COLORFRM;
  487.                     PostMessage (hwnd, WM_COMMAND, IDD_EDITCAT, 
  488.                                  MAKELONG (0, BN_CLICKED));
  489.                     break;
  490.                 case IDM_SSAVE:
  491.                     ShowWindow (GetDlgItem (hwnd, sActiveCat), SW_HIDE);
  492.                     ShowWindow (GetDlgItem (hwnd, IDD_SSAVEFRM), SW_SHOW);
  493.                     sActiveCat = IDD_SSAVEFRM;
  494.                     PostMessage (hwnd, WM_COMMAND, IDD_EDITCAT, 
  495.                                  MAKELONG (0, BN_CLICKED));
  496.                     break;
  497.                 case IDM_BGROUND:
  498.                     ShowWindow (GetDlgItem (hwnd, sActiveCat), SW_HIDE);
  499.                     ShowWindow (GetDlgItem (hwnd, IDD_BGROUNDFRM), SW_SHOW);
  500.                     sActiveCat = IDD_BGROUNDFRM;
  501.                     PostMessage (hwnd, WM_COMMAND, IDD_EDITCAT, 
  502.                                  MAKELONG (0, BN_CLICKED));
  503.                     break;
  504.                 case IDM_PATTERN:
  505.                     ShowWindow (GetDlgItem (hwnd, sActiveCat), SW_HIDE);
  506.                     ShowWindow (GetDlgItem (hwnd, IDD_PATTERNFRM), SW_SHOW);
  507.                     sActiveCat = IDD_PATTERNFRM;
  508.                     PostMessage (hwnd, WM_COMMAND, IDD_EDITCAT, 
  509.                                  MAKELONG (0, BN_CLICKED));
  510.                     break;
  511.                 case IDM_ABOUT:
  512.                     {
  513.                         FARPROC    lpfnAboutDlgFn;
  514.  
  515.                         lpfnAboutDlgFn = MakeProcInstance (AboutDlgProc, hInst);
  516.                         DialogBox (hInst, "About", hwnd, lpfnAboutDlgFn);
  517.                         FreeProcInstance (lpfnAboutDlgFn);
  518.                     }
  519.                     break;
  520.                      
  521.                 case IDM_EXIT:
  522.                     SendMessage (hwnd, WM_CLOSE, 0, 0);
  523.                     break;
  524.                 //
  525.                 // Button messages
  526.                 //
  527.                 case IDD_EDITCAT:
  528.                     if (HIWORD (lParam) == BN_CLICKED) {
  529.                         FARPROC    lpfnConfigDlgFn;
  530.                         
  531.                         switch (sActiveCat) {
  532.                            case IDD_COLORFRM:
  533.                                strcpy (szCurcategory, "Colors");
  534.                                break;
  535.                            case IDD_SSAVEFRM:
  536.                                strcpy (szCurcategory, "Screen Saver");
  537.                                break;
  538.                            case IDD_BGROUNDFRM:
  539.                                strcpy (szCurcategory, "Background");
  540.                                break;
  541.                            case IDD_PATTERNFRM:
  542.                                strcpy (szCurcategory, "Pattern");
  543.                                break;
  544.                         }       
  545.                         lpfnConfigDlgFn = MakeProcInstance (ConfigDlgProc, 
  546.                                                              hInst);
  547.                         DialogBox (hInst, "Config", hwnd, lpfnConfigDlgFn);
  548.                         FreeProcInstance (lpfnConfigDlgFn);
  549.                         return 0;
  550.                     }
  551.                     break;
  552.  
  553.                 case IDOK:
  554.                     if (HIWORD (lParam) == BN_CLICKED) {
  555.                         SendMessage (hwnd, WM_CLOSE, 0, 0);
  556.                         return 0;
  557.                     }
  558.                     break;
  559.             }
  560.             break;
  561.  
  562.         case WM_DESTROY:
  563.             //
  564.             //Save values
  565.             //
  566.             if (!IsIconic (hwnd) && !IsZoomed (hwnd)) {
  567.                 GetWindowRect (hwnd, &rect);
  568.                 itoa (rect.left, szTemp, 10);
  569.                 WritePrivateProfileString (szAppName, "x", szTemp, 
  570.                                            szProfileName);
  571.                 itoa (rect.top, szTemp, 10);
  572.                 WritePrivateProfileString (szAppName, "y", szTemp, 
  573.                                            szProfileName);
  574.             }
  575.             PostQuitMessage (0);
  576.             return 0;
  577.     }
  578.     return DefWindowProc (hwnd, message, wParam, lParam);
  579. }
  580. //======================================================================
  581. // Routines used by Configure DlgProc
  582. //======================================================================
  583. //------------------------------------------------------------------------
  584. // GetPossibleScrSav Fills listbox with possible Screen Savers
  585. //------------------------------------------------------------------------
  586. int GetPossibleScrSav (HWND hwnd, WORD wLBID, char * szDir, 
  587.                        char * szSearch) {
  588.     FIND_T    fs;
  589.     HANDLE    hFile;
  590.     OFSTRUCT    ofsFile;
  591.     int        i, rc, sCount;
  592.     char *    pszSSName;
  593.     char        szFullName[64];
  594.     
  595.     SendDlgItemMessage (hwnd, wLBID, LB_RESETCONTENT, 0, 0L);
  596.     sCount = 0;
  597.     rc = _dos_findfirst (szSearch, 0, &fs);
  598.     while (rc == 0) {
  599.        //
  600.        // Open the file found
  601.        //
  602.         strcpy (szFullName, szDir);
  603.         strcat (szFullName, "\\");
  604.         strcat (szFullName, fs.name);
  605.        hFile = OpenFile (szFullName, &ofsFile, OF_READ);
  606.        if (hFile != -1) {
  607.             //
  608.             // See if EXE, Check for New Exe header
  609.             //       
  610.              _lread (hFile, szBuffer, 0x40);
  611.           
  612.              if ((*(PWORD)&szBuffer == 0x5a4d) && (*(PWORD)&szBuffer[0x18] >= 0x40)) {
  613.                 //
  614.                 // Read New EXE header, check for Win EXE
  615.                 //
  616.               _llseek (hFile, *(PLONG)&szBuffer[0x3c], 0);
  617.               _lread (hFile, szBuffer, 0x40);
  618.               if ((*(PWORD)&szBuffer == 0x454e) && (szBuffer[0x36] == 2)) {
  619.                   //
  620.                   // Read Description from 1st entry in NonResident Names table
  621.                   //
  622.                   _llseek (hFile, (LONG) *(PWORD)&szBuffer[0x2c], 0);
  623.                   _lread (hFile, szBuffer, 256);
  624.                   szBuffer[szBuffer[0]+1] = 0;
  625.                   pszSSName = strchr (szBuffer+1, ':');
  626.                   if (pszSSName != 0) {
  627.                       pszSSName++;
  628.                         //
  629.                         // Find 1st char past : copy until end or other :
  630.                         //
  631.                       for (;*pszSSName != 0 && *pszSSName <= 32;pszSSName++);
  632.                       if (*strchr (pszSSName, ':')) {
  633.                           *strchr (pszSSName, ':') = 0;
  634.                           for (i = strlen (pszSSName); i > 0; i--) {
  635.                              if (*(pszSSName+i) > ' ') break;
  636.                               *(pszSSName+i) = 0;
  637.                           }
  638.                       }    
  639.                      strcat (pszSSName, "\t");
  640.  
  641.                        strcat (szBuffer, szDir);
  642.                         strcat (szBuffer, "\\");
  643.                        strcat (szBuffer, fs.name);
  644.                         SendDlgItemMessage (hwnd, wLBID, LB_ADDSTRING, 0, 
  645.                                             (LPARAM)(LPSTR) pszSSName);
  646.                     }
  647.                 }    
  648.             }
  649.             _lclose (hFile);    
  650.         }    
  651.         sCount++;                    
  652.         rc = _dos_findnext (&fs);
  653.     }
  654.     return sCount;
  655. }
  656.   
  657. //------------------------------------------------------------------------
  658. // GetPossibleFiles Fills listbox with possible file names
  659. //------------------------------------------------------------------------
  660. WORD GetPossibleFiles (HWND hwnd, WORD wLBID, char * szDir, 
  661.                        char * szSearch, BOOL bIncPath) {
  662.     FIND_T    fs;
  663.     int        rc;
  664.     WORD        i;
  665.     
  666.     SendDlgItemMessage (hwnd, wLBID, LB_RESETCONTENT, 0, 0L);
  667.     i = 0;
  668.     rc = _dos_findfirst (szSearch, 0, &fs);
  669.     while (rc == 0) {
  670.        strcpy (szBuffer, fs.name);
  671.         strcat (szBuffer, "\t");
  672.         if (bIncPath) {
  673.            strcat (szBuffer, szDir);
  674.             strcat (szBuffer, "\\");
  675.         }    
  676.        strcat (szBuffer, fs.name);
  677.         
  678.         SendDlgItemMessage (hwnd, wLBID, LB_ADDSTRING, 0, 
  679.                             (LPARAM)(LPSTR) szBuffer);
  680.         i++;                    
  681.         rc = _dos_findnext (&fs);
  682.     }
  683.     return i;
  684. }
  685.   
  686. //----------------------------------------------------------------------
  687. // FillLBox Fills a list box with items from a INI file category
  688. //----------------------------------------------------------------------
  689. int FillLBox (HWND hwnd, WORD wLBoxID, char * szcategory, char * szIniFile) {
  690.     int    i;
  691.     char *    pszBuffer;
  692.     char        szSel[512], szData[256];
  693.    
  694.     SendDlgItemMessage (hwnd, wLBoxID, LB_RESETCONTENT, 0, 0L);
  695.     
  696.     i = (int) GetPrivateProfileString (szcategory, 0, "None", szBuffer, 
  697.                              sizeof (szBuffer), szIniFile);                             
  698.     if (i == 0 || strcmp (szBuffer, "None") == 0) 
  699.        return 0;
  700.     
  701.     pszBuffer = szBuffer;
  702.     i = 0;
  703.     while (*pszBuffer != 0) {
  704.        
  705.         GetPrivateProfileString (szcategory, pszBuffer, "-1",
  706.                                  szData, sizeof (szData), szIniFile);
  707.         strcpy (szSel, pszBuffer);                                 
  708.         strcat (szSel, "\t");
  709.         strcat (szSel, szData);                                 
  710.         SendDlgItemMessage (hwnd, wLBoxID, LB_ADDSTRING, 0, 
  711.                             (LPARAM)(LPSTR) szSel);
  712.         for (; *pszBuffer != 0; pszBuffer++)
  713.             ;
  714.         pszBuffer++;
  715.         i++;
  716.     }
  717.     return i;
  718. }
  719.  
  720. //------------------------------------------------------------------------
  721. // GetPossibleGeneric  Returns list from generic selection
  722. //------------------------------------------------------------------------
  723. void GetPossibleGeneric (HWND hwnd, WORD wLBID, char * szcategory) {
  724.     char    szIniFile [20], szSrcCat [64];
  725.  
  726.     GetPrivateProfileString (szcategory, "SrcINI", "None", szIniFile, 
  727.                              sizeof (szIniFile), szProfileName);
  728.     GetPrivateProfileString (szcategory, "SrcCat", "None", szSrcCat,
  729.                              sizeof (szSrcCat), szProfileName);
  730.     FillLBox (hwnd, wLBID, szSrcCat, szIniFile);                                       
  731.     return;
  732. }
  733.   
  734. //------------------------------------------------------------------------
  735. // WriteActiveGeneric  Writes a list for generic selection
  736. //------------------------------------------------------------------------
  737. void WriteActiveGeneric (HWND hwnd, WORD wLBID, char * szcategory) {
  738.     char    szCat[64];
  739.     char *    pszTemp;
  740.     int    i, sCnt;
  741.  
  742.     sCnt = (int) SendDlgItemMessage (hwnd, IDD_SELACT, LB_GETCOUNT, 
  743.                                      0, 0L);
  744.     itoa (sCnt, szBuffer, 10);
  745.     WritePrivateProfileString (szCurcategory, "Count", 
  746.                                szBuffer, szProfileName);
  747.  
  748.     strcpy (szCat, szCurcategory);
  749.     strcat (szCat, " Active");                                                     
  750.     WritePrivateProfileString (szCat, 0, "", szProfileName);
  751.  
  752.     for (i = 0;i < sCnt; i++) {
  753.         SendDlgItemMessage (hwnd, wLBID, LB_GETTEXT, i,
  754.                              (LPARAM)(LPSTR) szBuffer);
  755.  
  756.         pszTemp = strchr (szBuffer, '\t');
  757.         if (pszTemp != 0) {
  758.             *pszTemp = 0;                                     
  759.             WritePrivateProfileString (szCat, szBuffer, 
  760.                              pszTemp+1, szProfileName);
  761.         }                     
  762.     }    
  763.    return;
  764. }
  765. //------------------------------------------------------------------------
  766. // GetActiveSelections Returns the active selections for a category
  767. //------------------------------------------------------------------------
  768. void GetActiveSelections (HWND hwnd, WORD wLBID, char * szcategory) {
  769.    char szTemp [64];
  770.    
  771.    strcpy (szTemp, szcategory);
  772.     strcat (szTemp, " Active");
  773.     FillLBox (hwnd, wLBID, szTemp, szProfileName);
  774. }
  775.  
  776. //------------------------------------------------------------------------
  777. // Transfer String - Transfer string from 1 list box to another
  778. //------------------------------------------------------------------------
  779. void TransferStr (HWND hwnd, WORD wSrcID, WORD wDestID, int sIndex) {
  780.  
  781.     SendDlgItemMessage (hwnd, wSrcID, LB_GETTEXT, sIndex,
  782.                          (LPARAM)(LPSTR) szBuffer);
  783.  
  784.     if (SendDlgItemMessage (hwnd, wDestID, LB_FINDSTRING, 0, 
  785.                               (LPARAM)(LPSTR) szBuffer) == LB_ERR) {
  786.         SendDlgItemMessage (hwnd, wDestID, LB_ADDSTRING, 
  787.                             0, (LPARAM)(LPSTR) szBuffer);
  788.         SendDlgItemMessage (hwnd, wSrcID, LB_DELETESTRING, 
  789.                             sIndex, 0);
  790.     }                        
  791. }                        
  792.  
  793. //------------------------------------------------------------------------
  794. // CompareLists Compares the 2 listboxes to insure no duplicates
  795. //------------------------------------------------------------------------
  796. void CompareLists (HWND hwnd, WORD wSrcID, WORD wDestID) {
  797.     int    i, sCnt;
  798.     DWORD    dwMatch;
  799.  
  800.     sCnt = (int) SendDlgItemMessage (hwnd, wSrcID, LB_GETCOUNT, 0, 0L);
  801.  
  802.     for (i = 0;i < sCnt; i++) {
  803.         SendDlgItemMessage (hwnd, wSrcID, LB_GETTEXT, i,
  804.                              (LPARAM)(LPSTR) szBuffer);
  805.         dwMatch = SendDlgItemMessage (hwnd, wDestID, LB_FINDSTRING, 
  806.                                       -1, (LPARAM)(LPSTR) szBuffer);
  807.         if (dwMatch != LB_ERR) 
  808.             SendDlgItemMessage (hwnd, wDestID, LB_DELETESTRING, 
  809.                                 (WPARAM)dwMatch, 0);
  810.     }                        
  811. }
  812.  
  813. //------------------------------------------------------------------------
  814. // SetButtons Sets the proper states for each button in the edit LBox
  815. //------------------------------------------------------------------------
  816. void SetButtons (HWND hwnd) {
  817.    
  818.     if (SendDlgItemMessage (hwnd, IDD_SELPOS, LB_GETCOUNT, 0, 0L)) {
  819.         EnableWindow (GetDlgItem (hwnd, IDD_SELADDALL), TRUE);
  820.         if (SendDlgItemMessage (hwnd, IDD_SELPOS, 
  821.                                     LB_GETCURSEL, 0, 0L) != LB_ERR) {
  822.             EnableWindow (GetDlgItem (hwnd, IDD_SELADD), TRUE);
  823.             EnableWindow (GetDlgItem (hwnd, IDD_SELREM), FALSE);
  824.         } else
  825.             EnableWindow (GetDlgItem (hwnd, IDD_SELADD), FALSE);
  826.     } else {
  827.         EnableWindow (GetDlgItem (hwnd, IDD_SELADD), FALSE);
  828.         EnableWindow (GetDlgItem (hwnd, IDD_SELADDALL), FALSE);
  829.     }
  830.         
  831.     if (SendDlgItemMessage (hwnd, IDD_SELACT, LB_GETCOUNT, 0, 0L)) {
  832.         EnableWindow (GetDlgItem (hwnd, IDD_SELREMALL), TRUE);
  833.         if (SendDlgItemMessage (hwnd, IDD_SELACT, 
  834.                                     LB_GETCURSEL, 0, 0L) != LB_ERR) {
  835.             EnableWindow (GetDlgItem (hwnd, IDD_SELREM), TRUE);
  836.             EnableWindow (GetDlgItem (hwnd, IDD_SELADD), FALSE);
  837.         } else
  838.             EnableWindow (GetDlgItem (hwnd, IDD_SELREM), FALSE);
  839.     } else {
  840.         EnableWindow (GetDlgItem (hwnd, IDD_SELREM), FALSE);
  841.         EnableWindow (GetDlgItem (hwnd, IDD_SELREMALL), FALSE);
  842.     }    
  843.     return;
  844. }
  845. //------------------------------------------------------------------------
  846. // Config category box dialog procedure
  847. //------------------------------------------------------------------------
  848. BOOL FAR PASCAL ConfigDlgProc (HWND hwnd, WORD message, WORD wParam, 
  849.                                LONG lParam) {
  850.     static    int    sChgType, sCurSelType;
  851.     int    i;
  852.     char    szTemp[64], szTemp1[64];
  853.  
  854.     switch (message) {
  855.  
  856.         case WM_INITDIALOG:
  857.         
  858.             strcpy (szTemp, "Edit ");
  859.             strcat (szTemp, szCurcategory);
  860.             SetWindowText (hwnd, szTemp);
  861.             sChgType = (int) GetPrivateProfileInt ("categories", 
  862.                                   szCurcategory, -1, szProfileName);                                            
  863.             CheckRadioButton (hwnd, IDD_CHRND, IDD_CHNO, 
  864.                               sChgType + IDD_CHRND);
  865.                                                         
  866.             sCurSelType = (int) GetPrivateProfileInt (szCurcategory, "Type", 
  867.                                                       -1, szProfileName);
  868.             i = 200;   //Make tab stop array
  869.             SendDlgItemMessage (hwnd, IDD_SELPOS, LB_SETTABSTOPS, 1, 
  870.                                 (LPARAM)(LPINT)&i);
  871.             SendDlgItemMessage (hwnd, IDD_SELACT, LB_SETTABSTOPS, 1, 
  872.                                 (LPARAM)(LPINT)&i);
  873.                                                       
  874.             GetActiveSelections (hwnd, IDD_SELACT, szCurcategory);
  875.             
  876.             switch (sCurSelType) {
  877.                //Color
  878.                case 0:
  879.                     GetPossibleGeneric (hwnd, IDD_SELPOS, "Colors");
  880.                    break;
  881.                    
  882.                //Wallpaper
  883.                case 1:               
  884.                     GetWindowsDirectory (szTemp, sizeof (szTemp));   
  885.                     strcpy (szTemp1, szTemp);               
  886.                     strcat (szTemp1, "\\*.bmp");               
  887.                    GetPossibleFiles (hwnd, IDD_SELPOS, szTemp, szTemp1, FALSE);
  888.                    break;
  889.                    
  890.                //Screen Saver
  891.                case 2:
  892.                     GetWindowsDirectory (szTemp, sizeof (szTemp));   
  893.                     strcpy (szTemp1, szTemp);               
  894.                     strcat (szTemp1, "\\*.scr");               
  895.                    GetPossibleScrSav (hwnd, IDD_SELPOS, szTemp, szTemp1);
  896.                    break;
  897.                    
  898.                //Pattern
  899.                case 3:
  900.                     GetPossibleGeneric (hwnd, IDD_SELPOS, "Pattern");
  901.                    break;
  902.                    
  903.             }
  904.             //
  905.             //Eliminate any duplicate entries
  906.             //
  907.             CompareLists (hwnd, IDD_SELACT, IDD_SELPOS);
  908.  
  909.             if (SendDlgItemMessage (hwnd, IDD_SELACT, LB_GETCOUNT, 0, 0L)) 
  910.                    SendDlgItemMessage (hwnd, IDD_SELACT, LB_SETCURSEL, 
  911.                                         GetPrivateProfileInt (szCurcategory, 
  912.                                                               "Active", 0, 
  913.                                                               szProfileName),
  914.                                        0L);
  915.             else             
  916.                 if (SendDlgItemMessage (hwnd, IDD_SELPOS, LB_GETCOUNT, 0, 0L)) 
  917.                    SendDlgItemMessage (hwnd, IDD_SELPOS, LB_SETCURSEL, 0, 0L);
  918.             SetButtons(hwnd);            
  919.             return TRUE;
  920.         
  921.         case WM_COMMAND:
  922.             switch (wParam) {
  923.            //
  924.            // Listbox controls
  925.            //
  926.               case IDD_SELPOS:
  927.                   if (HIWORD (lParam) == LBN_SELCHANGE) {
  928.                        SendDlgItemMessage (hwnd, IDD_SELACT, LB_SETCURSEL, -1, 0L);
  929.                         SetButtons(hwnd);
  930.                   }   
  931.                   if (HIWORD (lParam) == LBN_DBLCLK) 
  932.                        PostMessage (hwnd, WM_COMMAND, IDD_SELADD, 0L);
  933.  
  934.                   break;
  935.                                 
  936.               case IDD_SELACT:
  937.                   if (HIWORD (lParam) == LBN_SELCHANGE) {
  938.                        SendDlgItemMessage (hwnd, IDD_SELPOS, LB_SETCURSEL, -1, 0L);
  939.                         SetButtons(hwnd);
  940.                   }   
  941.                   if (HIWORD (lParam) == LBN_DBLCLK) 
  942.                        PostMessage (hwnd, WM_COMMAND, IDD_SELREM, 0L);
  943.                   break;
  944.            //
  945.            // Button controls
  946.            //
  947.                 case IDD_CHRND:
  948.                 case IDD_CHSEQ:
  949.                 case IDD_CHNO:
  950.                     if (HIWORD (lParam) == BN_CLICKED) 
  951.                        sChgType = wParam - IDD_CHRND;
  952.                     break;
  953.  
  954.                 case IDD_SELADDALL:
  955.                     i = (int) SendDlgItemMessage (hwnd, IDD_SELPOS, 
  956.                                                   LB_GETCOUNT, 0, 0L);
  957.                     for (;i > 0; --i) 
  958.                        TransferStr (hwnd, IDD_SELPOS, IDD_SELACT, 0);
  959.                     
  960.                     if (SendDlgItemMessage (hwnd, IDD_SELACT, LB_GETCOUNT, 0, 0L)) 
  961.                        SendDlgItemMessage (hwnd, IDD_SELACT, LB_SETCURSEL, 0, 0L);
  962.                     SetButtons(hwnd);
  963.                 return TRUE;
  964.                 
  965.                 case IDD_SELADD:
  966.                     i = (int) SendDlgItemMessage (hwnd, IDD_SELPOS, 
  967.                                                    LB_GETCURSEL, 0, 0L);
  968.                    if (i != LB_ERR) 
  969.                       TransferStr (hwnd, IDD_SELPOS, IDD_SELACT, i);
  970.                     if (SendDlgItemMessage (hwnd, IDD_SELPOS, LB_GETCOUNT, 0, 0L))
  971.                        SendDlgItemMessage (hwnd, IDD_SELPOS, LB_SETCURSEL, 0, 0L);
  972.                     else
  973.                        SendDlgItemMessage (hwnd, IDD_SELACT, LB_SETCURSEL, 0, 0L);
  974.                        
  975.                     SetButtons(hwnd);
  976.                     return TRUE;
  977.                 
  978.                 case IDD_SELREM:
  979.                     i = (int) SendDlgItemMessage (hwnd, IDD_SELACT, 
  980.                                                    LB_GETCURSEL, 0, 0L);
  981.                    if (i != LB_ERR)  
  982.                        TransferStr (hwnd, IDD_SELACT, IDD_SELPOS, i);
  983.                     if (SendDlgItemMessage (hwnd, IDD_SELACT, LB_GETCOUNT, 0, 0L)) 
  984.                        SendDlgItemMessage (hwnd, IDD_SELACT, LB_SETCURSEL, 0, 0L);
  985.                     else
  986.                        SendDlgItemMessage (hwnd, IDD_SELPOS, LB_SETCURSEL, 0, 0L);
  987.                     SetButtons(hwnd);
  988.                     return TRUE;
  989.                     
  990.                 case IDD_SELREMALL:
  991.                     i = (int) SendDlgItemMessage (hwnd, IDD_SELACT, 
  992.                                                   LB_GETCOUNT, 0, 0L);
  993.                     for (;i > 0; --i) 
  994.                        TransferStr (hwnd, IDD_SELACT, IDD_SELPOS, 0);
  995.                     if (SendDlgItemMessage (hwnd, IDD_SELPOS, LB_GETCOUNT, 0, 0L))
  996.                        SendDlgItemMessage (hwnd, IDD_SELPOS, LB_SETCURSEL, 0, 0L);
  997.                     SetButtons(hwnd);
  998.                 return TRUE;
  999.                 
  1000.                 case IDOK:
  1001.                     WriteActiveGeneric (hwnd, IDD_SELACT, szCurcategory);
  1002.                itoa (sChgType, szTemp, 10);
  1003.                     WritePrivateProfileString ("categories", 
  1004.                                    szCurcategory, szTemp, szProfileName);
  1005.  
  1006.                     WritePrivateProfileString (szCurcategory, "Active", 
  1007.                                                "0", szProfileName);
  1008.                     EndDialog (hwnd, 1);
  1009.                     return TRUE;
  1010.                     
  1011.                 case IDCANCEL:
  1012.                     EndDialog (hwnd, 0);
  1013.                     return TRUE;
  1014.             }
  1015.             break;
  1016.  
  1017.         case WM_CLOSE:
  1018.             EndDialog (hwnd, 0);
  1019.             return TRUE;
  1020.     }
  1021.     return FALSE;    
  1022. }
  1023.  
  1024. //------------------------------------------------------------------------
  1025. // About box dialog procedure
  1026. //------------------------------------------------------------------------
  1027. BOOL FAR PASCAL AboutDlgProc (HWND hwnd, WORD message, WORD wParam, 
  1028.                                LONG lParam) {
  1029.  
  1030.     switch (message) {
  1031.  
  1032.         case WM_COMMAND:
  1033.             switch (wParam) {
  1034.  
  1035.                 case IDCANCEL:
  1036.                 case IDOK:
  1037.                     EndDialog (hwnd, 1);
  1038.                     return TRUE;
  1039.             }
  1040.             break;
  1041.  
  1042.         case WM_CLOSE:
  1043.             EndDialog (hwnd, 0);
  1044.             return TRUE;
  1045.     }
  1046.     return FALSE;    
  1047. }
  1048.  
  1049. 
  1050.