home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / utility / v11n06.zip / DEMO5.C < prev    next >
Text File  |  1992-01-20  |  11KB  |  259 lines

  1. // DEMO5.C  Pen-aware Windows application that demonstrates 
  2. // keyboard text input or handwritten input via a dialog, validating
  3. // the input with the custom dictionary PCDICT.DLL. The data is then 
  4. // displayed in the main window.
  5. // Copyright (C) 1991 Ray Duncan
  6.  
  7. #define WIN31
  8.  
  9. #include "windows.h"
  10. #include "penwin.h"
  11. #include "demo5.h"
  12.  
  13. HANDLE hInst;                               // instance handle
  14. HWND hWnd = 0;                              // main window handle
  15. HWND hEditState = 0;                        // State BEDIT control handle
  16. HANDLE hUserDict = 0;                       // user dictionary handle
  17. unsigned hStateList = 0;                    // handle for state wordlist
  18. FARPROC lpUserDict = NULL;                  // user dictionary entry point
  19. HANDLE hPenWin = 0;                         // Pen Windows module handle
  20.  
  21. #define INPUTSIZE 80                        // max length of input
  22. #define OUTPUTSIZE 256                      // max length of output
  23.  
  24. char buf2[INPUTSIZE];                       // user's state input buffer
  25. char buf3[OUTPUTSIZE];                      // formatted output buffer
  26.  
  27. unsigned CancelFlag = TRUE;                 // TRUE if input cancelled
  28. unsigned ControlFlag = TRUE;                // TRUE if using HEDIT,
  29.                                             // FALSE if using BEDIT
  30. //
  31. // WinMain - entry point from Windows.  Registers window class,
  32. // creates main window, processes messages until WM_QUIT received.
  33. //
  34. int PASCAL WinMain(HANDLE hInstance, 
  35.     HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  36. {
  37.     MSG msg;                     
  38.  
  39.     hInst = hInstance;                      // save this instance handle
  40.  
  41.     if(!hPrevInstance)                      // if first instance,
  42.         if(!InitApplication(hInstance))     // register window class
  43.             return(FALSE);                  // exit if couldn't register
  44.  
  45.     if(!InitInstance(hInstance, nCmdShow))  // create this instance's window
  46.     {
  47.         MessageBox(0, "Initialization failed!", "DEMO5", MB_OK|MB_ICONSTOP);
  48.         return(FALSE);
  49.     }
  50.  
  51.     RegisterPenApp(RPA_DEFAULT, TRUE);      // enable HEDIT/BEDIT controls
  52.  
  53.     while(GetMessage(&msg, NULL, 0, 0))     // while message != WM_QUIT
  54.     {
  55.         TranslateMessage(&msg);             // translate virtual key codes
  56.         DispatchMessage(&msg);              // dispatch message to window
  57.     }
  58.     return(msg.wParam);                     // return code = WM_QUIT value
  59. }
  60.  
  61. //
  62. //  InitApplication - registers window class for this application
  63. //
  64. BOOL InitApplication(HANDLE hInstance)
  65. {
  66.     WNDCLASS  wc;
  67.                                             // set class parameters
  68.     wc.style = NULL;                        // class style
  69.     wc.lpfnWndProc = MainWndProc;           // class callback function
  70.     wc.cbClsExtra = 0;                      // extra per-class data
  71.     wc.cbWndExtra = 0;                      // extra per-window data
  72.     wc.hInstance = hInstance;               // handle of class owner
  73.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);     // default icon
  74.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);       // default cursor
  75.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); // background color 
  76.     wc.lpszMenuName =  "DemoMenu";          // name of menu resource
  77.     wc.lpszClassName = "DemoWinClass";      // name of window class
  78.  
  79.     return(RegisterClass(&wc));             // register class, return flag
  80. }
  81.  
  82. //
  83. // InitInstance - creates main window for this application instance
  84. //
  85. BOOL InitInstance(HANDLE hInstance, int nCmdShow)
  86. {
  87.     hWnd = CreateWindow(                    // create frame window
  88.         "DemoWinClass",                     // window class name
  89.         "Pen Windows Demo #5",              // text for title bar
  90.         WS_OVERLAPPEDWINDOW,                // window style
  91.         CW_USEDEFAULT,                      // default horizontal position
  92.         CW_USEDEFAULT,                      // default vertical position
  93.         CW_USEDEFAULT,                      // default width
  94.         CW_USEDEFAULT,                      // default height
  95.         NULL,                               // no parent window
  96.         NULL,                               // use class default menu
  97.         hInstance,                          // window owner
  98.         NULL                                // unused pointer
  99.     );
  100.  
  101.     if(!hWnd) return(FALSE);                // error, can't create window
  102.  
  103.                                             // link to user dictionary
  104.     if((hUserDict = LoadLibrary("pcdict.dll")) <= 32)
  105.         return(FALSE);                      // give up if dynlink failed
  106.     if((lpUserDict = GetProcAddress(hUserDict, "DictionaryProc")) == NULL)
  107.         return(FALSE);                      // give up if dynlink failed
  108.  
  109.                                             // open the states word list
  110.     (lpUserDict)(DIRQ_OPEN, (LPSTR) "states.dic", 
  111.                  (LPWORD) &hStateList, sizeof(hStateList), 0L, 0L);
  112.     if(!hStateList)                         // give up if state wordlist
  113.         return(FALSE);                      // was not found
  114.  
  115.     ShowWindow(hWnd, nCmdShow);             // make window visible
  116.     UpdateWindow(hWnd);                     // force WM_PAINT message
  117.     return(TRUE);                           // return success flag
  118. }
  119.  
  120. //
  121. // MainWndProc - callback function for main application window
  122. //
  123. long FAR PASCAL MainWndProc(HWND hWnd, WORD wMsg, WORD wParam, LONG lParam)
  124. {
  125.     HDC hdc;                                // device context handle
  126.     PAINTSTRUCT ps;                         // painting info structure
  127.     RECT rect;                              // receives client rectangle
  128.  
  129.     switch (wMsg) 
  130.     {
  131.         case WM_COMMAND:                    // menu command received,
  132.             DoCommand(hWnd, wParam);
  133.             break;
  134.  
  135.         case WM_PAINT:                      // window needs repainting
  136.             hdc = BeginPaint(hWnd, &ps);    // get device context
  137.             GetClientRect(hWnd, &rect);     // get client rectangle
  138.             if(!CancelFlag)                 // format user's input  
  139.                 wsprintf(buf3, "The state is %s.", (LPSTR) buf2);
  140.             else strcpy(buf3, "No data entered!");
  141.             DrawText(hdc, buf3, -1, &rect,  // display formatted text
  142.                 DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  143.             EndPaint(hWnd, &ps);            // release device context
  144.             break;
  145.  
  146.         case WM_SIZE:                       // window resized
  147.             InvalidateRect(hWnd, NULL, TRUE);   // repaint everything
  148.             UpdateWindow(hWnd);             // force WM_PAINT message
  149.             break;
  150.  
  151.         case WM_INITMENU:                    // set appropriate checkmarks 
  152.             if(ControlFlag)                    // on Options popup menu
  153.             {
  154.                 CheckMenuItem((HMENU) wParam, IDM_HEDIT, MF_CHECKED);
  155.                 CheckMenuItem((HMENU) wParam, IDM_BEDIT, MF_UNCHECKED);
  156.             }
  157.             else
  158.             {
  159.                 CheckMenuItem((HMENU) wParam, IDM_HEDIT, MF_UNCHECKED);
  160.                 CheckMenuItem((HMENU) wParam, IDM_BEDIT, MF_CHECKED);
  161.             }
  162.             break;
  163.  
  164.         case WM_DESTROY:                    // window being destroyed
  165.             PostQuitMessage(0);
  166.             break;
  167.  
  168.         default:                            // let Windows handle it
  169.             return(DefWindowProc(hWnd, wMsg, wParam, lParam));
  170.     }
  171.  
  172.     return(0);
  173. }
  174.  
  175. //
  176. // DoCommand - handles menu command messages.  
  177. //
  178. void DoCommand(HWND hWnd, WORD wParam)
  179. {
  180.     FARPROC lpProc;                         // far pointer to callback 
  181.     LPSTR   lpszDialogName;                 // far pointer to dialog name
  182.  
  183.                                             // select appropriate dialog
  184.     lpszDialogName = ControlFlag ? "TextEntryBox1" : "TextEntryBox2";
  185.  
  186.     switch(wParam)                          // decode it
  187.     {
  188.         case IDM_EXIT:                      // user picked File-Quit
  189.             SendMessage (hWnd, WM_CLOSE, 0, 0L);
  190.             break;
  191.  
  192.         case IDM_ENTER:                     // user picked Edit-Enter
  193.             lpProc = MakeProcInstance(EntryDlgProc, hInst);
  194.             CancelFlag = DialogBox(hInst, lpszDialogName, hWnd, lpProc);
  195.             InvalidateRect(hWnd, NULL, TRUE);   // repaint everything
  196.             UpdateWindow(hWnd);             // force WM_PAINT message
  197.             FreeProcInstance(lpProc);
  198.             break;
  199.  
  200.         case IDM_HEDIT:
  201.             ControlFlag = TRUE;
  202.             break;
  203.  
  204.         case IDM_BEDIT:
  205.             ControlFlag = FALSE;
  206.             break;
  207.  
  208.         default:                            // unknown command, ignore it
  209.             break;
  210.     }
  211. }
  212.  
  213. //
  214. // EntryDlgProc - callback function for "Text Entry..." dialog
  215. //
  216. BOOL FAR PASCAL EntryDlgProc(HWND hDlg, WORD wMsg, WORD wParam, LONG lParam)
  217. {
  218.     RC rc;
  219.  
  220.     switch (wMsg) 
  221.     {
  222.         case WM_INITDIALOG:                 // dialog box initialization
  223.  
  224.                                             // get edit control handle
  225.             hEditState = GetDlgItem(hDlg, IDD_EDITSTATE);
  226.  
  227.                                             // configure RC for State edit
  228.             SendMessage(hEditState, WM_HEDITCTL, HE_GETRC, (LONG) ((LPRC) &rc));
  229.             rc.lRcOptions = rc.lRcOptions | RCO_NOSPACEBREAK | RCO_SUGGEST;
  230. //          rc.lRcOptions = rc.lRcOptions | RCO_SUGGEST;
  231.             rc.rglpdf[0] = lpUserDict;
  232.             rc.rglpdf[1] = NULL;
  233.             rc.alc = ALC_DEFAULT;
  234.             SendMessage(hEditState, WM_HEDITCTL, HE_SETRC, (LONG) ((LPRC) &rc));
  235.  
  236.             return (TRUE);                  // signal message processed
  237.  
  238.         case WM_COMMAND:                    // command received
  239.             if(HIWORD(lParam) == EN_SETFOCUS)
  240.             {                               // edit is receiving focus,
  241.                 if(wParam == IDD_EDITSTATE)
  242.                     (lpUserDict)(DIRQ_SETWORDLISTS, (LPWORD) &hStateList,
  243.                                  0L, 1, 0L, 0L);
  244.             }
  245.             else if(wParam == IDOK)         // OK button clicked?
  246.             {                               // yes, retrieve text
  247.                 GetDlgItemText(hDlg, IDD_EDITSTATE, buf2, INPUTSIZE-1);
  248.                 EndDialog(hDlg, FALSE);     // signal text accepted
  249.             }
  250.             else if(wParam == IDD_CANCEL)   // Cancel button clicked?
  251.             {
  252.                 EndDialog(hDlg, TRUE);      // signal input was cancelled
  253.             }
  254.     }
  255.     return(FALSE);                          // signal message not processed
  256. }
  257.  
  258. 
  259.