Annotation of q_a/samples/output/output.c, revision 1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.