Annotation of mstools/samples/input/input.c, revision 1.1.1.3

1.1.1.3 ! 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: 
1.1       root       12: /****************************************************************************
                     13: 
                     14:     PROGRAM: Input.c
                     15: 
                     16:     PURPOSE: Input template for Windows applications
                     17: 
                     18:     FUNCTIONS:
                     19: 
1.1.1.3 ! root       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
1.1       root       25: 
                     26: ****************************************************************************/
                     27: 
                     28: #include "windows.h"
                     29: #include <string.h>
                     30: #include "input.h"
                     31: 
                     32: HANDLE hInst;
                     33: 
                     34: CHAR MouseText[48];                                   /* mouse state         */
                     35: CHAR ButtonText[48];                                  /* mouse-button state  */
                     36: CHAR KeyboardText[48];                                /* keyboard state      */
                     37: CHAR CharacterText[48];                               /* latest character    */
                     38: CHAR ScrollText[48];                                  /* scroll status       */
                     39: CHAR TimerText[48];                                   /* timer state         */
                     40: RECT rectMouse;
                     41: RECT rectButton;
                     42: RECT rectKeyboard;
                     43: RECT rectCharacter;
                     44: RECT rectScroll;
                     45: RECT rectTimer;
                     46: INT idTimer;                                          /* timer ID            */
