home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winui / cliptext / cliptext.c next >
C/C++ Source or Header  |  1997-10-02  |  14KB  |  439 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright (C) 1993 - 1997 Microsoft Corp.
  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.     HINSTANCE hInstance,
  66.     HINSTANCE 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.     HDC hDC;
  207.     PAINTSTRUCT ps;
  208.     RECT rectClient;
  209.     LPSTR lpszText;
  210.  
  211.     switch (message) {
  212.  
  213.         case WM_INITMENU:
  214.                 if (wParam == (UINT)GetMenu(hWnd)) {
  215.                 if (OpenClipboard(hWnd)) {
  216.                     if (IsClipboardFormatAvailable(CF_TEXT)
  217.                         || IsClipboardFormatAvailable(CF_OEMTEXT))
  218.                         EnableMenuItem((HMENU)wParam, IDM_PASTE, MF_ENABLED);
  219.                     else
  220.                         EnableMenuItem((HMENU)wParam, IDM_PASTE, MF_GRAYED);
  221.                     CloseClipboard();
  222.                     return (TRUE);
  223.                 }
  224.                 else                           /* Clipboard is not available */
  225.                     return (FALSE);
  226.  
  227.             }
  228.             return (TRUE);
  229.  
  230.         case WM_COMMAND:
  231.                 switch(LOWORD(wParam)) {
  232.                 case IDM_ABOUT:
  233.             DialogBox(hInst, "AboutBox", hWnd, About);
  234.                     break;
  235.  
  236.                 /* file menu commands */
  237.  
  238.                 case IDM_NEW:
  239.                 case IDM_OPEN:
  240.                 case IDM_SAVE:
  241.                 case IDM_SAVEAS:
  242.                 case IDM_PRINT:
  243.                     MessageBox (
  244.                           GetFocus ()
  245.                         , "Command not implemented."
  246.                         , "ClipText Sample Application"
  247.                         , MB_ICONASTERISK | MB_OK
  248.                     );
  249.                     break;  
  250.  
  251.                 case IDM_EXIT:
  252.                     DestroyWindow(hWnd);
  253.                     break;
  254.     
  255.                 /* edit menu commands */
  256.  
  257.                 case IDM_UNDO:
  258.                 case IDM_CLEAR:
  259.                     MessageBox (
  260.                           GetFocus ()
  261.                         , "Command not implemented."
  262.                         , "ClipText Sample Application"
  263.                         , MB_ICONASTERISK | MB_OK
  264.                     );
  265.                     break;  
  266.  
  267.                 case IDM_CUT:
  268.                 case IDM_COPY:
  269.  
  270.                     if (hText != NULL) {
  271.  
  272.                         /* Allocate memory and copy the string to it */
  273.  
  274.                         if (!(hData 
  275.                              = GlobalAlloc(GMEM_DDESHARE, GlobalSize (hText)))) {
  276.                             OutOfMemory();
  277.                             return (TRUE);
  278.                         }
  279.                         if (!(lpData = GlobalLock(hData))) {
  280.                             OutOfMemory();
  281.                             return (TRUE);
  282.                         }
  283.                         if (!(lpszText = GlobalLock (hText))) {
  284.                             OutOfMemory();
  285.                             return (TRUE);
  286.                         }
  287.                                     strcpy(lpData, lpszText);
  288.                         GlobalUnlock(hData);
  289.                         GlobalUnlock (hText);
  290.  
  291.                         /* Clear the current contents of the clipboard, and set
  292.                          * the data handle to the new string.
  293.                          */
  294.  
  295.                         if (OpenClipboard(hWnd)) {
  296.                             EmptyClipboard();
  297.                             SetClipboardData(CF_TEXT, hData);
  298.                             CloseClipboard();
  299.                         }
  300.                         hData = NULL;
  301.  
  302.                                     if (LOWORD(wParam) == IDM_CUT) {
  303.                             GlobalFree (hText);
  304.                             hText = NULL;
  305.                             EnableMenuItem(GetMenu (hWnd), IDM_CUT, MF_GRAYED);
  306.                             EnableMenuItem(GetMenu(hWnd), IDM_COPY, MF_GRAYED);
  307.                             InvalidateRect (hWnd, NULL, TRUE);
  308.                             UpdateWindow (hWnd);
  309.                         }
  310.                     }
  311.  
  312.                     return (TRUE);
  313.  
  314.                 case IDM_PASTE:
  315.                     if (OpenClipboard(hWnd)) {
  316.  
  317.                         /* get text from the clipboard */
  318.  
  319.                         if (!(hClipData = GetClipboardData(CF_TEXT))) {
  320.                             CloseClipboard();
  321.                             break;
  322.                         }
  323.                         if (hText != NULL) {
  324.                             GlobalFree(hText);
  325.                         }
  326.                         if (!(hText = GlobalAlloc(GMEM_MOVEABLE
  327.                                                     , GlobalSize(hClipData)))) {
  328.                             OutOfMemory();
  329.                             CloseClipboard();
  330.                             break;
  331.                         }
  332.                         if (!(lpClipData = GlobalLock(hClipData))) {
  333.                             OutOfMemory();
  334.                             CloseClipboard();
  335.                             break;
  336.                         }
  337.                         if (!(lpszText = GlobalLock(hText))) {
  338.                             OutOfMemory();
  339.                             CloseClipboard();
  340.                             break;
  341.                         }
  342.                         strcpy(lpszText, lpClipData);
  343.                         GlobalUnlock(hClipData);
  344.                         CloseClipboard();
  345.                         GlobalUnlock(hText);
  346.                         EnableMenuItem(GetMenu(hWnd), IDM_CUT, MF_ENABLED);
  347.                         EnableMenuItem(GetMenu(hWnd), IDM_COPY, MF_ENABLED);
  348.  
  349.                         /* copy text to the application window */
  350.  
  351.                         InvalidateRect(hWnd, NULL, TRUE);
  352.                         UpdateWindow(hWnd);
  353.                         return (TRUE);
  354.                     }
  355.                     else
  356.                         return (FALSE);
  357.             }
  358.             break;
  359.  
  360.             case WM_SIZE:
  361.                 InvalidateRect(hWnd, NULL, TRUE);
  362.                 break;
  363.  
  364.             case WM_PAINT:
  365.             hDC = BeginPaint (hWnd, &ps);
  366.             if (hText != NULL) {
  367.                 if (!(lpszText = GlobalLock (hText))) {
  368.                     OutOfMemory();
  369.                 } else {
  370.                             GetClientRect (hWnd, &rectClient);
  371.                     DrawText (hDC, lpszText, -1, &rectClient
  372.                                 , DT_EXTERNALLEADING | DT_NOPREFIX | DT_WORDBREAK);
  373.                     GlobalUnlock (hText);
  374.                 }
  375.             }
  376.                 EndPaint (hWnd, &ps);
  377.             break;
  378.  
  379.             case WM_DESTROY:
  380.                 PostQuitMessage(0);
  381.             break;
  382.  
  383.             default:
  384.                 return (DefWindowProc(hWnd, message, wParam, lParam));
  385.     }
  386.     return (0);
  387. }
  388.  
  389.  
  390. /****************************************************************************
  391.  
  392.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  393.  
  394.     PURPOSE:  Processes messages for "About" dialog box
  395.  
  396.     MESSAGES:
  397.  
  398.         WM_INITDIALOG - initialize dialog box
  399.         WM_COMMAND    - Input received
  400.  
  401. ****************************************************************************/
  402.  
  403. BOOL APIENTRY About( HWND hDlg, UINT message, UINT wParam, LONG lParam)
  404. {
  405.     switch (message) {
  406.             case WM_INITDIALOG:
  407.                 return (TRUE);
  408.  
  409.             case WM_COMMAND:
  410.                 if (LOWORD(wParam) == IDOK
  411.                        || LOWORD(wParam) == IDCANCEL) {
  412.  
  413.                         EndDialog(hDlg, TRUE);
  414.                         return (TRUE);
  415.             }
  416.                 break;
  417.     }
  418.     return (FALSE);
  419.         UNREFERENCED_PARAMETER(lParam);
  420. }
  421.  
  422.  
  423. /****************************************************************************
  424.  
  425.     FUNCTION: OutOfMemory(void)
  426.  
  427.     PURPOSE:  Displays warning message
  428.  
  429. ****************************************************************************/
  430. VOID OutOfMemory()
  431. {
  432.     MessageBox(
  433.         GetFocus(),
  434.         "Out of Memory",
  435.         NULL,
  436.         MB_ICONHAND | MB_SYSTEMMODAL);
  437.     return;
  438. }
  439.