home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n05 / gotcha.exe / CD32DEMO.C < prev    next >
C/C++ Source or Header  |  1995-05-01  |  8KB  |  241 lines

  1. //
  2. // CD32DEMO.C -- Demonstrates modifying the new Explorer-Like
  3. //               file open dialog.
  4. //
  5.  
  6. #define INMAIN       // Only define this for the file with WinMain in it!
  7. #include "cd32demo.h"
  8. #include <commdlg.h>
  9. #include <dlgs.h>
  10.  
  11. // Private function prototypes (this file scope only)
  12.  
  13. LONG WndProc_WM_COMMAND ( WINDOWS_PARAMS );
  14. BOOL bShowCustomOpenFileDialog ( HWND hWndParent );
  15. DLGPROC CustomTemplateDlgProc (DIALOG_PARAMS);
  16.  
  17. /**************************************************************
  18. *                                                             *
  19. *                      WinMain                                *
  20. *                                                             *
  21. **************************************************************/
  22.  
  23. int PASCAL WinMain (HANDLE hInstance,  HANDLE hPrevInstance, 
  24.                     LPSTR lpszCmdLine, int    nCmdShow       )
  25. {
  26.   MSG         msg       ;
  27.   WNDCLASS    wndclass  ;
  28.              
  29.   if (!hPrevInstance) 
  30.     {
  31.  
  32.     // Register Parent Window Class
  33.  
  34.     wndclass.style         = CS_HREDRAW | CS_VREDRAW | CS_BYTEALIGNCLIENT;
  35.     wndclass.lpfnWndProc   = (WNDPROC)CD32DEMO_WndProc ;
  36.     wndclass.cbClsExtra    = 0 ;
  37.     wndclass.cbWndExtra    = 0 ;
  38.     wndclass.hInstance     = hInstance ;
  39.     wndclass.hIcon         = LoadIcon ( hInstance, "BASE1");
  40.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  41.     wndclass.hbrBackground = GetStockObject ( GRAY_BRUSH ) ;
  42.     wndclass.lpszMenuName  = (LPSTR)"PlainMenu" ;
  43.     wndclass.lpszClassName = szAppName ;
  44.  
  45.     if (!RegisterClass (&wndclass))
  46.          return FALSE;
  47.     }
  48.  
  49.   ghInst   = hInstance;
  50.  
  51.   ghWnd =        CreateWindow (szAppName, 
  52.                                szCaption,
  53.                                WS_OVERLAPPEDWINDOW,
  54.                                CW_USEDEFAULT, 0,
  55.                                CW_USEDEFAULT, 0,
  56.                                (HWND)NULL, 
  57.                                (HMENU)NULL, 
  58.                                hInstance, 
  59.                                (LPVOID)NULL) ;
  60.  
  61.   ShowWindow ( ghWnd, nCmdShow );
  62.   UpdateWindow ( ghWnd );
  63.  
  64.   while (GetMessage((LPMSG)&msg, NULL, 0, 0))
  65.     {
  66.     TranslateMessage(&msg);
  67.     DispatchMessage(&msg);
  68.     }
  69. }
  70.  
  71. /*********************************************************************
  72. *                                                                    *
  73. *                       WndProc: Main Message Translator             *
  74. *                                                                    *
  75. *********************************************************************/
  76.  
  77. WINPROC CD32DEMO_WndProc ( WINDOWS_PARAMS )
  78. {
  79.   switch ( msg )
  80.     {
  81.     case WM_DESTROY :
  82.             
  83.       PostQuitMessage (0) ;
  84.       break ;
  85.  
  86.     case WM_COMMAND :
  87.  
  88.       return WndProc_WM_COMMAND ( hWnd, msg, wParam, lParam );
  89.       break;
  90.  
  91.     default :
  92.  
  93.       return DefWindowProc ( hWnd, msg, wParam, lParam );
  94.  
  95.     }
  96.   return 0L ;
  97. }
  98.  
  99. /*********************************************************************
  100. *                                                                    *
  101. *                                                                    *
  102. *                                                                    *
  103. *********************************************************************/
  104.  
  105. LONG WndProc_WM_COMMAND ( WINDOWS_PARAMS )
  106. {
  107.   CRACKER_VARS
  108.   CRACK_MESSAGEsc
  109.  
  110.   switch ( CRACKER_wID )
  111.     {
  112.     case IDM_FILEOPEN :
  113.  
  114.        bShowCustomOpenFileDialog ( hWnd );
  115.        break;
  116.  
  117.     default:
  118.  
  119.        return DefWindowProc ( hWnd, msg, wParam, lParam ) ;
  120.        break;
  121.     }
  122. }
  123.  
  124. /*********************************************************************
  125. *                                                                    *
  126. *                                                                    *
  127. *                                                                    *
  128. *********************************************************************/
  129.  
  130. BOOL bShowCustomOpenFileDialog ( HWND hWndParent )
  131. {
  132.     OPENFILENAME OpenFileName;
  133.     TCHAR         szFile[MAX_PATH]      = "\0";
  134.  
  135.     lstrcpy( szFile, "");
  136.  
  137.     // Fill in the OPENFILENAME structure to support a template and hook.
  138.  
  139.     OpenFileName.lStructSize       = sizeof(OPENFILENAME);
  140.     OpenFileName.hwndOwner         = hWndParent;
  141.     OpenFileName.hInstance         = ghInst;
  142.     OpenFileName.lpstrFilter       = NULL;
  143.     OpenFileName.lpstrCustomFilter = NULL;
  144.     OpenFileName.nMaxCustFilter    = 0;
  145.     OpenFileName.nFilterIndex      = 0;
  146.     OpenFileName.lpstrFile         = szFile;
  147.     OpenFileName.nMaxFile          = sizeof(szFile);
  148.     OpenFileName.lpstrFileTitle    = NULL;
  149.     OpenFileName.nMaxFileTitle     = 0;
  150.     OpenFileName.lpstrInitialDir   = NULL;
  151.     OpenFileName.lpstrTitle        = "Open File";
  152.     OpenFileName.nFileOffset       = 0;
  153.     OpenFileName.nFileExtension    = 0;
  154.     OpenFileName.lpstrDefExt       = NULL;
  155.     OpenFileName.lCustData         = (LPARAM)NULL;
  156.     OpenFileName.lpfnHook            = CustomTemplateDlgProc;  // See below
  157.     OpenFileName.lpTemplateName    = MAKEINTRESOURCE(IDD_DIALOG1); // In RC file
  158.     OpenFileName.Flags             = OFN_SHOWHELP | OFN_EXPLORER | OFN_ENABLEHOOK |
  159.                                      OFN_ENABLETEMPLATE;
  160.  
  161.     // Call the common dialog function.
  162.     return GetOpenFileName(&OpenFileName);
  163. }
  164.  
  165. /*********************************************************************
  166. *                                                                    *
  167. *                                                                    *
  168. *                                                                    *
  169. *********************************************************************/
  170.  
  171. DLGPROC CustomTemplateDlgProc (DIALOG_PARAMS)
  172. {
  173.     CRACKER_VARS
  174.  
  175.     switch (msg)
  176.       {
  177.       case WM_NOTIFY:  // This is sent by the common dialog to our child dialog
  178.  
  179.         switch (((LPOFNOTIFY)lParam)->hdr.code)
  180.           {
  181.           case CDN_SELCHANGE:  // User changed file selection
  182.             {
  183.             char szFullName[MAX_PATH];
  184.             char szText[MAX_PATH + 2 + 32];
  185.  
  186.             BOOL bErr = TRUE;
  187.  
  188.             // Get the full path of the file the user selected
  189.             // Notice how we use the parent of this dialog, which
  190.             // is the actual common dialog.
  191.             if (CommDlg_OpenSave_GetFilePath(GetParent(hDlg),
  192.                 szFullName, sizeof(szFullName)) <= sizeof(szFullName))
  193.               {
  194.               HFILE hFile;
  195.               OFSTRUCT of;
  196.               LONG dwSize;
  197.  
  198.               // Calculate its size
  199.               if (hFile = OpenFile ( szFullName, &of, OF_READ ))
  200.                  {
  201.                  // SetFilePointer replaces _llseek
  202.                  dwSize = SetFilePointer((HANDLE)hFile, 0, NULL, FILE_END);
  203.                  // Close yer file
  204.                  _lclose (hFile);
  205.                  // Format the string
  206.                  wsprintf ( szText, "%s, %d bytes", szFullName, dwSize);
  207.                  // Set the text in our child dialog
  208.                  SetDlgItemText(hDlg, IDC_NAMESIZE, szText);
  209.                  // We made it, Yogi!
  210.                  bErr = FALSE;  
  211.                  }
  212.               }
  213.  
  214.             if (bErr) SetDlgItemText(hDlg, IDC_NAMESIZE, "");
  215.             } // end case
  216.           } // end switch
  217.         break;
  218.  
  219.       case WM_COMMAND:  // User causes a command notification in our child dialog
  220.  
  221.         CRACK_MESSAGEsc
  222.         switch ( CRACKER_wID )
  223.            {
  224.            case IDC_CHEESE:  // Clicked on the cheese button!
  225.  
  226.              MessageBox ( GetParent(hDlg), "David Campbell Likes Cheese", "MSJ", MB_OK );
  227.              break;
  228.  
  229.            default:
  230.              return FALSE;
  231.            }
  232.         break;
  233.  
  234.       default:
  235.  
  236.         return FALSE;
  237.       }
  238.     return TRUE;
  239. }
  240.  
  241.