Annotation of mstools/samples/cliptext/cliptext.c, revision 1.1

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

unix.superglobalmegacorp.com

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