home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winui / cmddlg / cdtest / cdtest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-22  |  10.8 KB  |  549 lines

  1. /************************************************************************
  2.  
  3.   File: cdtest.c
  4.  
  5.   Purpose:  Contains the functions which handle the main window's functionality --
  6.             creating the main window, handling menu commands, exiting the program.
  7.  
  8.   Functions:
  9.  
  10.            WinMain()           - Program's entry point
  11.  
  12.            InitApplication()   - Registers class and some user defined messages
  13.  
  14.            InitInstance()      - Creates main window
  15.  
  16.            MainWndProc()       - Window procedure for main window
  17.  
  18.            HandleTheCommand()  - Processes all WM_COMMAND messages
  19.  
  20.            InitGlobals()       - Initializes all global variables that need to
  21.                                  be initialized at startup
  22.  
  23.            MyAtol()            - Converts an ASCII string in either hexi-
  24.                                  decimal or decimal notation to a LONG.
  25.  
  26.            AboutProc()         - Callback function for CDTEST's about box.
  27.  
  28. ************************************************************************/
  29.  
  30.  
  31.  
  32. #include <windows.h>
  33. #include <commdlg.h>
  34. #include <winnls.h>
  35. #include "cdtest.h"
  36. #include "colors.h"
  37. #include "save.h"
  38. #include "print.h"
  39. #include "title.h"
  40. #include "replace.h"
  41. #include "open.h"
  42. #include "font.h"
  43. #include "find.h"
  44.  
  45.  
  46.  
  47.  
  48.  
  49. /* Some defines, global variables, and function declarations */
  50.  
  51. #define szClass TEXT("cdtestclass")
  52. #define szIcon  TEXT("theicon")
  53. #define szMenu  TEXT("themenu")
  54.  
  55. #ifdef UNICODE
  56.   #define szTitle TEXT("Common Dialog Test App - Unicode Version")
  57. #else
  58.   #define szTitle TEXT("Common Dialog Test App - ANSI Version")
  59. #endif
  60.  
  61.  
  62. void InitGlobals(void) ;
  63.  
  64. UINT uMode = IDM_HEXMODE ;
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. /************************************************************************
  73.  
  74.  
  75.   Function: WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  76.  
  77.   Purpose:
  78.  
  79.     - Contains standard windows entry point
  80.  
  81.     - Initializes the application
  82.  
  83.     - Contains the main message loop
  84.  
  85.   Returns: Final msg.wParam
  86.  
  87.   Comments:
  88.  
  89. ************************************************************************/
  90.  
  91.  
  92. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  93. {
  94.  
  95.   MSG msg;
  96.  
  97.  
  98.   UNREFERENCED_PARAMETER( lpCmdLine );
  99.  
  100.  
  101.   if (!hPrevInstance)
  102.     if (!InitApplication(hInstance))
  103.         return (FALSE);
  104.  
  105.   if (!InitInstance(hInstance, nCmdShow))
  106.     return (FALSE);
  107.  
  108.   while (GetMessage(&msg, 0, 0, 0))
  109.   {
  110.     TranslateMessage(&msg);
  111.     DispatchMessage(&msg);
  112.   }
  113.  
  114.  
  115.   return (msg.wParam);
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. /************************************************************************
  125.  
  126.  
  127.   Function: InitApplication(HANDLE)
  128.  
  129.  
  130.   Purpose:
  131.  
  132.     - Fills in the WNDCLASS structure
  133.  
  134.     - Registers messages needed to communicate with the common dialogs
  135.  
  136.     - Registers the window class
  137.  
  138.   Returns: The return value of RegisterClass().  If this fails then the
  139.            whole program fails.
  140.  
  141.   Comments:
  142.  
  143.  
  144. ************************************************************************/
  145.  
  146.  
  147. BOOL InitApplication(HANDLE hInstance)     
  148. {
  149.   WNDCLASS  wc;
  150.  
  151.   wc.style = 0;
  152.   wc.lpfnWndProc = (WNDPROC)MainWndProc;
  153.   wc.cbClsExtra = 0;
  154.   wc.cbWndExtra = 0;
  155.   wc.hInstance = hInstance;
  156.   wc.hIcon = LoadIcon(hInstance, szIcon) ;
  157.   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  158.   wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  159.   wc.lpszMenuName = szMenu ;
  160.   wc.lpszClassName = szClass ;
  161.  
  162.   
  163.   InitGlobals() ;
  164.  
  165.   
  166.   /* Register any messages that the common dialogs will need to 
  167.      communicate with this app. */
  168.  
  169.   nFindMsg       = RegisterWindowMessage((LPTSTR) FINDMSGSTRING) ;
  170.   nOpenShareVMsg = RegisterWindowMessage((LPTSTR) SHAREVISTRING) ;
  171.   nHelpMessage   = RegisterWindowMessage((LPTSTR) HELPMSGSTRING) ;
  172.  
  173.  
  174.   return (RegisterClass(&wc));
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184. /************************************************************************
  185.  
  186.  
  187.  Function: InitInstance(HANDLE, int)
  188.  
  189.  
  190.  Purpose:
  191.  
  192.    - Creates the main window
  193.  
  194.    - Shows the main window
  195.  
  196.  Returns: FALSE if it cannot create the window, TRUE if it can.
  197.  
  198.  
  199.  Comments:
  200.  
  201.  
  202. ************************************************************************/
  203.  
  204.  
  205. BOOL InitInstance(HANDLE hInstance, int nCmdShow)
  206. {
  207.     HWND hWnd;
  208.     DWORD dwFlags = WS_OVERLAPPED | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU ;
  209.  
  210.     
  211.     /* save the instance handle in a global variable */
  212.  
  213.     hInst = hInstance;
  214.  
  215.  
  216.     /* Create the main window */
  217.  
  218.     hWnd = CreateWindow(szClass, szTitle, dwFlags, 
  219.                         10, 10, 400, 200, NULL, NULL, hInstance, NULL) ;
  220.  
  221.     if (!hWnd)
  222.         return (FALSE);
  223.  
  224.     ShowWindow(hWnd, SW_SHOWNORMAL) ;
  225.     UpdateWindow(hWnd);
  226.  
  227.     return (TRUE);
  228. }
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237. /************************************************************************
  238.  
  239.  
  240.   Function: MainWndProc(HWND, UINT, UINT, LONG)
  241.  
  242.   Purpose:
  243.  
  244.     - Is the callback function that handles all messages for the main window
  245.  
  246.   Returns:
  247.  
  248.     - Returns DefWindowProc() for any message it does not explicitly
  249.       respond to.
  250.  
  251.   Comments:
  252.  
  253.  
  254. ************************************************************************/
  255.  
  256.  
  257. LONG APIENTRY MainWndProc(HWND hWnd, UINT message, UINT wParam, LONG lParam)
  258. {
  259.     switch (message)
  260.     {
  261.       case WM_CREATE:
  262.         CheckMenuItem(GetMenu(hWnd), IDM_HEXMODE, MF_CHECKED) ;
  263.         break ;
  264.  
  265.       case WM_COMMAND:
  266.         HandleTheCommand(hWnd, wParam, lParam) ;
  267.         break ;
  268.  
  269.       case WM_DESTROY:
  270.           PostQuitMessage(0);
  271.           break;
  272.  
  273.       default:
  274.           return (DefWindowProc(hWnd, message, wParam, lParam));
  275.           break ;
  276.     }
  277.     return (0L);
  278. }
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285. /************************************************************************
  286.  
  287.  
  288.   Function: HandleTheCommand(HWND, UINT, UINT)
  289.  
  290.   Purpose:
  291.  
  292.     - Handles all WM_COMMAND messages passed to the MainWndProc().  The
  293.       menu or control ID value for Win32 WM_COMMAND messages is contained
  294.       in the low word of the wParam parameter.
  295.  
  296.   Returns: Nothing.
  297.  
  298.   Comments:
  299.  
  300.  
  301. ************************************************************************/
  302.  
  303.  
  304. void HandleTheCommand(HWND hWnd, UINT wParam, LONG lParam)
  305. {
  306.  
  307.   switch (LOWORD(wParam))
  308.   {
  309.     case IDM_COLOR:                //For any of the dialog creation
  310.       DoColorsDialog(hWnd) ;       //commands, call the appropriate
  311.       break ;                      //function.  The function will
  312.                                    //create the dialog...
  313.     case IDM_FONT:
  314.       DoFontDialog(hWnd) ;
  315.       break ;
  316.  
  317.     case IDM_TITLE:
  318.       DoTitleDialog(hWnd) ;
  319.       break ;
  320.  
  321.     case IDM_FIND:
  322.       DoFindDialog(hWnd) ;
  323.       break ;
  324.  
  325.     case IDM_OPEN:
  326.       DoOpenDialog(hWnd) ;
  327.       break ;
  328.  
  329.     case IDM_SAVE:
  330.       DoSaveDialog(hWnd) ;
  331.       break ;
  332.  
  333.     case IDM_PRINT:
  334.       DoPrintDialog(hWnd) ;
  335.       break ;
  336.  
  337.     case IDM_REPLACE:
  338.       DoReplaceDialog(hWnd) ;
  339.       break ;
  340.  
  341.     case IDM_EXIT:
  342.       PostQuitMessage(0) ;
  343.       break ;
  344.  
  345.     case IDM_HEXMODE:
  346.     case IDM_DECIMALMODE:
  347.  
  348.       /* We need to maintain a global variable that will indicate what
  349.          kind of number processing we have to do.  First, check the
  350.          menu item corresponding to the new mode... */
  351.  
  352.       CheckMenuItem(GetMenu(hWnd), uMode, MF_UNCHECKED) ;
  353.       uMode = wParam ;
  354.       CheckMenuItem(GetMenu(hWnd), uMode, MF_CHECKED) ;
  355.  
  356.  
  357.       /* and then create an appropriate filter for wsprintf() type
  358.          functions */
  359.  
  360.       if (uMode == IDM_HEXMODE)
  361.       {
  362.         lstrcpy(szShortFilter, TEXT("%x")) ;
  363.         lstrcpy(szLongFilter, TEXT("%lx")) ;
  364.       }
  365.       if (uMode == IDM_DECIMALMODE)
  366.       {
  367.         lstrcpy(szShortFilter, TEXT("%d")) ;
  368.         lstrcpy(szLongFilter, TEXT("%ld")) ;
  369.       }
  370.  
  371.       break ;
  372.  
  373.  
  374.     case IDM_ABOUT:
  375.        DialogBox(hInst, TEXT("about"), hWnd, AboutProc) ;
  376.        break ;
  377.  
  378.  
  379.     default: break ;
  380.   }
  381.  
  382.   return ;
  383. }
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392. /************************************************************************
  393.  
  394.  
  395.   Function: InitGlobals(void)
  396.  
  397.   Purpose:
  398.  
  399.     - Any global variables can be initialized here since this function is
  400.       called on app startup.
  401.  
  402.   Returns: Nothing
  403.  
  404.   Comments:
  405.  
  406.  
  407. ************************************************************************/
  408.  
  409.  
  410. void InitGlobals(void)
  411. {
  412.  
  413.   /* not really too much to do here.  Create a hex wsprintf() filter since
  414.      the app starts off in Hex mode. */
  415.  
  416.   lstrcpy(szShortFilter, TEXT("%x")) ;
  417.   lstrcpy(szLongFilter, TEXT("%lx")) ;
  418.  
  419. }
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429. /************************************************************************
  430.  
  431.  
  432.   Function: MyAtol(LPTSTR, BOOL, LPBOOL)
  433.  
  434.   Purpose:
  435.  
  436.     - This function will convert an ascii string to a LONG.
  437.  
  438.   Returns:
  439.  
  440.     - If it receives an invalid ascii character, it will return 0 and
  441.       set the LPBOOL variable to false...
  442.  
  443.   Comments:
  444.  
  445.       Since the function may need to deal with either a hex number or a decimal
  446.       number, it should use a variable as a multiplier.
  447.  
  448.  
  449. ************************************************************************/
  450.  
  451.  
  452. LONG MyAtol(LPTSTR szString, BOOL bHex, LPBOOL bSuccess)
  453. {
  454.   LPTSTR p ;
  455.   LONG l ;
  456.   LONG lMultiplier ;
  457.   BOOL bDigit ;
  458.  
  459.   if (bHex)
  460.     lMultiplier = 16 ;
  461.   else
  462.     lMultiplier = 10 ;
  463.  
  464.   p = szString ;
  465.   l = 0 ;
  466.  
  467.   while (*p)      //while chars
  468.   {
  469.      bDigit = FALSE ;  //set to false for each char that we look at
  470.  
  471.      if (*p >= (TCHAR) '0' && *p <= (TCHAR) '9')  //is it an ascii char ?
  472.      {
  473.        bDigit = TRUE ;
  474.        l+=(*p - (TCHAR) '0') ;
  475.      }
  476.  
  477.      if (bHex)
  478.      {
  479.        if (*p >= (TCHAR) 'A' && *p <= (TCHAR) 'F')  //or hex?
  480.        {
  481.          l+=(*p - (TCHAR) 'A' + 10) ;
  482.          bDigit = TRUE ;
  483.        }
  484.  
  485.        if (*p >= (TCHAR) 'a' && *p <= (TCHAR) 'f') 
  486.        {
  487.          l+=(*p - (TCHAR) 'a' + 10) ;
  488.          bDigit = TRUE ;
  489.        }
  490.  
  491.      }
  492.  
  493.      if (bDigit == FALSE)
  494.      {
  495.        *bSuccess = FALSE ;
  496.        return 0 ;
  497.      }
  498.  
  499.      p++ ;               //get next char
  500.  
  501.      if (*p)             //if there is going to be at least one more char
  502.        l*=lMultiplier ;  //then multiply what we have by the multiplier...
  503.   }
  504.  
  505.   *bSuccess = TRUE ;
  506.  
  507.   return l ;             //success! return the value.
  508. }
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518. /************************************************************************
  519.  
  520.  
  521.   Function: AboutProc(HWND, UINT, UINT, LONG)
  522.  
  523.   Purpose:  Callback function for the about dialog box.
  524.  
  525.  
  526.   Returns:  BOOL - FALSE ...
  527.  
  528.  
  529.   Comments:
  530.  
  531. ************************************************************************/
  532.  
  533.  
  534. BOOL APIENTRY AboutProc(HWND hwnd, UINT msg, UINT wParam, LONG lParam)
  535. {
  536.  
  537.   if (msg == WM_INITDIALOG)
  538.     return TRUE;
  539.   if (
  540.        (msg == WM_COMMAND) &&
  541.        (LOWORD(wParam) == IDCANCEL)
  542.      )
  543.  
  544.      EndDialog(hwnd, FALSE) ;
  545.  
  546.   else
  547.     return FALSE ;
  548. }
  549.