home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1992 / 05 / demo4.c < prev    next >
Text File  |  1992-01-28  |  11KB  |  251 lines

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