home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1992 / 04 / demo3.c < prev    next >
Text File  |  1991-11-26  |  8KB  |  206 lines

  1. // DEMO3.C  Medium pen-aware Windows application that demonstrates 
  2. // simple text input or handwritten input via a dialog, selecting
  3. // a different dialog based on the presence or absence of Pen Windows, 
  4. // then displays same text in main window.
  5. // Copyright (C) 1991 Ray Duncan
  6.  
  7. #define WIN31
  8.  
  9. #include "windows.h"
  10. #include "penwin.h"
  11. #include "demo3.h"
  12.  
  13. HANDLE hInst;                               // instance handle
  14. HWND hWnd;                                  // main window handle
  15.  
  16. HANDLE hPenWin = NULL;                      // Pen Windows module handle
  17.  
  18. #define INPUTSIZE 80                        // max length of input
  19. #define OUTPUTSIZE 256                      // max length of output
  20.  
  21. char buf1[INPUTSIZE];                       // user's input buffer
  22. char buf2[OUTPUTSIZE];                      // formatted output buffer
  23.  
  24. //
  25. // WinMain - entry point from Windows.  Registers window class,
  26. // creates main window, processes messages until WM_QUIT received.
  27. //
  28. int PASCAL WinMain(HANDLE hInstance, 
  29.     HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  30. {
  31.     MSG msg;                     
  32.  
  33.     VOID (FAR PASCAL *RegisterPenApp)(WORD, BOOL) = NULL;
  34.  
  35.     hInst = hInstance;                      // save this instance handle
  36.  
  37.     strcpy(buf1, "Nothing yet!");           // initialize output text
  38.  
  39.     if(!hPrevInstance)                      // if first instance,
  40.         if(!InitApplication(hInstance))     // register window class
  41.             return (FALSE);                 // exit if couldn't register
  42.  
  43.     if(!InitInstance(hInstance, nCmdShow))  // create this instance's window
  44.         return (FALSE);
  45.  
  46.     if(hPenWin = GetSystemMetrics(SM_PENWINDOWS))   // Pen Windows running?
  47.     {                                       // if so, register as Pen app
  48.         if(RegisterPenApp = GetProcAddress(hPenWin, "RegisterPenApp"))
  49.             (*RegisterPenApp)(RPA_DEFAULT, TRUE);
  50.     }
  51.  
  52.     while(GetMessage(&msg, NULL, 0, 0))     // while message != WM_QUIT
  53.     {
  54.         TranslateMessage(&msg);             // translate virtual key codes
  55.         DispatchMessage(&msg);              // dispatch message to window
  56.     }
  57.     return (msg.wParam);                    // return code = WM_QUIT value
  58. }
  59.  
  60. //
  61. //  InitApplication - registers window class for this application
  62. //
  63. BOOL InitApplication(HANDLE hInstance)
  64. {
  65.     WNDCLASS  wc;
  66.  
  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 #3",              // 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.     ShowWindow(hWnd, nCmdShow);             // make window visible
  104.     UpdateWindow(hWnd);                     // force WM_PAINT message
  105.     return (TRUE);                          // return success flag
  106. }
  107.  
  108. //
  109. // MainWndProc - callback function for main application window
  110. //
  111. long FAR PASCAL MainWndProc(HWND hWnd, WORD wMsg, WORD wParam, LONG lParam)
  112. {
  113.     HDC hdc;                                // device context handle
  114.     PAINTSTRUCT ps;                         // painting info structure
  115.     RECT rect;                              // receives client rectangle
  116.  
  117.     switch (wMsg) 
  118.     {
  119.         case WM_COMMAND:                    // menu command received,
  120.             DoCommand(hWnd, wParam);
  121.             break;
  122.  
  123.         case WM_PAINT:                      // window needs repainting
  124.             hdc = BeginPaint(hWnd, &ps);    // get device context
  125.             GetClientRect(hWnd, &rect);     // get client rectangle
  126.             wsprintf(buf2, "You entered: %s", (LPSTR) buf1);
  127.             DrawText(hdc, buf2, -1, &rect,  // display formatted text
  128.                 DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  129.             EndPaint(hWnd, &ps);            // release device context
  130.             break;
  131.  
  132.         case WM_SIZE:                       // window resized
  133.             InvalidateRect(hWnd, NULL, TRUE);   // repaint everything
  134.             UpdateWindow(hWnd);             // force WM_PAINT message
  135.             break;
  136.  
  137.         case WM_DESTROY:                    // window being destroyed
  138.             PostQuitMessage(0);
  139.             break;
  140.  
  141.         default:                            // let Windows handle it
  142.             return(DefWindowProc(hWnd, wMsg, wParam, lParam));
  143.     }
  144.  
  145.     return(0);
  146. }
  147.  
  148. //
  149. // DoCommand - handles menu command messages.  In the case of an
  150. // IDM_ENTER message, uses the dialog TextEntryBox1 if ordinary
  151. // Windows is running, or TextEntryBox2 if Pen Windows is present.
  152. //
  153. void DoCommand(HWND hWnd, WORD wParam)
  154. {
  155.     FARPROC lpProc;                         // far pointer to callback 
  156.     LPSTR   lpszDialogName;                 // far pointer to dialog name
  157.  
  158.                                             // select appropriate dialog
  159.     lpszDialogName = hPenWin ? "TextEntryBox2" : "TextEntryBox1";
  160.  
  161.     switch(wParam)                          // decode it
  162.     {
  163.         case IDM_EXIT:                      // user picked File-Quit
  164.             SendMessage (hWnd, WM_CLOSE, 0, 0L);
  165.             break;
  166.  
  167.         case IDM_ENTER:                     // user picked Edit-Enter
  168.             lpProc = MakeProcInstance(EntryDlgProc, hInst);
  169.             DialogBox(hInst, lpszDialogName, hWnd, lpProc);
  170.             InvalidateRect(hWnd, NULL, TRUE);   // repaint everything
  171.             UpdateWindow(hWnd);             // force WM_PAINT message
  172.             FreeProcInstance(lpProc);
  173.             break;
  174.  
  175.         default:                            // unknown command, ignore it
  176.             break;
  177.     }
  178. }
  179.  
  180. //
  181. // EntryDlgProc - callback function for "Text Entry..." dialog
  182. //
  183. BOOL FAR PASCAL EntryDlgProc(HWND hDlg, WORD wMsg, WORD wParam, LONG lParam)
  184. {
  185.     switch (wMsg) 
  186.     {
  187.         case WM_INITDIALOG:                 // dialog box initialization
  188.             return (TRUE);                  // signal message processed
  189.  
  190.         case WM_COMMAND:                    // command received
  191.             if(wParam == IDOK)              // OK button clicked?
  192.             {                               // yes, retrieve text
  193.                 GetDlgItemText(hDlg, IDD_EDITTEXT, buf1, INPUTSIZE-1);
  194.                 EndDialog(hDlg, TRUE);      // signal text accepted
  195.             }
  196.             else if(wParam == IDD_CANCEL)   // Cancel button clicked?
  197.             {
  198.                 strcpy(buf1, "Input was cancelled!");
  199.                 EndDialog(hDlg, FALSE);     // signal text not accepted
  200.             }
  201.     }
  202.     return(FALSE);                          // signal message not processed
  203. }
  204.  
  205. 
  206.