home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / OUTPUT.PAK / OUTPUT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  11.9 KB  |  429 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF 
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE: output.c
  9. //
  10. //  PURPOSE: Show basic text and graphics output
  11. //   
  12. //
  13. //  FUNCTIONS:
  14. //    WndProc    - Processes messages for the main window.
  15. //    InitOutput - Create graphics objects used by DrawShapes
  16. //    FreeOutput - Destroy graphics objects used by DrawShapes
  17. //    DrawOutput - Excercise some basic text and shape drawing api
  18. //    UseTextOut - Use the TextOut function to display text
  19. //    UseDrawText- Use the DrawText function to display text
  20. //    DrawShapes - Use several GDI functions to draw some shapes
  21. //
  22. //   COMMENTS:
  23. //      
  24.  
  25. #include <windows.h>            // required for all Windows applications
  26. #include <windowsx.h>
  27. #ifdef WIN16
  28. #include "win16ext.h"           // required only for win16 applications
  29. #endif
  30. #include "globals.h"            // prototypes specific to this application
  31. #include "resource.h"
  32.  
  33.  
  34. int  UseTextOut(HDC,POINT);
  35. int  UseDrawText(HDC,POINT);
  36. void DrawShapes(HDC,POINT);
  37.  
  38. static HPEN hpenDash;           // "---" pen handle
  39. static HPEN hpenDot;            // "..." pen handle
  40. static HBRUSH hbrushRed;        // red brush handle
  41. static HBRUSH hbrushGreen;      // green brush handle
  42. static HBRUSH hbrushBlue;       // blue brush handle
  43.  
  44.  
  45. //
  46. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  47. //
  48. //  PURPOSE:  Processes messages
  49. //
  50. //  PARAMETERS:
  51. //    hwnd     - window handle
  52. //    uMessage - message number
  53. //    wparam   - additional information (dependant of message number)
  54. //    lparam   - additional information (dependant of message number)
  55. //
  56. //   MESSAGES:
  57. //   
  58. //      WM_COMMAND    - exit command
  59. //      WM_DESTROY    - destroy window
  60. //   
  61. //  RETURN VALUE:
  62. //
  63. //    Depends on the message number.
  64. //
  65. //  COMMENTS:
  66. //
  67. //
  68.  
  69. LRESULT CALLBACK WndProc(HWND hwnd, 
  70.                          UINT uMessage, 
  71.                          WPARAM wparam, 
  72.                          LPARAM lparam)
  73. {
  74.     switch (uMessage)
  75.     {
  76.  
  77.         //
  78.         // **TODO** Add cases here for application messages
  79.         //
  80.  
  81.         case WM_PAINT:
  82.  
  83.             // **OUTPUT** - Do the actual painting
  84.             DrawOutput(hwnd);
  85.             break;
  86.     
  87.         case WM_COMMAND: // message: command from application menu
  88.     
  89.             // Message packing of wparam and lparam have changed for Win32,
  90.             // so use the GET_WM_COMMAND macro to unpack the commnad
  91.     
  92.             switch (GET_WM_COMMAND_ID(wparam,lparam))
  93.             {
  94.     
  95.                 //
  96.                 // **TODO** Add cases here for application
  97.                 //          specific command messages
  98.                 //
  99.     
  100.                 case IDM_EXIT:
  101.                     DestroyWindow (hwnd);
  102.                     break;
  103.     
  104.                
  105.                 default:
  106.                     return(DefWindowProc(hwnd, uMessage, wparam, lparam));
  107.             }
  108.             break;
  109.     
  110.         case WM_DESTROY:  // message: window being destroyed
  111.  
  112.             PostQuitMessage(0);
  113.             break;
  114.     
  115.         default:          // Passes it on if unproccessed
  116.             return(DefWindowProc(hwnd, uMessage, wparam, lparam));
  117.     }
  118.     return (0);
  119. }
  120.     
  121.     
  122. //
  123. //  FUNCTION: InitOutput(void)
  124. //
  125. //  PURPOSE: Create brushes and pens
  126. //
  127. //  PARAMETERS:
  128. //      NONE.
  129. //
  130. //  RETURN VALUE:
  131. //      TRUE  - Success
  132. //      FALSE - Failed, most likely due to memeory allocation problems
  133. //
  134. //  COMMENTS:
  135. //      Initialize all of the static gdi objects that are to be
  136. //      used by paint.
  137. //
  138.  
  139. BOOL InitOutput(void)
  140. {
  141.     // Create the brush objects
  142.  
  143.     hbrushRed   = CreateSolidBrush(RGB(255,   0,   0));
  144.     hbrushGreen = CreateSolidBrush(RGB(  0, 255,   0));
  145.     hbrushBlue  = CreateSolidBrush(RGB(  0,   0, 255));
  146.  
  147.     // Create the "---" pen
  148.  
  149.     hpenDash = CreatePen(PS_DASH,                // style
  150.                          1,                      // width
  151.                          RGB(0, 0, 0));          // color
  152.  
  153.     // Create the "..." pen
  154.  
  155.     hpenDot = CreatePen(PS_DOT,                 // style
  156.                         1,                      // width
  157.                         RGB(0, 0, 0));          // color
  158.  
  159.     // Return success only if all of the above creates succeeded
  160.  
  161.     return !(hpenDash    == NULL &&
  162.              hpenDot     == NULL &&
  163.              hbrushRed   == NULL &&
  164.              hbrushGreen == NULL &&
  165.              hbrushBlue  == NULL);
  166. }
  167.  
  168. //
  169. //  FUNCTION: FreeOutput(void)
  170. //
  171. //  PURPOSE: Frees the brushes and pens
  172. //
  173. //  PARAMETERS:
  174. //      NONE.
  175. //
  176. //  RETURN VALUE:
  177. //      NONE.
  178. //
  179. //  COMMENTS:
  180. //      Deletes the brushes and pens created by InitOutput().
  181. //
  182.  
  183. void FreeOutput(void)
  184. {
  185.     DeleteObject(hbrushRed);
  186.     DeleteObject(hbrushGreen);
  187.     DeleteObject(hbrushBlue);
  188.     DeleteObject(hpenDash);
  189.     DeleteObject(hpenDot);
  190. }
  191.  
  192. //
  193. //  FUNCTION: DrawOutput(HWND)
  194. //
  195. //  PURPOSE: Uses GDI api to draw text and graphics to the window.
  196. //
  197. //  PARAMETERS:
  198. //      hwnd - Handle to the window on which to draw.
  199. //
  200. //  RETURN VALUE:
  201. //      NONE.
  202. //
  203. //  COMMENTS:
  204. //      Within the BeginPaint/EndPaint pair, which is used to get
  205. //      and release a handle to a display context, functions are
  206. //      called to draw text in two different ways and to draw
  207. //      several different kinds of graphical objects.
  208. //
  209.  
  210.  
  211. void DrawOutput(HWND hwnd)
  212. {
  213.     PAINTSTRUCT ps;
  214.     HDC hdc;
  215.     POINT pnt;
  216.     int xQI;        // Quarter inch in x pixels
  217.     int yQI;        // Quarter inch in y pixels
  218.  
  219.     // Set up a display context to begin painting
  220.     hdc = BeginPaint(hwnd, &ps);
  221.  
  222.     // Initialize the x & y quarter inch values
  223.     xQI = GetDeviceCaps(hdc, LOGPIXELSX) / 4;   // 1/4 inch
  224.     yQI = GetDeviceCaps(hdc, LOGPIXELSY) / 4;   // 1/4 inch
  225.  
  226.     // Initialize drawing position to 1/4 inch from the top
  227.     // and from the left of the top, left corner of the
  228.     // client area of the main windows.
  229.  
  230.     pnt.x = xQI;
  231.     pnt.y = yQI;
  232.  
  233.     //  Draw some text using the TextOut function
  234.     pnt.y += UseTextOut(hdc, pnt) + yQI;
  235.  
  236.     // Draw some text using the DrawTextFunction
  237.     pnt.y += UseDrawText(hdc, pnt);
  238.  
  239.     // Draw some graphics object
  240.     DrawShapes(hdc, pnt);
  241.  
  242.     // Tell Windows you are done painting
  243.     EndPaint(hwnd,  &ps);
  244. }
  245.  
  246. //
  247. //  FUNCTION: UseTextOut(HDC, POINT)
  248. //
  249. //  PURPOSE: Uses the TextOut function to draw text to the display context
  250. //
  251. //  PARAMETERS:
  252. //      hdc  - Handle to the display context on which to draw.
  253. //      pnt  - The coordinates for the origin of the TextOut.
  254. //
  255. //  RETURN VALUE:
  256. //      Return - The height of the text drawn
  257. //
  258. //  COMMENTS:
  259. //   
  260. //
  261.  
  262. int UseTextOut(HDC hdc, POINT pnt)
  263. {
  264.     TEXTMETRIC tm;
  265.     char rgchText[80];  // Text buffer
  266.     int  dy;            // Line height
  267.  
  268.     // Get the size characteristics of the current font.
  269.     // This information will be used for determining the
  270.     // vertical spacing of text on the screen.
  271.  
  272.     GetTextMetrics (hdc, &tm);
  273.  
  274.     // Calculate the pixel distance between the top of each pair of
  275.     //  lines of text.  This is equal to the standard height of the
  276.     //  font characters (tmHeight) plus the standard amount of spacing
  277.     //  (tmExternalLeading) between adjacent lines.
  278.  
  279.     dy = tm.tmExternalLeading + tm.tmHeight;
  280.  
  281.     // Send characters to the screen.  After displaying each
  282.     // line of text, advance the vertical position for the
  283.     // next line of text.
  284.  
  285.     lstrcpy(rgchText, "These characters are being painted using ");
  286.     TextOut(hdc, pnt.x, pnt.y, rgchText, lstrlen (rgchText));
  287.     pnt.y += dy;
  288.  
  289.     lstrcpy(rgchText, "the TextOut() function, which is fast and ");
  290.     TextOut(hdc, pnt.x, pnt.y, rgchText, lstrlen (rgchText));
  291.     pnt.y += dy;
  292.  
  293.     lstrcpy(rgchText, "allows programmer control of placement and ");
  294.     TextOut(hdc, pnt.x, pnt.y, rgchText, lstrlen (rgchText));
  295.     pnt.y += dy;
  296.  
  297.     lstrcpy(rgchText, "formatting details.  However, TextOut() ");
  298.     TextOut(hdc, pnt.x, pnt.y, rgchText, lstrlen (rgchText));
  299.     pnt.y += dy;
  300.  
  301.     lstrcpy(rgchText, "does not provide any automatic formatting.");
  302.     TextOut(hdc, pnt.x, pnt.y, rgchText, lstrlen (rgchText));
  303.     pnt.y += dy;
  304.  
  305.     // Return the total height of the text drawn.
  306.     return dy * 5;
  307. }
  308.  
  309. //
  310. //  FUNCTION: UseDrawText(HDC, POINT)
  311. //
  312. //  PURPOSE: Uses the DrawText function to draw text to the display context
  313. //
  314. //  PARAMETERS:
  315. //      hdc - Handle to the display context on which to draw.
  316. //      pnt - The coordinates for the origin of the DrawText
  317. //
  318. //  RETURN VALUE:
  319. //      Return - The height of the text drawn
  320. //
  321. //  COMMENTS:
  322. //      Put text in a 5-inch by 1-inch rectangle and display it.
  323. //
  324.  
  325. int UseDrawText(HDC hdc, POINT pnt)
  326. {
  327.     RECT rc;
  328.     char rgchText[300];
  329.     int  dx;                // Width of the text
  330.     int  dy;                // Height of the text
  331.  
  332.     // First define the size of the rectangle around the text
  333.  
  334.     dx = (5 * GetDeviceCaps(hdc, LOGPIXELSX));  // 5"
  335.     dy = (1 * GetDeviceCaps(hdc, LOGPIXELSY));  // 1"
  336.  
  337.     SetRect(&rc, pnt.x, pnt.y, pnt.x + dx, pnt.y + dy);
  338.  
  339.     // Draw the text within the bounds of the above rectangle
  340.     lstrcpy(rgchText,
  341.             "This text is being displayed with a single call to DrawText().  "
  342.             "DrawText() isn't as fast as TextOut(), and it is somewhat more "
  343.             "constrained, but it provides numerous optional formatting features, "
  344.             "such as the centering and line breaking used in this example.");
  345.  
  346.     DrawText(hdc,
  347.              rgchText,
  348.              lstrlen(rgchText),
  349.              &rc,
  350.              DT_CENTER | DT_EXTERNALLEADING | 
  351.              DT_NOCLIP | DT_NOPREFIX | DT_WORDBREAK);
  352.  
  353.     // Return the height of the text
  354.     return dy;
  355. }
  356.  
  357. //
  358. //  FUNCTION: DrawShapes(HDC, POINT)
  359. //
  360. //  PURPOSE: Draw several shapes using a variety of GDI api and objects
  361. //
  362. //  PARAMETERS:
  363. //
  364. //      hdc - Handle to the display context on which to draw.
  365. //      pnt - The coordinates for the origin of the DrawText
  366. //
  367. //  RETURN VALUE:
  368. //
  369. //      Return - NONE.
  370. //
  371. //  COMMENTS:
  372. //
  373. //
  374. //
  375.  
  376. void DrawShapes(HDC hdc, POINT pnt)
  377. {
  378.     HPEN   hpenOld;         // old pen handle
  379.     HBRUSH hbrushOld;       // old brush handle
  380.  
  381.     // The (x,y) pixel coordinates of the objects about to be
  382.     // drawn are below, and to the right of, the current
  383.     // coordinate (pnt.x,pnt.y).
  384.  
  385.     // Draw a red rectangle.
  386.  
  387.     hbrushOld = SelectObject(hdc, hbrushRed);
  388.     Rectangle(hdc, pnt.x, pnt.y, pnt.x + 50, pnt.y + 30);
  389.  
  390.     // Draw a green ellipse
  391.  
  392.     SelectObject(hdc, hbrushGreen);
  393.     Ellipse(hdc, pnt.x + 150, pnt.y, pnt.x + 150 + 50, pnt.y + 30);
  394.  
  395.     // Draw a blue pie shape
  396.  
  397.     SelectObject(hdc, hbrushBlue);
  398.     Pie(hdc,
  399.         pnt.x + 300, pnt.y, pnt.x + 300 + 50, pnt.y + 50,   // Bounding rect
  400.         pnt.x + 300 + 50, pnt.y,                            // 1st Radial
  401.         pnt.x + 300 + 50, pnt.y + 50);                      // 2nd Radial
  402.     pnt.y += 50;
  403.  
  404.     // Restore the old brush
  405.     SelectObject(hdc, hbrushOld);
  406.  
  407.     // Select a "---" pen, save the old value
  408.     pnt.y += GetDeviceCaps(hdc, LOGPIXELSY) / 4;  // 1/4 inch
  409.     hpenOld = SelectObject(hdc, hpenDash);
  410.  
  411.     // Move to a specified point
  412.     MoveToEx(hdc, pnt.x, pnt.y, NULL );
  413.  
  414.     // Draw a line
  415.     LineTo(hdc, pnt.x + 350, pnt.y);
  416.  
  417.     // Select a "..." pen
  418.     SelectObject(hdc, hpenDot);
  419.  
  420.     // Draw an arc connecting the line
  421.     Arc(hdc,
  422.         pnt.x, pnt.y - 20, pnt.x + 350, pnt.y + 20, // Bounding rectangle
  423.         pnt.x, pnt.y,                               // 1st radial endpoint
  424.         pnt.x + 350, pnt.y);                        // 2st radial endpoint
  425.  
  426.     // Restore the old pen
  427.     SelectObject(hdc, hpenOld);
  428. }
  429.