home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_sdk / cliptext / cliptext.c next >
Encoding:
C/C++ Source or Header  |  1990-06-01  |  12.8 KB  |  430 lines

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