home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 9.ddi / CMDLGAP.ZIP / CMDLGAP.CPP next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  9.0 KB  |  263 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2. //
  3. // Cmdlgap.cpp - Common Dialogs example in OWL
  4.  
  5. // Beginning of file cmdlgap.cpp
  6.  
  7. /*
  8. This program has example code that makes use of the Common Dialogs available
  9. in Windows 3.1.  It is meant to be a simple example, which is why there are
  10. no classes derived from TDialog for each of the Common Dialogs.  The code
  11. that actually brings up the Common Dialogs can be easily ported to Windows
  12. applications written in C.
  13.  
  14. The main window will have menu selections for opening a file, changing the
  15. font and changing the color used for the selected font.  When a file is
  16. selected the name will be displayed on the client area of the window.
  17.  
  18. The files needed to build this program are . . .
  19.  
  20. cmdlgap.h                Header file for application
  21. cmdlgapr.h                Header file for application and resources
  22. cmdlgap.cpp                Source file for application
  23. cmdlgap.rc                Resource file for application
  24. */
  25.  
  26. #include "cmdlgap.h"
  27.  
  28. TCMDLGApp::TCMDLGApp( LPSTR lpszNam, HINSTANCE hInst, HINSTANCE hPrevInst,
  29.     LPSTR lpszCmdLn, int nCmdShw )
  30.     : TApplication( lpszNam, hInst, hPrevInst, lpszCmdLn, nCmdShw )
  31. {
  32. }
  33.  
  34. TCMDLGApp::~TCMDLGApp()
  35. {
  36. }
  37.  
  38. void TCMDLGApp::InitMainWindow()
  39. {
  40.     MainWindow = new TCMDLGWnd( NULL, "Common Dialog example" );
  41. }
  42.  
  43. TCMDLGWnd::TCMDLGWnd( PTWindowsObject AParent, LPSTR ATitle )
  44.     : TWindow( AParent, ATitle )
  45. {
  46. /*
  47. The OPENFILENAME, CHOOSECOLOR and CHOOSEFONT structures could be members of
  48. this OWL window class.  If the initialization were done in this constructor
  49. the HWindow data member _would not_ contain the appropriate value.  A better
  50. place for the initialization of structures requiring an HWindow would be a
  51. virtual SetupWindow() member function.
  52. */
  53.     AssignMenu( "CMDLGAPMENU" );      // Set up the menu
  54.     strcpy( szName, "" );           // Empty the file name string
  55.     crColor = RGB( 0, 0, 0 );       // Use black as the default color
  56.     hfFont = 0;                     // Empty the handle to the font
  57.     tfFontLoaded = FALSE;           // Set the font selected flag to false
  58. }
  59.  
  60. TCMDLGWnd::~TCMDLGWnd()
  61. {
  62.    if (hfFont)
  63.       DeleteObject(hfFont);
  64. }
  65.  
  66. LPSTR TCMDLGWnd::GetClassName()
  67. {
  68.     return "TCMDLGWnd";
  69. }
  70.  
  71. // Display the file name using the selected font in the selected color.
  72. void TCMDLGWnd::Paint( HDC hdc, PAINTSTRUCT _FAR & )
  73. {
  74.     HFONT fTemp;    // Placeholder for the original font
  75.     RECT rTemp;        // Client are needed by DrawText()
  76.  
  77.     SetTextColor( hdc, crColor );
  78.     if( tfFontLoaded == TRUE ) fTemp = (HFONT)SelectObject( hdc, hfFont );
  79.     GetClientRect( HWindow, &rTemp );
  80.     DrawText( hdc, szName, strlen( szName ), &rTemp, DT_CENTER | DT_WORDBREAK );
  81.     if( tfFontLoaded == TRUE )
  82.         SelectObject( hdc, fTemp );
  83. }
  84.  
  85. void TCMDLGWnd::CMExit( RTMessage Msg )
  86. {
  87.     TWindow::CMExit( Msg );
  88. }
  89.  
  90. /***************************************************************************
  91. Using the OPENFILENAME structure and the Windows 3.1 API call
  92. GetOpenFileName() eases the selection of files for the programmer and for
  93. the user.  The WINHELP.EXE help file WIN31WH.HLP (found in the BORLANDC\BIN
  94. directory) contains a detailed description of the function call and its
  95. associated structure.  The Flags field of the structure is particularly
  96. useful when custimization is required.
  97. ****************************************************************************/
  98. void TCMDLGWnd::CMUFileOpen( RTMessage )
  99. {
  100.     OPENFILENAME ofnTemp;
  101.     char szTemp[] = "All Files (*.*)\0*.*\0Text Files (*.txt)\0*.txt\0";
  102. /*
  103. Note the initialization method of the above string.  The GetOpenFileName()
  104. function expects to find a string in the OPENFILENAME structure that has
  105. a '\0' terminator between strings and an extra '\0' that terminates the
  106. entire filter data set.  Using the technique shown below will fail because
  107. "X" is really 'X' '\0' '\0' '\0' in memory.  When the GetOpenFileName()
  108. function scans szTemp it will stop after finding the extra trailing '\0'
  109. characters.
  110.  
  111.     char szTemp[][4] = { "X", "*.*", "ABC", "*.*", "" };
  112.  
  113. The string should be "X\0*.*\0ABC\0*.*\0".
  114.  
  115. Remember that in C or C++ a quoted string is automatically terminated with
  116. a '\0' character so   char "X\0";   would result in 'X' '\0' '\0' which
  117. provides the extra '\0' that GetOpenFileName() needs to see in order to
  118. terminate the scan of the string.  Just 'char ch "X";' would result in 'X'
  119. '\0' and GetOpenFileName() would wander off in memory until it lucked into
  120. a '\0' '\0' character sequence.
  121. */
  122.  
  123. /*
  124. Some Windows structures require the size of themselves in an effort to
  125. provide backward compatibility with future versions of Windows.  If the
  126. lStructSize member is not set the call to GetOpenFileName() will fail.
  127. */
  128.     ofnTemp.lStructSize = sizeof( OPENFILENAME );
  129.     ofnTemp.hwndOwner = HWindow;            // An invalid hWnd causes non-modality
  130.     ofnTemp.hInstance = 0;
  131.     ofnTemp.lpstrFilter = (LPSTR)szTemp;    // See previous note concerning string
  132.     ofnTemp.lpstrCustomFilter = NULL;
  133.     ofnTemp.nMaxCustFilter = 0;
  134.     ofnTemp.nFilterIndex = 1;
  135.     ofnTemp.lpstrFile = (LPSTR)szName;        // Stores the result in this variable
  136.     ofnTemp.nMaxFile = sizeof( szName );
  137.     ofnTemp.lpstrFileTitle = NULL;
  138.     ofnTemp.nMaxFileTitle = 0;
  139.     ofnTemp.lpstrInitialDir = NULL;
  140.     ofnTemp.lpstrTitle = Title;                // Title for dialog
  141.     ofnTemp.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
  142.     ofnTemp.nFileOffset = 0;
  143.     ofnTemp.nFileExtension = 0;
  144.     ofnTemp.lpstrDefExt = "*";
  145.     ofnTemp.lCustData = NULL;
  146.     ofnTemp.lpfnHook = NULL;
  147.     ofnTemp.lpTemplateName = NULL;
  148. /*
  149. If the call to GetOpenFileName() fails you can call CommDlgExtendedError()
  150. to retrieve the type of error that occured.
  151. */
  152.     if(GetOpenFileName( &ofnTemp ) != TRUE)
  153.     {
  154.         DWORD Errval;    // Error value
  155.         char Errstr[50]="GetOpenFileName returned Error #";
  156.         char buf[5];    // Error buffer
  157.  
  158.         Errval=CommDlgExtendedError();
  159.         if(Errval!=0)   // 0 value means user selected Cancel
  160.         {
  161.             sprintf(buf,"%ld",Errval);
  162.             strcat(Errstr,buf);
  163.             MessageBox(HWindow,Errstr,"WARNING",MB_OK|MB_ICONSTOP);
  164.         }
  165.     }
  166.     InvalidateRect( HWindow, NULL, TRUE );    // Repaint to display the new name
  167. }
  168.  
  169. /***************************************************************************
  170. Using the CHOOSECOLOR structure and the Windows 3.1 API call ChooseColor(),
  171. eases the selection of colors for the programmer and for the user.  The
  172. comments for the File Open dialog regarding the help file and the structure
  173. size also apply to the color dialog.
  174. ****************************************************************************/
  175. void TCMDLGWnd::CMUColor( RTMessage )
  176. {
  177.     CHOOSECOLOR ccTemp;
  178.     COLORREF crTemp[16];    // Important, sometimes unused, array
  179.  
  180.     ccTemp.lStructSize = sizeof( CHOOSECOLOR );
  181.     ccTemp.hwndOwner = HWindow;
  182.     ccTemp.hInstance = 0;
  183.     ccTemp.rgbResult = crColor;    // CC_RGBINIT flag makes this the default color
  184. /*
  185. lpCustColors must be set to a valid array of 16 COLORREF's, even if it
  186. is not used.  If it isn't you will probably fail with a GP fault in
  187. COMMDLG.DLL.
  188. */
  189.     ccTemp.lpCustColors = crTemp;
  190.     ccTemp.Flags = CC_PREVENTFULLOPEN | CC_RGBINIT;
  191.     ccTemp.lCustData = 0L;
  192.     ccTemp.lpfnHook = NULL;
  193.     ccTemp.lpTemplateName = NULL;
  194.     if( ChooseColor( &ccTemp ) == TRUE )
  195.         crColor = ccTemp.rgbResult;
  196.     InvalidateRect( HWindow, NULL, TRUE );
  197. }
  198.  
  199. /***************************************************************************
  200. Using the CHOOSEFONT structure and the Windows 3.1 API call ChooseFont()
  201. eases the selection of fonts for the programmer and for the user.  The
  202. comments for the File Open dialog regarding the help file and the structure
  203. size also apply to the font dialog.
  204. ****************************************************************************/
  205. void TCMDLGWnd::CMUFont( RTMessage )
  206. {
  207. /*
  208. The variables below are static so that multiple calls to the font dialog will
  209. retain previous user selections.
  210. */
  211.     static CHOOSEFONT cfTemp;
  212.     static LOGFONT lfTemp;
  213.  
  214.     if( tfFontLoaded == TRUE )    // cfTemp contains previous selections
  215.     {
  216.         cfTemp.Flags |= CF_INITTOLOGFONTSTRUCT;
  217.         cfTemp.rgbColors = crColor;
  218.     }
  219.     else
  220.     {
  221.         cfTemp.lStructSize = sizeof( CHOOSEFONT );
  222.         cfTemp.hwndOwner = HWindow;
  223.         cfTemp.hDC = 0;
  224.         cfTemp.lpLogFont = &lfTemp;    // Store the result here
  225.         cfTemp.Flags = CF_EFFECTS | CF_FORCEFONTEXIST | CF_SCREENFONTS;
  226.         cfTemp.rgbColors = crColor;    // Color and font dialogs use the same color
  227.         cfTemp.lCustData = 0L;
  228.         cfTemp.lpfnHook = NULL;
  229.         cfTemp.lpTemplateName = NULL;
  230.         cfTemp.hInstance = 0;
  231.         cfTemp.lpszStyle = NULL;
  232.         cfTemp.nFontType = SCREEN_FONTTYPE;
  233.         cfTemp.nSizeMin = 0;
  234.         cfTemp.nSizeMax = 0;
  235.     }
  236.     if( ChooseFont( &cfTemp ) == TRUE )
  237.     {
  238.         if( tfFontLoaded == TRUE )
  239.             DeleteObject( hfFont );
  240.         crColor = cfTemp.rgbColors;
  241.         hfFont = CreateFontIndirect( &lfTemp );
  242.         tfFontLoaded = TRUE;
  243.     }
  244.     InvalidateRect( HWindow, NULL, TRUE );
  245. }
  246.  
  247. void TCMDLGWnd::CMUHelpAbout( RTMessage )
  248. {
  249.     MessageBox(HWindow,szCMDLGAPAbout,"ABOUT BOX",MB_OK);
  250. }
  251.  
  252. int PASCAL WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpszCmdLn,
  253.     int nCmdShw )
  254. {
  255.     TCMDLGApp App( "CMDLGAP", hInst, hPrevInst, lpszCmdLn, nCmdShw );
  256.  
  257.     App.Run();
  258.     return App.Status;
  259. }
  260.  
  261. // End of file cmdlgap.cpp
  262.  
  263.