Annotation of q_a/samples/cliptext/cliptext.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: Cliptext.c
        !            15: 
        !            16:     PURPOSE: Demonstrates copying text to and from the clipboard
        !            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:         OutOfMemory() - displays warning message
        !            26: 
        !            27: ****************************************************************************/
        !            28: 
        !            29: #include "cliptext.h"
        !            30: #include <string.h>
        !            31: 
        !            32: HANDLE hInst;
        !            33: HANDLE hAccTable;
        !            34: HWND   hwnd;
        !            35: 
        !            36: HANDLE hText = NULL;
        !            37: 
        !            38: CHAR szInitialClientAreaText[] = 
        !            39:     "This program demonstrates the use of the Edit menu to copy and "
        !            40:     "paste text to and from the clipboard.  Try using the Copy command " 
        !            41:     "to move this text to the clipboard, and the Paste command to replace "
        !            42:     "this text with data from another application.  \r\n\r\n"
        !            43:     "You might want to try running Notepad and Clipbrd alongside this "
        !            44:     "application so that you can watch the data exchanges take place.  ";
        !            45: 
        !            46: HANDLE hData, hClipData;                            /* handles to clip data  */
        !            47: LPSTR lpData, lpClipData;                           /* pointers to clip data */
        !            48: 
        !            49: /* functions declared here, because of MIPS lack of passing C_DEFINES*/
        !            50: BOOL InitApplication(HANDLE);
        !            51: BOOL InitInstance(HANDLE, INT);
        !            52: LONG APIENTRY MainWndProc(HWND, UINT, UINT, LONG);
        !            53: BOOL APIENTRY About(HWND, UINT, UINT, LONG);
        !            54: VOID OutOfMemory(VOID);
        !            55: 
        !            56: /****************************************************************************
        !            57: 
        !            58:     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
        !            59: 
        !            60:     PURPOSE: calls initialization function, processes message loop
        !            61: 
        !            62: ****************************************************************************/
        !            63: 
        !            64: int APIENTRY WinMain(
        !            65:     HANDLE hInstance,
        !            66:     HANDLE hPrevInstance,
        !            67:     LPSTR lpCmdLine,
        !            68:     int nCmdShow
        !            69:     )
        !            70: {
        !            71:     MSG msg;
        !            72: 
        !            73:     UNREFERENCED_PARAMETER( lpCmdLine );
        !            74: 
        !            75:     if (!hPrevInstance)
        !            76:         if (!InitApplication(hInstance))
        !            77:             return (FALSE);
        !            78: 
        !            79:     if (!InitInstance(hInstance, nCmdShow))
        !            80:         return (FALSE);
        !            81: 
        !            82:     while (GetMessage(&msg, NULL, 0, 0)) {
        !            83: 
        !            84:     /* Only translate message if it is not an accelerator message */
        !            85: 
        !            86:         if (!TranslateAccelerator(hwnd, hAccTable, &msg)) {
        !            87:             TranslateMessage(&msg);
        !            88:             DispatchMessage(&msg); 
        !            89:         }
        !            90:     }
        !            91:     return (msg.wParam);
        !            92: }
        !            93: 
        !            94: 
        !            95: /****************************************************************************
        !            96: 
        !            97:     FUNCTION: InitApplication(HANDLE)
        !            98: 
        !            99:     PURPOSE: Initializes window data and registers window class
        !           100: 
        !           101: ****************************************************************************/
        !           102: 
        !           103: BOOL InitApplication(HANDLE hInstance)
        !           104: {
        !           105:     WNDCLASS  wc;
        !           106: 
        !           107:     wc.style = 0;
        !           108:     wc.lpfnWndProc = (WNDPROC) MainWndProc;
        !           109:     wc.cbClsExtra = 0;
        !           110:     wc.cbWndExtra = 0;
        !           111:     wc.hInstance = hInstance;
        !           112:     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        !           113:     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        !           114:     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
        !           115:     wc.lpszMenuName =  "CliptextMenu";
        !           116:     wc.lpszClassName = "CliptextWClass";
        !           117: 
        !           118:     return (RegisterClass(&wc));
        !           119: }
        !           120: 
        !           121: 
        !           122: /****************************************************************************
        !           123: 
        !           124:     FUNCTION:  InitInstance(HANDLE, int)
        !           125: 
        !           126:     PURPOSE:  Saves instance handle and creates main window
        !           127: 
        !           128: ****************************************************************************/
        !           129: 
        !           130: BOOL InitInstance(
        !           131:     HANDLE          hInstance,
        !           132:     INT             nCmdShow)
        !           133: {
        !           134:     LPSTR           lpszText;
        !           135: 
        !           136:     hInst = hInstance;
        !           137: 
        !           138:     hAccTable = LoadAccelerators(hInst, "ClipTextAcc");
        !           139: 
        !           140:     if (!(hText 
        !           141:           = GlobalAlloc(GMEM_MOVEABLE,(DWORD)sizeof(szInitialClientAreaText)))) {
        !           142:         OutOfMemory();
        !           143:         return (FALSE);
        !           144:     }
        !           145:       
        !           146:     if (!(lpszText = GlobalLock(hText))) {
        !           147:         OutOfMemory();
        !           148:         return (FALSE);
        !           149:     }
        !           150: 
        !           151:     strcpy(lpszText, szInitialClientAreaText);
        !           152:     GlobalUnlock(hText);
        !           153: 
        !           154:     hwnd = CreateWindow(
        !           155:         "CliptextWClass",
        !           156:         "Cliptext Sample Application",
        !           157:         WS_OVERLAPPEDWINDOW,
        !           158:         CW_USEDEFAULT,
        !           159:         CW_USEDEFAULT,
        !           160:         CW_USEDEFAULT,
        !           161:         CW_USEDEFAULT,
        !           162:         NULL,
        !           163:         NULL,
        !           164:         hInstance,
        !           165:         NULL
        !           166:     );
        !           167: 
        !           168:     if (!hwnd)
        !           169:         return (FALSE);
        !           170: 
        !           171:     ShowWindow(hwnd, nCmdShow);
        !           172:     UpdateWindow(hwnd);
        !           173:     return (TRUE);
        !           174: 
        !           175: }
        !           176: 
        !           177: /****************************************************************************
        !           178: 
        !           179:     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
        !           180: 
        !           181:     PURPOSE:  Processes messages
        !           182: 
        !           183:     MESSAGES:
        !           184: 
        !           185:         WM_COMMAND    - message from menu
        !           186:         WM_INITMENU   - initialize menu
        !           187:         WM_PAINT      - update window
        !           188:         WM_DESTROY    - destroy window
        !           189: 
        !           190:     COMMENTS:
        !           191: 
        !           192:         WM_INITMENU - when this message is received, the application checks
        !           193:         to see if there is any text data in the clipboard, and enables or
        !           194:         disables the Paste menu item accordingly.
        !           195: 
        !           196:         Seclecting the Copy menu item will send the text "Hello Windows" to
        !           197:         the clipboard.
        !           198: 
        !           199:         Seclecting the Paste menu item will copy whatever text is in the
        !           200:         clipboard to the application window.
        !           201: 
        !           202: ****************************************************************************/
        !           203: 
        !           204: LONG APIENTRY MainWndProc(HWND hWnd, UINT message, UINT wParam, LONG lParam)
        !           205: {
        !           206:     FARPROC lpProcAbout;
        !           207:     HDC hDC;
        !           208:     PAINTSTRUCT ps;
        !           209:     RECT rectClient;
        !           210:     LPSTR lpszText;
        !           211: 
        !           212:     switch (message) {
        !           213: 
        !           214:         case WM_INITMENU:
        !           215:                 if (wParam == (UINT)GetMenu(hWnd)) {
        !           216:                 if (OpenClipboard(hWnd)) {
        !           217:                     if (IsClipboardFormatAvailable(CF_TEXT)
        !           218:                         || IsClipboardFormatAvailable(CF_OEMTEXT))
        !           219:                         EnableMenuItem((HMENU)wParam, IDM_PASTE, MF_ENABLED);
        !           220:                     else
        !           221:                         EnableMenuItem((HMENU)wParam, IDM_PASTE, MF_GRAYED);
        !           222:                     CloseClipboard();
        !           223:                     return (TRUE);
        !           224:                 }
        !           225:                 else                           /* Clipboard is not available */
        !           226:                     return (FALSE);
        !           227: 
        !           228:             }
        !           229:             return (TRUE);
        !           230: 
        !           231:         case WM_COMMAND:
        !           232:                 switch(LOWORD(wParam)) {
        !           233:                 case IDM_ABOUT:
        !           234:                     lpProcAbout = MakeProcInstance((FARPROC)About, hInst);
        !           235:                     DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
        !           236:                     FreeProcInstance(lpProcAbout);
        !           237:                     break;
        !           238: 
        !           239:                 /* file menu commands */
        !           240: 
        !           241:                 case IDM_NEW:
        !           242:                 case IDM_OPEN:
        !           243:                 case IDM_SAVE:
        !           244:                 case IDM_SAVEAS:
        !           245:                 case IDM_PRINT:
        !           246:                     MessageBox (
        !           247:                           GetFocus ()
        !           248:                         , "Command not implemented."
        !           249:                         , "ClipText Sample Application"
        !           250:                         , MB_ICONASTERISK | MB_OK
        !           251:                     );
        !           252:                     break;  
        !           253: 
        !           254:                 case IDM_EXIT:
        !           255:                     DestroyWindow(hWnd);
        !           256:                     break;
        !           257:     
        !           258:                 /* edit menu commands */
        !           259: 
        !           260:                 case IDM_UNDO:
        !           261:                 case IDM_CLEAR:
        !           262:                     MessageBox (
        !           263:                           GetFocus ()
        !           264:                         , "Command not implemented."
        !           265:                         , "ClipText Sample Application"
        !           266:                         , MB_ICONASTERISK | MB_OK
        !           267:                     );
        !           268:                     break;  
        !           269: 
        !           270:                 case IDM_CUT:
        !           271:                 case IDM_COPY:
        !           272: 
        !           273:                     if (hText != NULL) {
        !           274: 
        !           275:                         /* Allocate memory and copy the string to it */
        !           276: 
        !           277:                         if (!(hData 
        !           278:                              = GlobalAlloc(GMEM_DDESHARE, GlobalSize (hText)))) {
        !           279:                             OutOfMemory();
        !           280:                             return (TRUE);
        !           281:                         }
        !           282:                         if (!(lpData = GlobalLock(hData))) {
        !           283:                             OutOfMemory();
        !           284:                             return (TRUE);
        !           285:                         }
        !           286:                         if (!(lpszText = GlobalLock (hText))) {
        !           287:                             OutOfMemory();
        !           288:                             return (TRUE);
        !           289:                         }
        !           290:                                     strcpy(lpData, lpszText);
        !           291:                         GlobalUnlock(hData);
        !           292:                         GlobalUnlock (hText);
        !           293: 
        !           294:                         /* Clear the current contents of the clipboard, and set
        !           295:                          * the data handle to the new string.
        !           296:                          */
        !           297: 
        !           298:                         if (OpenClipboard(hWnd)) {
        !           299:                             EmptyClipboard();
        !           300:                             SetClipboardData(CF_TEXT, hData);
        !           301:                             CloseClipboard();
        !           302:                         }
        !           303:                         hData = NULL;
        !           304: 
        !           305:                                     if (LOWORD(wParam) == IDM_CUT) {
        !           306:                             GlobalFree (hText);
        !           307:                             hText = NULL;
        !           308:                             EnableMenuItem(GetMenu (hWnd), IDM_CUT, MF_GRAYED);
        !           309:                             EnableMenuItem(GetMenu(hWnd), IDM_COPY, MF_GRAYED);
        !           310:                             InvalidateRect (hWnd, NULL, TRUE);
        !           311:                             UpdateWindow (hWnd);
        !           312:                         }
        !           313:                     }
        !           314: 
        !           315:                     return (TRUE);
        !           316: 
        !           317:                 case IDM_PASTE:
        !           318:                     if (OpenClipboard(hWnd)) {
        !           319: 
        !           320:                         /* get text from the clipboard */
        !           321: 
        !           322:                         if (!(hClipData = GetClipboardData(CF_TEXT))) {
        !           323:                             CloseClipboard();
        !           324:                             break;
        !           325:                         }
        !           326:                         if (hText != NULL) {
        !           327:                             GlobalFree(hText);
        !           328:                         }
        !           329:                         if (!(hText = GlobalAlloc(GMEM_MOVEABLE
        !           330:                                                     , GlobalSize(hClipData)))) {
        !           331:                             OutOfMemory();
        !           332:                             CloseClipboard();
        !           333:                             break;
        !           334:                         }
        !           335:                         if (!(lpClipData = GlobalLock(hClipData))) {
        !           336:                             OutOfMemory();
        !           337:                             CloseClipboard();
        !           338:                             break;
        !           339:                         }
        !           340:                         if (!(lpszText = GlobalLock(hText))) {
        !           341:                             OutOfMemory();
        !           342:                             CloseClipboard();
        !           343:                             break;
        !           344:                         }
        !           345:                         strcpy(lpszText, lpClipData);
        !           346:                         GlobalUnlock(hClipData);
        !           347:                         CloseClipboard();
        !           348:                         GlobalUnlock(hText);
        !           349:                         EnableMenuItem(GetMenu(hWnd), IDM_CUT, MF_ENABLED);
        !           350:                         EnableMenuItem(GetMenu(hWnd), IDM_COPY, MF_ENABLED);
        !           351: 
        !           352:                         /* copy text to the application window */
        !           353: 
        !           354:                         InvalidateRect(hWnd, NULL, TRUE);
        !           355:                         UpdateWindow(hWnd);
        !           356:                         return (TRUE);
        !           357:                     }
        !           358:                     else
        !           359:                         return (FALSE);
        !           360:             }
        !           361:             break;
        !           362: 
        !           363:             case WM_SIZE:
        !           364:                 InvalidateRect(hWnd, NULL, TRUE);
        !           365:                 break;
        !           366: 
        !           367:             case WM_PAINT:
        !           368:             hDC = BeginPaint (hWnd, &ps);
        !           369:             if (hText != NULL) {
        !           370:                 if (!(lpszText = GlobalLock (hText))) {
        !           371:                     OutOfMemory();
        !           372:                 } else {
        !           373:                             GetClientRect (hWnd, &rectClient);
        !           374:                     DrawText (hDC, lpszText, -1, &rectClient
        !           375:                                 , DT_EXTERNALLEADING | DT_NOPREFIX | DT_WORDBREAK);
        !           376:                     GlobalUnlock (hText);
        !           377:                 }
        !           378:             }
        !           379:                 EndPaint (hWnd, &ps);
        !           380:             break;
        !           381: 
        !           382:             case WM_DESTROY:
        !           383:                 PostQuitMessage(0);
        !           384:             break;
        !           385: 
        !           386:             default:
        !           387:                 return (DefWindowProc(hWnd, message, wParam, lParam));
        !           388:     }
        !           389:     return (0);
        !           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 APIENTRY About( HWND hDlg, UINT message, UINT wParam, LONG lParam)
        !           407: {
        !           408:     switch (message) {
        !           409:             case WM_INITDIALOG:
        !           410:                 return (TRUE);
        !           411: 
        !           412:             case WM_COMMAND:
        !           413:                 if (LOWORD(wParam) == IDOK
        !           414:                        || LOWORD(wParam) == IDCANCEL) {
        !           415: 
        !           416:                         EndDialog(hDlg, TRUE);
        !           417:                         return (TRUE);
        !           418:             }
        !           419:                 break;
        !           420:     }
        !           421:     return (FALSE);
        !           422:         UNREFERENCED_PARAMETER(lParam);
        !           423: }
        !           424: 
        !           425: 
        !           426: /****************************************************************************
        !           427: 
        !           428:     FUNCTION: OutOfMemory(void)
        !           429: 
        !           430:     PURPOSE:  Displays warning message
        !           431: 
        !           432: ****************************************************************************/
        !           433: VOID OutOfMemory()
        !           434: {
        !           435:     MessageBox(
        !           436:         GetFocus(),
        !           437:         "Out of Memory",
        !           438:         NULL,
        !           439:         MB_ICONHAND | MB_SYSTEMMODAL);
        !           440:     return;
        !           441: }

unix.superglobalmegacorp.com

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