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