home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 7.ddi / MWHC.007 / RA < prev    next >
Encoding:
Text File  |  1991-08-28  |  13.3 KB  |  422 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: Output.c
  4.  
  5.     PURPOSE: Output template for Windows applications
  6.  
  7.     FUNCTIONS:
  8.  
  9.     WinMain() - calls initialization function, processes message loop
  10.     InitApplication() - initializes window data and registers window
  11.     InitInstance() - saves instance handle and creates main window
  12.     MainWndProc() - processes messages
  13.     About() - processes messages for "About" dialog box
  14.  
  15. ****************************************************************************/
  16.  
  17. #include "windows.h"
  18. #include "string.h"
  19. #include "output.h"
  20.  
  21. HANDLE hInst;
  22.  
  23. HPEN hDashPen;                                         /* "---" pen handle   */
  24. HPEN hDotPen;                                          /* "..." pen handle   */
  25. HBRUSH hOldBrush;                                      /* old brush handle   */
  26. HBRUSH hRedBrush;                                      /* red brush handle   */
  27. HBRUSH hGreenBrush;                                    /* green brush handle */
  28. HBRUSH hBlueBrush;                                     /* blue brush handle  */
  29.  
  30. /****************************************************************************
  31.  
  32.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  33.  
  34.     PURPOSE: calls initialization function, processes message loop
  35.  
  36. ****************************************************************************/
  37.  
  38. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  39. HANDLE hInstance;
  40. HANDLE hPrevInstance;
  41. LPSTR lpCmdLine;
  42. int nCmdShow;
  43. {
  44.     MSG msg;
  45.  
  46.     if (!hPrevInstance)
  47.     if (!InitApplication(hInstance))
  48.         return (FALSE);
  49.  
  50.     if (!InitInstance(hInstance, nCmdShow))
  51.         return (FALSE);
  52.  
  53.     while (GetMessage(&msg, NULL, NULL, NULL)) {
  54.     TranslateMessage(&msg);
  55.     DispatchMessage(&msg);
  56.         }
  57.     return (msg.wParam);
  58. }
  59.  
  60.  
  61. /****************************************************************************
  62.  
  63.     FUNCTION: InitApplication(HANDLE)
  64.  
  65.     PURPOSE: Initializes window data and registers window class
  66.  
  67. ****************************************************************************/
  68.  
  69. BOOL InitApplication(hInstance)
  70. HANDLE hInstance;
  71. {
  72.     WNDCLASS  wc;
  73.  
  74.     wc.style = NULL;
  75.     wc.lpfnWndProc = MainWndProc;
  76.     wc.cbClsExtra = 0;
  77.     wc.cbWndExtra = 0;
  78.     wc.hInstance = hInstance;
  79.     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  80.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  81.     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
  82.     wc.lpszMenuName =  "OutputMenu";
  83.     wc.lpszClassName = "OutputWClass";
  84.  
  85.     return (RegisterClass(&wc));
  86. }
  87.  
  88.  
  89. /****************************************************************************
  90.  
  91.     FUNCTION:  InitInstance(HANDLE, int)
  92.  
  93.     PURPOSE:  Saves instance handle and creates main window
  94.  
  95. ****************************************************************************/
  96.  
  97. BOOL InitInstance(hInstance, nCmdShow)
  98.     HANDLE          hInstance;
  99.     int             nCmdShow;
  100. {
  101.     HWND            hWnd;
  102.  
  103.     hInst = hInstance;
  104.  
  105.     hWnd = CreateWindow(
  106.         "OutputWClass",
  107.         "Output Sample Application",
  108.         WS_OVERLAPPEDWINDOW,
  109.     0,
  110.     0,
  111.     GetSystemMetrics(SM_CXSCREEN),
  112.     GetSystemMetrics(SM_CYSCREEN),
  113.         NULL,
  114.         NULL,
  115.         hInstance,
  116.         NULL
  117.     );
  118.  
  119.     if (!hWnd)
  120.         return (FALSE);
  121.  
  122.     ShowWindow(hWnd, nCmdShow);
  123.     UpdateWindow(hWnd);
  124.     return (TRUE);
  125.  
  126. }
  127.  
  128. /****************************************************************************
  129.  
  130.     FUNCTION: MainWndProc(HWND, WORD, WORD, LONG)
  131.  
  132.     PURPOSE:  Processes messages
  133.  
  134.     MESSAGES:
  135.  
  136.     WM_COMMAND    - application menu (About dialog box)
  137.     WM_CREATE     - create window and objects
  138.     WM_PAINT      - update window, draw objects
  139.     WM_DESTROY    - destroy window
  140.  
  141.     COMMENTS:
  142.  
  143.     Handles to the objects you will use are obtained when the WM_CREATE
  144.     message is received, and deleted when the WM_DESTROY message is
  145.     received.  The actual drawing is done whenever a WM_PAINT message is
  146.     received.
  147.  
  148. ****************************************************************************/
  149.  
  150. long FAR PASCAL MainWndProc(hWnd, message, wParam, lParam)
  151. HWND hWnd;
  152. WORD message;
  153. WORD wParam;
  154. LONG lParam;
  155. {
  156.     FARPROC lpProcAbout;
  157.  
  158.     HDC hDC;                          /* display-context variable  */
  159.     PAINTSTRUCT ps;                   /* paint structure           */
  160.     RECT rcTextBox;                   /* rectangle around the text */
  161.     HPEN hOldPen;                     /* old pen handle            */
  162.  
  163.     switch (message) {
  164.     case WM_COMMAND:
  165.         if (wParam == IDM_ABOUT) {
  166.         lpProcAbout = MakeProcInstance(About, hInst);
  167.  
  168.         DialogBox(hInst,
  169.             "AboutBox",
  170.             hWnd,
  171.             lpProcAbout);
  172.  
  173.         FreeProcInstance(lpProcAbout);
  174.         break;
  175.         }
  176.         else
  177.         return (DefWindowProc(hWnd, message, wParam, lParam));
  178.  
  179.         case WM_CREATE:
  180.             /* Create the brush objects */
  181.  
  182.             hRedBrush =   CreateSolidBrush(RGB(255,   0,   0));
  183.             hGreenBrush = CreateSolidBrush(RGB(  0, 255,   0));
  184.             hBlueBrush =  CreateSolidBrush(RGB(  0,   0, 255));
  185.  
  186.             /* Create the "---" pen */
  187.  
  188.             hDashPen = CreatePen(PS_DASH,                /* style */
  189.                 1,                                       /* width */
  190.                 RGB(0, 0, 0));                           /* color */
  191.  
  192.             /* Create the "..." pen */
  193.  
  194.             hDotPen = CreatePen(2,                       /* style */
  195.                 1,                                       /* width */
  196.                 RGB(0, 0, 0));                           /* color */
  197.             break;
  198.  
  199.         case WM_PAINT:
  200.             {
  201.         TEXTMETRIC textmetric;
  202.         int nDrawX;
  203.         int nDrawY;
  204.         char szText[300];
  205.  
  206.         /* Set up a display context to begin painting */
  207.  
  208.                 hDC = BeginPaint (hWnd, &ps);
  209.  
  210.                 /* Get the size characteristics of the current font.  */
  211.                 /* This information will be used for determining the  */
  212.                 /* vertical spacing of text on the screen.            */
  213.  
  214.                 GetTextMetrics (hDC, &textmetric);
  215.  
  216.                 /* Initialize drawing position to 1/4 inch from the top  */
  217.                 /* and from the left of the top, left corner of the      */
  218.                 /* client area of the main windows.                      */
  219.  
  220.                 nDrawX = GetDeviceCaps (hDC, LOGPIXELSX) / 4;   /* 1/4 inch */
  221.                 nDrawY = GetDeviceCaps (hDC, LOGPIXELSY) / 4;   /* 1/4 inch */
  222.  
  223.                 /* Send characters to the screen.  After displaying each   */
  224.                 /* line of text, advance the vertical position for the     */
  225.                 /* next line of text.  The pixel distance between the top  */ 
  226.                 /* of each line of text is equal to the standard height of */
  227.                 /* the font characters (tmHeight), plus the standard       */
  228.                 /* amount of spacing (tmExternalLeading) between adjacent  */
  229.                 /* lines.                                                  */
  230.  
  231.                 strcpy (szText, "These characters are being painted using ");
  232.                 TextOut (hDC, nDrawX, nDrawY, szText, strlen (szText));
  233.                 nDrawY += textmetric.tmExternalLeading + textmetric.tmHeight;
  234.  
  235.                 strcpy (szText, "the TextOut() function, which is fast and ");
  236.                 TextOut (hDC, nDrawX, nDrawY, szText, strlen (szText));
  237.                 nDrawY += textmetric.tmExternalLeading + textmetric.tmHeight;
  238.  
  239.                 strcpy (szText, "allows programmer control of placement and ");
  240.                 TextOut (hDC, nDrawX, nDrawY, szText, strlen (szText));
  241.                 nDrawY += textmetric.tmExternalLeading + textmetric.tmHeight;
  242.  
  243.                 strcpy (szText, "formatting details.  However, TextOut() ");
  244.                 TextOut (hDC, nDrawX, nDrawY, szText, strlen (szText));
  245.                 nDrawY += textmetric.tmExternalLeading + textmetric.tmHeight;
  246.  
  247.                 strcpy (szText, "does not provide any automatic formatting.");
  248.                 TextOut (hDC, nDrawX, nDrawY, szText, strlen (szText));
  249.                 nDrawY += textmetric.tmExternalLeading + textmetric.tmHeight;
  250.  
  251.                 /* Put text in a 5-inch by 1-inch rectangle and display it. */
  252.                 /* First define the size of the rectangle around the text   */
  253.  
  254.                 nDrawY += GetDeviceCaps (hDC, LOGPIXELSY) / 4;  /* 1/4 inch */
  255.                 SetRect (
  256.                       &rcTextBox
  257.                     , nDrawX
  258.                     , nDrawY
  259.                     , nDrawX + (5 * GetDeviceCaps (hDC, LOGPIXELSX)) /* 5" */
  260.                     , nDrawY + (1 * GetDeviceCaps (hDC, LOGPIXELSY)) /* 1" */
  261.                 );
  262.  
  263.                 /* Draw the text within the bounds of the above rectangle */
  264.  
  265.                 strcpy (szText, "This text is being displayed with a single "
  266.                             "call to DrawText().  DrawText() isn't as fast "
  267.                             "as TextOut(), and it is somewhat more "
  268.                             "constrained, but it provides numerous optional "
  269.                             "formatting features, such as the centering and "
  270.                             "line breaking used in this example.");
  271.                 DrawText (
  272.                       hDC
  273.                     , szText
  274.                     , strlen (szText)
  275.                     , &rcTextBox
  276.                     , DT_CENTER | DT_EXTERNALLEADING | DT_NOCLIP
  277.                                                 | DT_NOPREFIX | DT_WORDBREAK
  278.                 );
  279.             
  280.                 /*  Paint the next object immediately below the bottom of   */
  281.                 /*  the above rectangle in which the text was drawn.        */
  282.  
  283.                 nDrawY = rcTextBox.bottom;
  284.  
  285.                 /* The (x,y) pixel coordinates of the objects about to be   */
  286.                 /* drawn are below, and to the right of, the current        */
  287.                 /* coordinate (nDrawX,nDrawY).                              */
  288.  
  289.                 /* Draw a red rectangle.. */
  290.  
  291.                 hOldBrush = SelectObject(hDC, hRedBrush);
  292.                 Rectangle (
  293.                       hDC
  294.                     , nDrawX
  295.                     , nDrawY
  296.                     , nDrawX + 50
  297.                     , nDrawY + 30
  298.                 );
  299.  
  300.                 /* Draw a green ellipse */
  301.  
  302.                 SelectObject(hDC, hGreenBrush);
  303.                 Ellipse (
  304.                       hDC
  305.                     , nDrawX + 150
  306.                     , nDrawY
  307.                     , nDrawX + 150 + 50
  308.                     , nDrawY + 30
  309.                 );
  310.  
  311.                 /* Draw a blue pie shape */
  312.  
  313.                 SelectObject(hDC, hBlueBrush);
  314.                 Pie (
  315.                       hDC
  316.                     , nDrawX + 300
  317.                     , nDrawY
  318.                     , nDrawX + 300 + 50
  319.                     , nDrawY + 50
  320.                     , nDrawX + 300 + 50
  321.                     , nDrawY
  322.                     , nDrawX + 300 + 50
  323.                     , nDrawY + 50
  324.                 );
  325.  
  326.                 nDrawY += 50;
  327.  
  328.                 /* Restore the old brush */
  329.  
  330.                 SelectObject(hDC, hOldBrush);
  331.  
  332.                 /* Select a "---" pen, save the old value */
  333.  
  334.                 nDrawY += GetDeviceCaps (hDC, LOGPIXELSY) / 4;  /* 1/4 inch */
  335.                 hOldPen = SelectObject(hDC, hDashPen);
  336.  
  337.                 /* Move to a specified point */
  338.  
  339.                 MoveTo(hDC, nDrawX, nDrawY);
  340.  
  341.                 /* Draw a line */
  342.  
  343.                 LineTo(hDC, nDrawX + 350, nDrawY);
  344.  
  345.                 /* Select a "..." pen */
  346.  
  347.                 SelectObject(hDC, hDotPen);
  348.  
  349.                 /* Draw an arc connecting the line */
  350.  
  351.                 Arc (
  352.                       hDC
  353.                     , nDrawX
  354.                     , nDrawY - 20
  355.                     , nDrawX + 350
  356.                     , nDrawY + 20
  357.                     , nDrawX
  358.                     , nDrawY
  359.                     , nDrawX + 350
  360.                     , nDrawY
  361.                 );
  362.  
  363.                 /* Restore the old pen */
  364.  
  365.                 SelectObject(hDC, hOldPen);
  366.  
  367.                 /* Tell Windows you are done painting */
  368.  
  369.                 EndPaint (hWnd,  &ps);
  370.             }
  371.             break;
  372.  
  373.     case WM_DESTROY:
  374.             DeleteObject(hRedBrush);
  375.             DeleteObject(hGreenBrush);
  376.             DeleteObject(hBlueBrush);
  377.             DeleteObject(hDashPen);
  378.             DeleteObject(hDotPen);
  379.         PostQuitMessage(0);
  380.         break;
  381.  
  382.     default:
  383.         return (DefWindowProc(hWnd, message, wParam, lParam));
  384.     }
  385.     return (NULL);
  386. }
  387.  
  388.  
  389. /****************************************************************************
  390.  
  391.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  392.  
  393.     PURPOSE:  Processes messages for "About" dialog box
  394.  
  395.     MESSAGES:
  396.  
  397.     WM_INITDIALOG - initialize dialog box
  398.     WM_COMMAND    - Input received
  399.  
  400. ****************************************************************************/
  401.  
  402. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  403. HWND hDlg;
  404. unsigned message;
  405. WORD wParam;
  406. LONG lParam;
  407. {
  408.     switch (message) {
  409.     case WM_INITDIALOG:
  410.         return (TRUE);
  411.  
  412.     case WM_COMMAND:
  413.         if (wParam == IDOK
  414.                 || wParam == IDCANCEL) {
  415.         EndDialog(hDlg, TRUE);
  416.         return (TRUE);
  417.         }
  418.         break;
  419.     }
  420.     return (FALSE);
  421. }
  422.