home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Multimedia Jumpstart 1.1a / CD_ROM.BIN / develpmt / source / mergedib / rare.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-12  |  6.7 KB  |  225 lines

  1. /* rare.c
  2.  *
  3.  * Rarely-called (discardable) utility functions.
  4.  */
  5.  
  6. #include <windows.h>
  7. // commdlg included with 3.1 SDK or package available from MM BBS (206) 936-4082
  8. #include "commdlg.h"
  9. #include "MergeDIB.h"
  10.  
  11.  
  12. /* globals */
  13. char        gachAppName[20];     // for title bar etc.
  14.  
  15.  
  16. /* AppInit(hInst, hPrev)
  17.  *
  18.  * This is called when the application is first loaded into memory.
  19.  * It performs all initialization that doesn't need to be done once
  20.  * per instance.  Returns TRUE iff successful.
  21.  */
  22. BOOL FAR PASCAL
  23. AppInit(hInst, hPrev)
  24. HANDLE        hInst;        // instance handle of current instance
  25. HANDLE        hPrev;        // instance handle of previous instance
  26. {
  27.     WNDCLASS    wndclass;
  28.  
  29.     /* get the application name from a string resource */
  30.     gachAppName[0] = 0;        // in case of error
  31.     LoadString(ghInst, IDS_APPNAME, gachAppName, sizeof(gachAppName));
  32.  
  33. #ifdef XDEBUG
  34.     wpfGetDebugLevel(gachAppName);
  35.     dprintf("----- %s -----\n", (LPSTR) gachAppName);
  36. #endif
  37.  
  38.     if (!hPrev)
  39.     {
  40.         /* register a class for the main application window */
  41.         wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  42.         wndclass.hIcon = LoadIcon(hInst, "AppIcon");
  43.         wndclass.lpszMenuName = "AppMenu";
  44.         wndclass.lpszClassName = gachAppName;
  45.         wndclass.hbrBackground = (HBRUSH) COLOR_WINDOW + 1;
  46.         wndclass.hInstance = hInst;
  47.         wndclass.style = CS_BYTEALIGNCLIENT | CS_VREDRAW | CS_HREDRAW;
  48.         wndclass.lpfnWndProc = (LPWNDPROC) AppWndProc;
  49.         wndclass.cbWndExtra = 0;
  50.         wndclass.cbClsExtra = 0;
  51.  
  52.         if (!RegisterClass(&wndclass))
  53.             return FALSE;
  54.     }
  55.  
  56.     return TRUE;
  57. }
  58.  
  59.  
  60. /* AppExit(hwnd)
  61.  *
  62.  * This is called when the application is about to exit.  Global cleanup
  63.  * should be done here.  <hwnd> is the application window, which has *not*
  64.  * been destroyed yet (but is about to be destroyed).
  65.  */
  66. void FAR PASCAL
  67. AppExit(hwnd)
  68. HWND        hwnd;        // application window
  69. {
  70.     FreeDIB(PRIMARY_DIB);
  71.     FreeDIB(SECONDARY_DIB);
  72.     FreeDIB(MERGED_DIB);
  73. }
  74.  
  75.  
  76. /* AboutDlgProc(hwnd, wMsg, wParam, lParam)
  77.  *
  78.  * This function handles messages belonging to the "About" dialog box.
  79.  * The only message that it looks for is WM_COMMAND, indicating the use
  80.  * has pressed the "OK" button.  When this happens, it takes down
  81.  * the dialog box.  Returns TRUE iff message has been processed
  82.  */
  83. BOOL FAR PASCAL _export
  84. AboutDlgProc(hwnd, wMsg, wParam, lParam)
  85. HWND         hwnd;        // window handle of "about" dialog box
  86. WORD        wMsg;        // message number
  87. WORD         wParam;        // message-dependent parameter
  88. LONG         lParam;        // message-dependent parameter
  89. {
  90.     switch (wMsg)
  91.     {
  92.  
  93.     case WM_COMMAND:
  94.  
  95.         if (wParam == IDOK)
  96.             EndDialog(hwnd, TRUE);
  97.         break;
  98.  
  99.     case WM_INITDIALOG:
  100.  
  101.         return TRUE;
  102.  
  103.     }
  104.  
  105.     return FALSE;
  106. }
  107.  
  108.  
  109. /* fOK = PromptForFileName(hwndOwner, hInst, achFileName, cchFileName,
  110.  *                         idCaption, idFilter, idDefExt, dwFlags)
  111.  *
  112.  * Prompt the user for the name of a file to open or save to.  <hwndOwner>
  113.  * is the window that will own the dialog.  <hInst> is the module that the
  114.  * string resources and RCDATA resource (see below) will be loaded from.
  115.  *
  116.  * The returned file name will be placed in <achFileName>, which must
  117.  * have a capacity of at least <cchFileName> bytes.
  118.  *
  119.  * <idCaption> is the string resource id of the caption to display.
  120.  * <idFilter> is the RCDATA resource id that contains the filter spec
  121.  * (in the format used by COMMDLG).  <idDefExt> is the string resource id
  122.  * of the default extension (without the period).  Any or all of these
  123.  * three parameters may be NULL, for default behaviour.
  124.  *
  125.  * <dwFlags> are used as follows:
  126.  *   -- PFFN_OPENFILE: prompt the user for the name of a file to open
  127.  *   -- PFFN_SAVEFILE: prompt the user for the name of a file to save to
  128.  *   -- PFFN_SHOWDEFAULT: on entry, <achFileName> will contain the name
  129.  *      of a file to display as the "default file name"
  130.  *   -- PFFN_OVERWRITEPROMPT: prompt the user before accepting allowing them
  131.  *      to choose an existing file
  132.  *   -- PFFN_UPPERCASE: make <achFileName> uppercase before returning
  133.  */
  134. BOOL FAR PASCAL
  135. PromptForFileName(hwndOwner, hInst, achFileName, cchFileName,
  136.     idCaption, idFilter, idDefExt, dwFlags)
  137. HWND        hwndOwner;        // window that will own dialog box
  138. HANDLE        hInst;            // module that contains the resources
  139. LPSTR        achFileName;        // where to put file name
  140. WORD        cchFileName;        // size of <achFileName>
  141. WORD        idCaption;        // string ID of caption string
  142. WORD        idFilter;        // resource ID of RCDATA of file filter
  143. WORD        idDefExt;        // string ID of default extension
  144. DWORD        dwFlags;        // random flags
  145. {
  146.     char        achCaption[80];    // caption on Open File dialog
  147.     HANDLE        hResInfo;    // info about filter resource
  148.     HANDLE        hResData;    // handle to filter resource
  149.     char        achDefExt[4];    // default extension
  150.     OPENFILENAME    ofname;        // parameter block
  151.     BOOL        f;
  152.  
  153.     /* initialize <ofname.lpstrFilter> */
  154.     if (idFilter != NULL)
  155.     {
  156.         /* load the filter spec for GetOpenFileName() */
  157.         hResInfo = FindResource(hInst,
  158.             MAKEINTRESOURCE(idFilter), RT_RCDATA);
  159.         if ((hResData = LoadResource(hInst, hResInfo)) == NULL)
  160.             ofname.lpstrFilter = NULL;
  161.         else
  162.             ofname.lpstrFilter = LockResource(hResData);
  163.     }
  164.     else
  165.         ofname.lpstrFilter = NULL;
  166.  
  167.     /* initialize <ofname.lpstrTitle> */
  168.     if (idCaption != NULL)
  169.     {
  170.         achCaption[0] = 0;    // in case of error
  171.         LoadString(ghInst, idCaption, achCaption, sizeof(achCaption));
  172.         ofname.lpstrTitle = achCaption;
  173.     }
  174.     else
  175.         ofname.lpstrTitle = NULL;
  176.  
  177.     /* initialize <ofname.lpstrDefExt> */
  178.     if (idDefExt != NULL)
  179.     {
  180.         achDefExt[0] = 0;    // in case of error
  181.         LoadString(ghInst, idDefExt, achDefExt, sizeof(achDefExt));
  182.         ofname.lpstrDefExt = achDefExt;
  183.     }
  184.     else
  185.         ofname.lpstrDefExt = NULL;
  186.  
  187.     /* the initial file name is "" unless PFFN_SHOWDEFAULT is given */
  188.     if (!(dwFlags & PFFN_SHOWDEFAULT))
  189.         achFileName[0] = 0;
  190.  
  191.     /* fill in the other fields of <ofname> */
  192.     ofname.lStructSize = sizeof(ofname);
  193.     ofname.hwndOwner = hwndOwner;
  194.     ofname.hInstance = hInst;
  195.     ofname.lpstrCustomFilter = NULL;
  196.     ofname.nMaxCustFilter = 0;
  197.     ofname.nFilterIndex = 1;
  198.     ofname.lpstrFile = achFileName;
  199.     ofname.nMaxFile = cchFileName;
  200.     ofname.lpstrFileTitle = NULL;
  201.     ofname.nMaxFileTitle = 0;
  202.     ofname.lpstrInitialDir = NULL;
  203.     ofname.Flags = OFN_HIDEREADONLY |
  204.         (dwFlags & PFFN_OVERWRITEPROMPT ? OFN_OVERWRITEPROMPT : 0);
  205.     ofname.lCustData = NULL;
  206.     ofname.lpfnHook = NULL;
  207.     ofname.lpTemplateName = NULL;
  208.  
  209.     /* prompt the user for the file name */
  210.     if (dwFlags & PFFN_OPENFILE)
  211.         f = GetOpenFileName(&ofname);
  212.     else
  213.         f = GetSaveFileName(&ofname);
  214.  
  215.     /* unlock the OpenFileFilter resource */
  216.     if (idFilter != NULL)
  217.         UnlockResource(hResData);
  218.  
  219.     /* make the name uppercase if requested */
  220.     if (f && (dwFlags & PFFN_UPPERCASE))
  221.         AnsiUpper(achFileName);
  222.  
  223.     return f;
  224. }
  225.