1.1.1.3 ! root       47: INT nTimerCount = 0;                                  /* current timer count */
1.1       root       48: 
                     49: /****************************************************************************
                     50: 
                     51:     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
                     52: 
                     53:     PURPOSE: calls initialization function, processes message loop
                     54: 
                     55: ****************************************************************************/
                     56: 
                     57: int APIENTRY WinMain(
                     58:     HANDLE hInstance,
                     59:     HANDLE hPrevInstance,
                     60:     LPSTR lpCmdLine,
                     61:     int nCmdShow
                     62:     )
                     63: {
                     64:     MSG msg;
                     65: 
                     66:     UNREFERENCED_PARAMETER( lpCmdLine );
                     67: 
                     68:     if (!hPrevInstance)
1.1.1.3 ! root       69:         if (!InitApplication(hInstance))
        !            70:             return (FALSE);
1.1       root       71: 
                     72:     if (!InitInstance(hInstance, nCmdShow))
                     73:         return (FALSE);
                     74: 
1.1.1.3 ! root       75:     while (GetMessage(&msg, NULL, 0, 0)) {
        !            76:         TranslateMessage(&msg);
        !            77:         DispatchMessage(&msg);
1.1       root       78:     }
                     79:     return (msg.wParam);
                     80: }
                     81: 
                     82: 
                     83: /****************************************************************************
                     84: 
                     85:     FUNCTION: InitApplication(HANDLE)
                     86: 
                     87:     PURPOSE: Initializes window data and registers window class
                     88: 
                     89: ****************************************************************************/
                     90: 
                     91: BOOL InitApplication(HANDLE hInstance)
                     92: {
                     93:     WNDCLASS  wc;
                     94: 
                     95:     wc.style = CS_DBLCLKS;          /* double-click messages */
                     96:     wc.lpfnWndProc = (WNDPROC) MainWndProc;
                     97:     wc.cbClsExtra = 0;
                     98:     wc.cbWndExtra = 0;
                     99:     wc.hInstance = hInstance;
                    100:     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
                    101:     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
                    102:     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
                    103:     wc.lpszMenuName =  "InputMenu";
                    104:     wc.lpszClassName = "InputWClass";
                    105: 
                    106:     return (RegisterClass(&wc));
                    107: }
                    108: 
                    109: 
                    110: /****************************************************************************
                    111: 
                    112:     FUNCTION:  InitInstance(HANDLE, int)
                    113: 
                    114:     PURPOSE:  Saves instance handle and creates main window
                    115: 
                    116: ****************************************************************************/
                    117: 
                    118: BOOL InitInstance(
                    119:     HANDLE          hInstance,
                    120:     INT             nCmdShow)
                    121: {
                    122:     HWND            hWnd;
                    123:     HDC             hDC;
                    124:     TEXTMETRIC      textmetric;
                    125:     RECT            rect;
                    126:     INT             nLineHeight;
                    127:  
                    128: 
                    129:     hInst = hInstance;
                    130: 
                    131:     hWnd = CreateWindow(
                    132:         "InputWClass",
                    133:         "Input Sample Application",
                    134:         WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,  /* horz & vert scroll bars */
                    135:         CW_USEDEFAULT,
                    136:         CW_USEDEFAULT,
                    137:         CW_USEDEFAULT,
                    138:         CW_USEDEFAULT,
                    139:         NULL,
                    140:         NULL,
                    141:         hInstance,
                    142:         NULL
                    143:     );
                    144: 
                    145:     if (!hWnd)
                    146:         return (FALSE);
                    147: 
                    148: 
                    149:     hDC = GetDC(hWnd);
                    150:     GetTextMetrics(hDC, &textmetric);
                    151:     nLineHeight = textmetric.tmExternalLeading + textmetric.tmHeight;
                    152: 
                    153:     rect.left   = GetDeviceCaps(hDC, LOGPIXELSX) / 4;   /* 1/4 inch */
                    154:     rect.right  = GetDeviceCaps(hDC, HORZRES);
                    155:     rect.top    = GetDeviceCaps(hDC, LOGPIXELSY) / 4;   /* 1/4 inch */
                    156:     ReleaseDC(hWnd, hDC);
                    157:     rect.bottom = rect.top + nLineHeight;
                    158:     rectMouse   = rect;
                    159: 
                    160:     rect.top += nLineHeight;
                    161:     rect.bottom += nLineHeight;
                    162:     rectButton = rect;
                    163: 
                    164:     rect.top += nLineHeight;
                    165:     rect.bottom += nLineHeight;
                    166:     rectKeyboard = rect;
                    167: 
                    168:     rect.top += nLineHeight;
                    169:     rect.bottom += nLineHeight;
                    170:     rectCharacter = rect;
                    171: 
                    172:     rect.top += nLineHeight;
                    173:     rect.bottom += nLineHeight;
                    174:     rectScroll = rect;
                    175: 
                    176:     rect.top += nLineHeight;
                    177:     rect.bottom += nLineHeight;
                    178:     rectTimer = rect;
                    179:     
                    180:     ShowWindow(hWnd, nCmdShow);
                    181:     UpdateWindow(hWnd);
                    182:     return (TRUE);
                    183: 
                    184: }
                    185: 
                    186: /****************************************************************************
                    187: 
                    188:     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
                    189: 
                    190:     PURPOSE:  Processes messages
                    191: 
                    192:     MESSAGES:
                    193: 
1.1.1.3 ! root      194:         WM_COMMAND    - application menu (About dialog box)
        !           195:         WM_CREATE     - create window
1.1       root      196:         WM_MOUSEMOVE  - mouse movement
                    197:         WM_LBUTTONDOWN - left mouse button pressed
                    198:         WM_LBUTTONUP  - left mouse button released
                    199:         WM_LBUTTONDBLCLK - left mouse button double clicked
                    200:         WM_KEYDOWN    - key pressed
                    201:         WM_KEYUP      - key released
                    202:         WM_CHAR       - ASCII character received
                    203:         WM_TIMER      - timer has elapsed
                    204:         WM_HSCROLL    - mouse click in horizontal scroll bar
                    205:         WM_VSCROLL    - mouse click in vertical scroll bar
                    206:         WM_PAINT      - update window, draw objects
                    207:         WM_DESTROY    - destroy window
                    208: 
                    209:     COMMENTS:
                    210: 
                    211:         This demonstrates how input messages are received, and what the
                    212:         additional information is that comes with the message.
                    213: 
                    214: ****************************************************************************/
                    215: 
                    216: LONG APIENTRY MainWndProc(
1.1.1.3 ! root      217:         HWND hWnd,
        !           218:         UINT message,
        !           219:         UINT wParam,
        !           220:         LONG lParam)
