home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / CMDLG.PAK / CMDLG.C next >
C/C++ Source or Header  |  1995-08-29  |  13KB  |  360 lines

  1. // (C) Copyright 1994 by Borland International
  2. //
  3. // Cmdlg.c - Common Dialogs example in C
  4.  
  5. /*********************************************************************
  6. This program has example code that makes use of the Common Dialogs
  7. available in Windows 3.1.  It is meant to be a simple example using the
  8. C programming language.
  9.  
  10. The main window will have menu selections for opening a file, changing the
  11. font and changing the color used for the selected font.  When a file is
  12. selected the name will be displayed on the client area of the window.
  13.  
  14. The files needed to build this program are . . .
  15.  
  16. cmdlg.h         Header file for application
  17. cmdlgr.h           Header file for application and resources
  18. cmdlg.c         Source file for application
  19. cmdlg.rc           Resource file for application
  20. ***********************************************************************/
  21.  
  22. #include <windowsx.h>
  23. // Contains the necessary headers and definitions for this program
  24. #include "cmdlg.h"
  25.  
  26. /***  Global Variables  ***/
  27. char szName[256];
  28. COLORREF crColor;
  29. HFONT hfFont;
  30. BOOL tfFontLoaded;
  31. HINSTANCE hInst;  // current instance
  32.  
  33.  
  34. /*********************************************************************
  35. Using the OPENFILENAME structure and the Windows 3.1 API call
  36. GetOpenFileName() eases the selection of files for the programmer and for
  37. the user.  The WINHELP.EXE help file WIN31WH.HLP (found in the BORLANDC\BIN
  38. directory) contains a detailed description of the function call and its
  39. associated structure.  The Flags field of the structure is particularly
  40. useful when custimization is required.
  41. **********************************************************************/
  42. void CMUFileOpen( HWND hWnd )
  43. {
  44.   OPENFILENAME ofnTemp;
  45.   DWORD Errval; // Error value
  46.   char buf[5];  // Error buffer
  47.   char Errstr[50]="GetOpenFileName returned Error #";
  48.   char szTemp[] = "All Files (*.*)\0*.*\0Text Files (*.txt)\0*.txt\0";
  49. /*
  50. Note the initialization method of the above string.  The GetOpenFileName()
  51. function expects to find a string in the OPENFILENAME structure that has
  52. a '\0' terminator between strings and an extra '\0' that terminates the
  53. entire filter data set.  Using the technique shown below will fail because
  54. "X" is really 'X' '\0' '\0' '\0' in memory.  When the GetOpenFileName()
  55. function scans szTemp it will stop after finding the extra trailing '\0'
  56. characters.
  57.  
  58.   char szTemp[][4] = { "X", "*.*", "ABC", "*.*", "" };
  59.  
  60. The string should be "X\0*.*\0ABC\0*.*\0".
  61.  
  62. Remember that in C or C++ a quoted string is automatically terminated with
  63. a '\0' character so   char "X\0";   would result in 'X' '\0' '\0' which
  64. provides the extra '\0' that GetOpenFileName() needs to see in order to
  65. terminate the scan of the string.  Just 'char ch "X";' would result in 'X'
  66. '\0' and GetOpenFileName() would wander off in memory until it lucked into
  67. a '\0' '\0' character sequence.
  68. */
  69.  
  70. /*
  71. Some Windows structures require the size of themselves in an effort to
  72. provide backward compatibility with future versions of Windows.  If the
  73. lStructSize member is not set the call to GetOpenFileName() will fail.
  74. */
  75.   ofnTemp.lStructSize = sizeof( OPENFILENAME );
  76.   ofnTemp.hwndOwner = hWnd; // An invalid hWnd causes non-modality
  77.   ofnTemp.hInstance = 0;
  78.   ofnTemp.lpstrFilter = (LPSTR)szTemp;  // See previous note concerning string
  79.   ofnTemp.lpstrCustomFilter = NULL;
  80.   ofnTemp.nMaxCustFilter = 0;
  81.   ofnTemp.nFilterIndex = 1;
  82.   ofnTemp.lpstrFile = (LPSTR)szName;  // Stores the result in this variable
  83.   ofnTemp.nMaxFile = sizeof( szName );
  84.   ofnTemp.lpstrFileTitle = NULL;
  85.   ofnTemp.nMaxFileTitle = 0;
  86.   ofnTemp.lpstrInitialDir = NULL;
  87.   ofnTemp.lpstrTitle = "OPENFILENAME";  // Title for dialog
  88.   ofnTemp.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
  89.   ofnTemp.nFileOffset = 0;
  90.   ofnTemp.nFileExtension = 0;
  91.   ofnTemp.lpstrDefExt = "*";
  92.   ofnTemp.lCustData = 0L;
  93.   ofnTemp.lpfnHook = NULL;
  94.   ofnTemp.lpTemplateName = NULL;
  95. /*
  96. If the call to GetOpenFileName() fails you can call CommDlgExtendedError()
  97. to retrieve the type of error that occured.
  98. */
  99.   if(GetOpenFileName( &ofnTemp ) != TRUE)
  100.   {
  101.     Errval=CommDlgExtendedError();
  102.     if(Errval!=0) // 0 value means user selected Cancel
  103.     {
  104.       sprintf(buf,"%ld",Errval);
  105.       strcat(Errstr,buf);
  106.       MessageBox(hWnd,Errstr,"WARNING",MB_OK|MB_ICONSTOP);
  107.     }
  108.  
  109.   }
  110.   InvalidateRect( hWnd, NULL, TRUE ); // Repaint to display the new name
  111. }
  112.  
  113. /*************************************************************************
  114. Using the CHOOSECOLOR structure and the Windows 3.1 API call ChooseColor(),
  115. eases the selection of colors for the programmer and for the user.  The
  116. comments for the File Open dialog regarding the help file and the structure
  117. size also apply to the color dialog.
  118. **************************************************************************/
  119. void CMUColor( HWND hWnd )
  120. {
  121.   CHOOSECOLOR ccTemp;
  122.   COLORREF crTemp[16];  // Important, sometimes unused, array
  123.  
  124.   ccTemp.lStructSize = sizeof( CHOOSECOLOR );
  125.   ccTemp.hwndOwner = hWnd;
  126.   ccTemp.hInstance = 0;
  127.   ccTemp.rgbResult = crColor; // CC_RGBINIT flag makes this the default color
  128. /*
  129. lpCustColors must be set to a valid array of 16 COLORREF's, even if it
  130. is not used.  If it isn't you will probably fail with a GP fault in
  131. COMMDLG.DLL.
  132. */
  133.   ccTemp.lpCustColors = crTemp;
  134.   ccTemp.Flags = CC_PREVENTFULLOPEN | CC_RGBINIT;
  135.   ccTemp.lCustData = 0L;
  136.   ccTemp.lpfnHook = NULL;
  137.   ccTemp.lpTemplateName = NULL;
  138.   if( ChooseColor( &ccTemp ) == TRUE ) crColor = ccTemp.rgbResult;
  139.   InvalidateRect( hWnd, NULL, TRUE );
  140. }
  141.  
  142. /**************************************************************************
  143. Using the CHOOSEFONT structure and the Windows 3.1 API call ChooseFont()
  144. eases the selection of fonts for the programmer and for the user.  The
  145. comments for the File Open dialog regarding the help file and the structure
  146. size also apply to the font dialog.
  147. ***************************************************************************/
  148. void CMUFont( HWND hWnd )
  149. {
  150. /*
  151. The variables below are static so that multiple calls to the font dialog will
  152. retain previous user selections.
  153. */
  154.   static CHOOSEFONT cfTemp;
  155.   static LOGFONT lfTemp;
  156.  
  157.   if( tfFontLoaded == TRUE )  // cfTemp contains previous selections
  158.   {
  159.     cfTemp.Flags |= CF_INITTOLOGFONTSTRUCT;
  160.     cfTemp.rgbColors = crColor;
  161.   }
  162.   else
  163.   {
  164.     cfTemp.lStructSize = sizeof( CHOOSEFONT );
  165.     cfTemp.hwndOwner = hWnd;
  166.     cfTemp.hDC = 0;
  167.     cfTemp.lpLogFont = &lfTemp; // Store the result here
  168.     cfTemp.Flags = CF_EFFECTS | CF_FORCEFONTEXIST | CF_SCREENFONTS;
  169.     cfTemp.rgbColors = crColor; // Color and font dialogs use the same color
  170.     cfTemp.lCustData = 0L;
  171.     cfTemp.lpfnHook = NULL;
  172.     cfTemp.lpTemplateName = NULL;
  173.     cfTemp.hInstance = 0;
  174.     cfTemp.lpszStyle = NULL;
  175.     cfTemp.nFontType = SCREEN_FONTTYPE;
  176.     cfTemp.nSizeMin = 0;
  177.     cfTemp.nSizeMax = 0;
  178.   }
  179.   if( ChooseFont( &cfTemp ) == TRUE )
  180.   {
  181.     if( tfFontLoaded == TRUE )
  182.       DeleteObject( hfFont );
  183.     crColor = cfTemp.rgbColors;
  184.     hfFont = CreateFontIndirect( &lfTemp );
  185.     tfFontLoaded = TRUE;
  186.   }
  187.   InvalidateRect( hWnd, NULL, TRUE );
  188. }
  189.  
  190. /**********************************************************************/
  191. BOOL InitApplication(HINSTANCE hInstance)
  192. {
  193.   WNDCLASS  wc;
  194.  
  195.   // Fill in window class structure with parameters that describe the
  196.   // main window.
  197.  
  198.   wc.style = CS_HREDRAW | CS_VREDRAW; // Class style(s).
  199.   wc.lpfnWndProc = (long (FAR PASCAL*)())MainWndProc; // Function to retrieve messages for
  200.                             // windows of this class.
  201.   wc.cbClsExtra = 0;  // No per-class extra data.
  202.   wc.cbWndExtra = 0;  // No per-window extra data.
  203.   wc.hInstance = hInstance; // Application that owns the class.
  204.   wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  205.   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  206.   wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  207.   wc.lpszMenuName = "CMDLGAPMENU";  // Name of menu resource in .RC file.
  208.   wc.lpszClassName = "CMDLG"; // Name used in call to CreateWindow.
  209.  
  210.   /* Register the window class and return success/failure code. */
  211.  
  212.   return (RegisterClass(&wc));
  213.  
  214. }
  215.  
  216.  
  217. /************************************************************************/
  218. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  219. {
  220.   HWND  hWnd; // Main window handle.
  221.  
  222.   /* Save the instance handle in static variable, which will be used in  */
  223.   /* many subsequence calls from this application to Windows.            */
  224.  
  225.   hInst = hInstance;
  226.  
  227.   /* Create a main window for this application instance.  */
  228.  
  229.   hWnd = CreateWindow(
  230.     "CMDLG",                 // See RegisterClass() call.
  231.     "Common Dialog Example",  // Text for window title bar.
  232.     WS_OVERLAPPEDWINDOW,        // Window style.
  233.     CW_USEDEFAULT,         // Default horizontal position.
  234.     CW_USEDEFAULT,          // Default vertical position.
  235.     CW_USEDEFAULT,            // Default width.
  236.     CW_USEDEFAULT,        // Default height.
  237.     NULL,                  // Overlapped windows have no parent.
  238.     NULL,                  // Use the window class menu.
  239.     hInstance,              // This instance owns this window.
  240.     NULL                   // Pointer not needed.
  241.   );
  242.  
  243.   /* If window could not be created, return "failure" */
  244.  
  245.   if (!hWnd)
  246.     return (FALSE);
  247.  
  248.   /* Make the window visible; update its client area; and return "success" */
  249.  
  250.   ShowWindow(hWnd, nCmdShow); // Show the window
  251.   UpdateWindow(hWnd);     // Sends WM_PAINT message
  252.   return (TRUE);        // Returns the value from PostQuitMessage
  253.  
  254. }
  255.  
  256.  
  257. /****************************************************************************
  258.   FUNCTION: MainWndProc(HWND, UINT, WPARAM, LPARAM)
  259. ****************************************************************************/
  260. LRESULT FAR PASCAL _export MainWndProc(HWND hWnd, UINT message,
  261.                   WPARAM wParam, LPARAM lParam)
  262. {
  263.   HFONT fTemp = (HFONT)NULL;  // Placeholder for the original font
  264.   RECT rTemp;                 // Client are needed by DrawText()
  265.   HDC hdc;                    // HDC for Window
  266.   PAINTSTRUCT ps;             // Paint Struct for BeginPaint call
  267.  
  268.   switch (message) {
  269.   case WM_CREATE: // Initialize Global vars
  270.       strcpy( szName, "" );           // Empty the file name string
  271.       crColor = RGB( 0, 0, 0 );       // Use black as the default color
  272.       hfFont = 0;                     // Empty the handle to the font
  273.       tfFontLoaded = FALSE;           // Set the font selected flag to false
  274.     return 0L;
  275.  
  276.   case WM_PAINT:
  277.   // Display the file name using the selected font in the selected color.
  278.  
  279.       hdc=BeginPaint(hWnd,&ps);
  280.       SetTextColor( hdc, crColor );
  281.       if( tfFontLoaded == TRUE )
  282.         fTemp = (HFONT)SelectObject( hdc, hfFont );
  283.       GetClientRect( hWnd, &rTemp );
  284.       DrawText( hdc, szName, strlen( szName ), &rTemp, DT_CENTER | DT_WORDBREAK );
  285.       if( tfFontLoaded == TRUE )
  286.         SelectObject( hdc, fTemp );
  287.       EndPaint(hWnd,&ps);
  288.     break;
  289.  
  290.   case WM_COMMAND:  // message: command from application menu
  291.     switch(GET_WM_COMMAND_ID(wParam, lParam))
  292.     {
  293.       case CM_EXIT:
  294.           DestroyWindow(hWnd);
  295.         break;
  296.  
  297.       case CM_U_FILEOPEN:
  298.           CMUFileOpen(hWnd);
  299.         break;
  300.  
  301.       case CM_U_COLOR:
  302.           CMUColor(hWnd);
  303.         break;
  304.  
  305.       case CM_U_FONT:
  306.           CMUFont(hWnd);
  307.         break;
  308.  
  309.       case CM_U_HELPABOUT:
  310.           MessageBox(hWnd,szCMDLGAPAbout,"About CMDLG",MB_OK);
  311.         break;
  312.  
  313.       default:
  314.         break;
  315.     }
  316.     break;
  317.  
  318.   case WM_QUIT:
  319.   case WM_DESTROY:  // message: window being destroyed
  320.       if( hfFont != 0 )
  321.         DeleteObject( hfFont );
  322.       PostQuitMessage(0);
  323.     break;
  324.  
  325.   default:      // Passes it on if unproccessed
  326.     return (DefWindowProc(hWnd, message, wParam, lParam));
  327.   }
  328.   return 0L;
  329. }
  330.  
  331. #pragma argsused
  332. /**************************************************************/
  333. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  334.            LPSTR lpCmdLine, int nCmdShow)
  335. {
  336.   MSG msg;      // message
  337.   if (!hPrevInstance) // Other instances of app running?
  338.   if (!InitApplication(hInstance))  // Initialize shared things
  339.     return (FALSE); // Exits if unable to initialize
  340.  
  341.   /* Perform initializations that apply to a specific instance */
  342.  
  343.   if (!InitInstance(hInstance, nCmdShow))
  344.     return (FALSE);
  345.  
  346.   /* Acquire and dispatch messages until a WM_QUIT message is received. */
  347.  
  348.   while (GetMessage(&msg, // message structure
  349.     NULL, // handle of window receiving the message
  350.     0,    // lowest message to examine
  351.     0))   // highest message to examine
  352.   {
  353.   TranslateMessage(&msg); // Translates virtual key codes
  354.   DispatchMessage(&msg);  // Dispatches message to window
  355.   }
  356.   return (msg.wParam);  // Returns the value from PostQuitMessage
  357. }
  358.  
  359. // End of file cmdlg.c
  360.