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

  1. // DEMO1.C  Windows application that demonstrates simple text input
  2. //          via a dialog, then displays same text in main window.
  3. // Copyright (C) 1991 Ray Duncan
  4.  
  5. #include "windows.h"
  6. #include "demo1.h"
  7.  
  8. HANDLE hInst;                               // instance handle
  9. HWND hWnd;                                  // main window handle
  10.  
  11. #define INPUTSIZE 80                        // max length of input
  12. #define OUTPUTSIZE 256                      // max length of output
  13.  
  14. char buf1[INPUTSIZE];                       // user's input buffer
  15. char buf2[OUTPUTSIZE];                      // formatted output buffer
  16.  
  17. //
  18. // WinMain - entry point from Windows.  Registers window class,
  19. // creates main window, processes messages until WM_QUIT received.
  20. //
  21. int PASCAL WinMain(HANDLE hInstance, 
  22.     HANDLE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  23. {
  24.     MSG msg;                     
  25.  
  26.     hInst = hInstance;                      // save this instance handle
  27.  
  28.     strcpy(buf1, "Nothing yet!");           // initialize output text
  29.  
  30.     if(!hPrevInstance)                      // if first instance,
  31.         if(!InitApplication(hInstance))     // register window class
  32.             return (FALSE);                 // exit if couldn't register
  33.  
  34.     if(!InitInstance(hInstance, nCmdShow))  // create this instance's window
  35.         return (FALSE);
  36.  
  37.     while(GetMessage(&msg, NULL, 0, 0))     // while message != WM_QUIT
  38.     {
  39.         TranslateMessage(&msg);             // translate virtual key codes
  40.         DispatchMessage(&msg);              // dispatch message to window
  41.     }
  42.     return (msg.wParam);                    // return code = WM_QUIT value
  43. }
  44.  
  45. //
  46. //  InitApplication - registers window class for this application
  47. //
  48. BOOL InitApplication(HANDLE hInstance)
  49. {
  50.     WNDCLASS  wc;
  51.  
  52.                                             // set class parameters
  53.     wc.style = NULL;                        // class style
  54.     wc.lpfnWndProc = MainWndProc;           // class callback function
  55.     wc.cbClsExtra = 0;                      // extra per-class data
  56.     wc.cbWndExtra = 0;                      // extra per-window data
  57.     wc.hInstance = hInstance;               // handle of class owner
  58.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);     // default icon
  59.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);       // default cursor
  60.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); // background color 
  61.     wc.lpszMenuName =  "DemoMenu";          // name of menu resource
  62.     wc.lpszClassName = "DemoWinClass";      // name of window class
  63.  
  64.     return (RegisterClass(&wc));            // register class, return flag
  65. }
  66.  
  67. //
  68. // InitInstance - creates main window for this application instance
  69. //
  70. BOOL InitInstance(HANDLE hInstance, int nCmdShow)
  71. {
  72.     hWnd = CreateWindow(                    // create frame window
  73.         "DemoWinClass",                     // window class name
  74.         "Pen Windows Demo #1",              // text for title bar
  75.         WS_OVERLAPPEDWINDOW,                // window style
  76.         CW_USEDEFAULT,                      // default horizontal position
  77.         CW_USEDEFAULT,                      // default vertical position
  78.         CW_USEDEFAULT,                      // default width
  79.         CW_USEDEFAULT,                      // default height
  80.         NULL,                               // no parent window
  81.         NULL,                               // use class default menu
  82.         hInstance,                          // window owner
  83.         NULL                                // unused pointer
  84.     );
  85.  
  86.     if (!hWnd) return (FALSE);              // error, can't create window
  87.  
  88.     ShowWindow(hWnd, nCmdShow);             // make window visible
  89.     UpdateWindow(hWnd);                     // force WM_PAINT message
  90.     return (TRUE);                          // return success flag
  91. }
  92.  
  93. //
  94. // MainWndProc - callback function for main application window
  95. //
  96. long FAR PASCAL MainWndProc(HWND hWnd, WORD wMsg, WORD wParam, LONG lParam)
  97. {
  98.     HDC hdc;                                // device context handle
  99.     PAINTSTRUCT ps;                         // painting info structure
  100.     RECT rect;                              // receives client rectangle
  101.  
  102.     switch (wMsg) 
  103.     {
  104.         case WM_COMMAND:                    // menu command received,
  105.             DoCommand(hWnd, wParam);
  106.             break;
  107.  
  108.         case WM_PAINT:                      // window needs repainting
  109.             hdc = BeginPaint(hWnd, &ps);    // get device context
  110.             GetClientRect(hWnd, &rect);     // get client rectangle
  111.             wsprintf(buf2, "You entered: %s", (LPSTR) buf1);
  112.             DrawText(hdc, buf2, -1, &rect,  // display formatted text
  113.                 DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  114.             EndPaint(hWnd, &ps);            // release device context
  115.             break;
  116.  
  117.         case WM_SIZE:                       // window resized
  118.             InvalidateRect(hWnd, NULL, TRUE);   // repaint everything
  119.             UpdateWindow(hWnd);             // force WM_PAINT message
  120.             break;
  121.  
  122.         case WM_DESTROY:                    // window being destroyed
  123.             PostQuitMessage(0);
  124.             break;
  125.  
  126.         default:                            // let Windows handle it
  127.             return(DefWindowProc(hWnd, wMsg, wParam, lParam));
  128.     }
  129.  
  130.     return(0);
  131. }
  132.  
  133. //
  134. // DoCommand - handles menu command messages
  135. //
  136. void DoCommand(HWND hWnd, WORD wParam)
  137. {
  138.     FARPROC lpProc;                         // far pointer to callback 
  139.  
  140.     switch(wParam)                          // decode it
  141.     {
  142.         case IDM_EXIT:                      // user picked File-Quit
  143.             SendMessage (hWnd, WM_CLOSE, 0, 0L);
  144.             break;
  145.  
  146.         case IDM_ENTER:                     // user picked Edit-Enter
  147.             lpProc = MakeProcInstance(EntryDlgProc, hInst);
  148.             DialogBox(hInst, "TextEntryBox", hWnd, lpProc);
  149.             InvalidateRect(hWnd, NULL, TRUE);   // repaint everything
  150.             UpdateWindow(hWnd);             // force WM_PAINT message
  151.             FreeProcInstance(lpProc);
  152.             break;
  153.  
  154.         default:                            // unknown command, ignore it
  155.             break;
  156.     }
  157. }
  158.  
  159. //
  160. // EntryDlgProc - callback function for "Text Entry..." dialog
  161. //
  162. BOOL FAR PASCAL EntryDlgProc(HWND hDlg, WORD wMsg, WORD wParam, LONG lParam)
  163. {
  164.     switch (wMsg) 
  165.     {
  166.         case WM_INITDIALOG:                 // dialog box initialization
  167.             return (TRUE);                  // signal message processed
  168.  
  169.         case WM_COMMAND:                    // command received
  170.             if(wParam == IDOK)              // OK button clicked?
  171.             {                               // yes, retrieve text
  172.                 GetDlgItemText(hDlg, IDD_EDITTEXT, buf1, INPUTSIZE-1);
  173.                 EndDialog(hDlg, TRUE);      // signal text accepted
  174.             }
  175.             else if(wParam == IDD_CANCEL)   // Cancel button clicked?
  176.             {
  177.                 strcpy(buf1, "Input was cancelled!");
  178.                 EndDialog(hDlg, FALSE);     // signal text not accepted
  179.             }
  180.     }
  181.     return(FALSE);                          // signal message not processed
  182. }
  183.