1.1       root      221: {
                    222:     FARPROC lpProcAbout;
                    223: 
                    224:     HDC hDC;                         /* display-context variable     */
                    225:     PAINTSTRUCT ps;                  /* paint structure              */
                    226:     CHAR HorzOrVertText[12];
                    227:     CHAR ScrollTypeText[20];
                    228:     RECT rect;
                    229: 
                    230:     switch (message) {
1.1.1.3 ! root      231:         case WM_COMMAND:
        !           232:             if (LOWORD(wParam) == IDM_ABOUT) {
        !           233:                 lpProcAbout = MakeProcInstance((FARPROC)About, hInst);
        !           234: 
        !           235:                 DialogBox(hInst,
        !           236:                     "AboutBox",
        !           237:                     hWnd,
        !           238:                     lpProcAbout);
        !           239: 
        !           240:                 FreeProcInstance(lpProcAbout);
        !           241:                 break;
        !           242:             }
        !           243:             else
        !           244:                 return (DefWindowProc(hWnd, message, wParam, lParam));
1.1       root      245: 
                    246:         case WM_CREATE:
                    247: 
                    248:             /* Set the timer for five-second intervals */
                    249: 
1.1.1.3 ! root      250:             idTimer =  SetTimer(hWnd, 0, 5000, (TIMERPROC)(LPVOID)NULL);
1.1       root      251: 
                    252:             break;
                    253: 
                    254:         case WM_MOUSEMOVE:
                    255:             wsprintf(MouseText, "WM_MOUSEMOVE: %x, %d, %d",
                    256:                 wParam, LOWORD(lParam), HIWORD(lParam));
                    257:             InvalidateRect(hWnd, &rectMouse, TRUE);
                    258:             break;
                    259: 
                    260:         case WM_LBUTTONDOWN:
                    261:             wsprintf(ButtonText, "WM_LBUTTONDOWN: %x, %d, %d",
                    262:                 wParam, LOWORD(lParam), HIWORD(lParam));
                    263:             InvalidateRect(hWnd, &rectButton, TRUE);
                    264:             break;
                    265: 
                    266:         case WM_LBUTTONUP:
                    267:             wsprintf(ButtonText, "WM_LBUTTONUP: %x, %d, %d",
                    268:                 wParam, LOWORD(lParam), HIWORD(lParam));
                    269:             InvalidateRect(hWnd, &rectButton, TRUE);
                    270:             break;
                    271: 
                    272:         case WM_LBUTTONDBLCLK:
                    273:             wsprintf(ButtonText, "WM_LBUTTONDBLCLK: %x, %d, %d",
                    274:                 wParam, LOWORD(lParam), HIWORD(lParam));
                    275:             InvalidateRect(hWnd, &rectButton, TRUE);
                    276:             break;
                    277: 
                    278:         case WM_RBUTTONDOWN:
                    279:             wsprintf(ButtonText, "WM_RBUTTONDOWN: %x, %d, %d",
                    280:                 wParam, LOWORD(lParam), HIWORD(lParam));
                    281:             InvalidateRect(hWnd, &rectButton, TRUE);
                    282:             break;
                    283: 
                    284:         case WM_RBUTTONUP:
                    285:             wsprintf(ButtonText, "WM_RBUTTONUP: %x, %d, %d",
                    286:                 wParam, LOWORD(lParam), HIWORD(lParam));
                    287:             InvalidateRect(hWnd, &rectButton, TRUE);
                    288:             break;
                    289: 
                    290:         case WM_RBUTTONDBLCLK:
                    291:             wsprintf(ButtonText, "WM_RBUTTONDBLCLK: %x, %d, %d",
                    292:                 wParam, LOWORD(lParam), HIWORD(lParam));
                    293:             InvalidateRect(hWnd, &rectButton, TRUE);
                    294:             break;
                    295: 
                    296:         case WM_KEYDOWN:
                    297:             wsprintf(KeyboardText, "WM_KEYDOWN: %x, %x, %x",
                    298:                 wParam, LOWORD(lParam), HIWORD(lParam));
                    299:             InvalidateRect(hWnd, &rectKeyboard, TRUE);
                    300:             break;
                    301: 
                    302:         case WM_KEYUP:
                    303:             wsprintf(KeyboardText, "WM_KEYUP: %x, %x, %x",
                    304:                 wParam, LOWORD(lParam), HIWORD(lParam));
                    305:             InvalidateRect(hWnd, &rectKeyboard, TRUE);
                    306:             break;
                    307: 
                    308:         case WM_CHAR:
                    309:             wsprintf(CharacterText, "WM_CHAR: %c, %x, %x",
                    310:                 wParam, LOWORD(lParam), HIWORD(lParam));
                    311:             InvalidateRect(hWnd, &rectCharacter, TRUE);
                    312:             break;
                    313: 
                    314:         case WM_TIMER:
                    315:             wsprintf(TimerText, "WM_TIMER: %d seconds",
                    316:                 nTimerCount += 5);
                    317:             InvalidateRect(hWnd, &rectTimer, TRUE);
                    318:             break;
                    319: 
                    320:         case WM_HSCROLL:
                    321:         case WM_VSCROLL:
                    322:             strcpy(HorzOrVertText,
                    323:                 (message == WM_HSCROLL) ? "WM_HSCROLL" : "WM_VSCROLL");
                    324:             strcpy(ScrollTypeText,
                    325:                 (GET_WM_HSCROLL_CODE(wParam, lParam) == SB_LINEUP) ? "SB_LINEUP" :
                    326:                 (GET_WM_HSCROLL_CODE(wParam, lParam) == SB_LINEDOWN) ? "SB_LINEDOWN" :
                    327:                 (GET_WM_HSCROLL_CODE(wParam, lParam) == SB_PAGEUP) ? "SB_PAGEUP" :
                    328:                 (GET_WM_HSCROLL_CODE(wParam, lParam) == SB_PAGEDOWN) ? "SB_PAGEDOWN" :
                    329:                 (GET_WM_HSCROLL_CODE(wParam, lParam) == SB_THUMBPOSITION) ? "SB_THUMBPOSITION" :
                    330:                 (GET_WM_HSCROLL_CODE(wParam, lParam) == SB_THUMBTRACK) ? "SB_THUMBTRACK" :
                    331:                 (GET_WM_HSCROLL_CODE(wParam, lParam) == SB_ENDSCROLL) ? "SB_ENDSCROLL" : "unknown");
                    332:             wsprintf(ScrollText, "%s: %s, %x, %x",
                    333:                 (LPSTR)HorzOrVertText,
                    334:                 (LPSTR)ScrollTypeText,
                    335:                 GET_WM_HSCROLL_POS(wParam, lParam),
                    336:                 (INT)GET_WM_HSCROLL_HWND(wParam, lParam));
                    337:             InvalidateRect(hWnd, &rectScroll, TRUE);
                    338:             break;
                    339: 
                    340:         case WM_PAINT:
                    341:             hDC = BeginPaint (hWnd, &ps);
                    342: 
                    343:             if (IntersectRect(&rect, &rectMouse, &ps.rcPaint))
                    344:                 TextOut(hDC, rectMouse.left, rectMouse.top, 
                    345:                         MouseText, strlen(MouseText));
                    346:             if (IntersectRect(&rect, &rectButton, &ps.rcPaint))
                    347:                 TextOut(hDC, rectButton.left, rectButton.top, 
                    348:                         ButtonText, strlen(ButtonText));
                    349:             if (IntersectRect(&rect, &rectKeyboard, &ps.rcPaint))
                    350:                 TextOut(hDC, rectKeyboard.left, rectKeyboard.top, 
                    351:                         KeyboardText, strlen(KeyboardText));
                    352:             if (IntersectRect(&rect, &rectCharacter, &ps.rcPaint))
                    353:                 TextOut(hDC, rectCharacter.left, rectCharacter.top, 
                    354:                         CharacterText, strlen(CharacterText));
                    355:             if (IntersectRect(&rect, &rectTimer, &ps.rcPaint))
                    356:                 TextOut(hDC, rectTimer.left, rectTimer.top, 
                    357:                         TimerText, strlen(TimerText));
                    358:             if (IntersectRect(&rect, &rectScroll, &ps.rcPaint))
                    359:                 TextOut(hDC, rectScroll.left, rectScroll.top, 
                    360:                         ScrollText, strlen(ScrollText));
                    361: 
                    362:             EndPaint(hWnd, &ps);
                    363:             break;
                    364: 
                    365:         case WM_DESTROY:
1.1.1.3 ! root      366:                     /* Bug?, SetTimer returns an int, KillTimer takes a WORD*/
1.1       root      367:             KillTimer(hWnd, (WORD)idTimer);                     /* Stops the timer */
                    368:             PostQuitMessage(0);
                    369:             break;
                    370: 
1.1.1.3 ! root      371:         default:
        !           372:             return (DefWindowProc(hWnd, message, wParam, lParam));
1.1       root      373:     }
1.1.1.3 ! root      374:     return (0);
1.1       root      375: }
                    376: 
                    377: 
                    378: /****************************************************************************
                    379: 
                    380:     FUNCTION: About(HWND, unsigned, WORD, LONG)
                    381: 
                    382:     PURPOSE:  Processes messages for "About" dialog box
                    383: 
                    384:     MESSAGES:
                    385: 
1.1.1.3 ! root      386:         WM_INITDIALOG - initialize dialog box
        !           387:         WM_COMMAND    - Input received
1.1       root      388: 
                    389: ****************************************************************************/
                    390: 
                    391: BOOL APIENTRY About(
1.1.1.3 ! root      392:         HWND hDlg,
        !           393:         UINT message,
        !           394:         UINT wParam,
        !           395:         LONG lParam)
1.1       root      396: {
                    397:     switch (message) {
1.1.1.3 ! root      398:         case WM_INITDIALOG:
        !           399:             return (TRUE);
1.1       root      400: 
1.1.1.3 ! root      401:         case WM_COMMAND:
        !           402:             if (LOWORD(wParam) == IDOK) {
        !           403:                 EndDialog(hDlg, TRUE);
        !           404:                 return (TRUE);
        !           405:             }
        !           406:             break;
1.1       root      407:     }
                    408:     return (FALSE);
1.1.1.3 ! root      409:         UNREFERENCED_PARAMETER(lParam);
1.1       root      410: }

unix.superglobalmegacorp.com

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