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