home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 18.ddi / SAMPLES / CLIPTEXT / CLIPTEXT.C_ / CLIPTEXT.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  13.5 KB  |  444 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.  
  54.     if (!hPrevInstance)
  55.     if (!InitApplication(hInstance))
  56.         return (FALSE);
  57.  
  58.     if (!InitInstance(hInstance, nCmdShow))
  59.         return (FALSE);
  60.  
  61.     while (GetMessage(&msg, NULL, NULL, NULL))
  62.     {
  63.         // Only translate message if it is not an accelerator message
  64.  
  65.         if (!TranslateAccelerator(hwnd, hAccTable, &msg))
  66.         {
  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.     {
  124.         OutOfMemory();
  125.         return (FALSE);
  126.     }
  127.  
  128.     if (!(lpszText = GlobalLock(hText)))
  129.     {
  130.         OutOfMemory();
  131.         return (FALSE);
  132.     }
  133.  
  134.     lstrcpy(lpszText, szInitialClientAreaText);
  135.     GlobalUnlock(hText);
  136.  
  137.     hwnd = CreateWindow("CliptextWClass",
  138.                         "Cliptext Sample Application",
  139.                         WS_OVERLAPPEDWINDOW,
  140.                         CW_USEDEFAULT,
  141.                         CW_USEDEFAULT,
  142.                         CW_USEDEFAULT,
  143.                         CW_USEDEFAULT,
  144.                         NULL,
  145.                         NULL,
  146.                         hInstance,
  147.                         NULL
  148.                        );
  149.  
  150.     if (!hwnd)
  151.         return (FALSE);
  152.  
  153.     ShowWindow(hwnd, nCmdShow);
  154.     UpdateWindow(hwnd);
  155.     return (TRUE);
  156.  
  157. }
  158.  
  159. /****************************************************************************
  160.  
  161.     FUNCTION: MainWndProc(HWND, UINT, WPARAM, LPARAM)
  162.  
  163.     PURPOSE:  Processes messages
  164.  
  165.     MESSAGES:
  166.  
  167.         WM_COMMAND    - message from menu
  168.         WM_INITMENU   - initialize menu
  169.         WM_PAINT      - update window
  170.         WM_DESTROY    - destroy window
  171.  
  172.     COMMENTS:
  173.  
  174.         WM_INITMENU - when this message is received, the application checks
  175.         to see if there is any text data in the clipboard, and enables or
  176.         disables the Paste menu item accordingly.
  177.  
  178.         Selecting the Copy menu item will send the text "Hello Windows" to
  179.         the clipboard.
  180.  
  181.         Selecting the Paste menu item will copy whatever text is in the
  182.         clipboard to the application window.
  183.  
  184. ****************************************************************************/
  185.  
  186. long FAR PASCAL __export MainWndProc(hWnd, message, wParam, lParam)
  187. HWND hWnd;
  188. UINT message;
  189. WPARAM wParam;
  190. LPARAM lParam;
  191. {
  192.     FARPROC lpProcAbout;
  193.     HDC hDC;
  194.     PAINTSTRUCT ps;
  195.     RECT rectClient;
  196.     LPSTR lpszText;
  197.  
  198.     switch (message)
  199.     {
  200.         case WM_INITMENU:
  201.             if (wParam == GetMenu(hWnd))
  202.             {
  203.                 if (OpenClipboard(hWnd))
  204.                 {
  205.                     if (IsClipboardFormatAvailable(CF_TEXT)
  206.                         || IsClipboardFormatAvailable(CF_OEMTEXT))
  207.                         EnableMenuItem(wParam, IDM_PASTE, MF_ENABLED);
  208.                     else
  209.                         EnableMenuItem(wParam, IDM_PASTE, MF_GRAYED);
  210.                     CloseClipboard();
  211.                     return (TRUE);
  212.                 }
  213.                 else                    // Clipboard is not available
  214.                     return (FALSE);
  215.  
  216.             }
  217.             return (TRUE);
  218.  
  219.         case WM_COMMAND:
  220.             switch(wParam)
  221.             {
  222.                 case IDM_ABOUT:
  223.                     lpProcAbout = MakeProcInstance(About, hInst);
  224.                     DialogBox(hInst, "AboutBox", hWnd, 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 (GetFocus (),
  236.                                 "Command not implemented.",
  237.                                 "ClipText Sample Application",
  238.                                 MB_ICONASTERISK | MB_OK
  239.                                );
  240.                     break;
  241.  
  242.                 case IDM_EXIT:
  243.                     DestroyWindow(hWnd);
  244.                     break;
  245.  
  246.                 // edit menu commands
  247.  
  248.                 case IDM_UNDO:
  249.                 case IDM_CLEAR:
  250.                     MessageBox (GetFocus (),
  251.                                 "Command not implemented.",
  252.                                 "ClipText Sample Application",
  253.                                 MB_ICONASTERISK | MB_OK
  254.                                );
  255.                     break;
  256.  
  257.                 case IDM_CUT:
  258.                 case IDM_COPY:
  259.  
  260.                     if (hText != NULL)
  261.                     {
  262.  
  263.                         // Allocate memory and copy the string to it
  264.  
  265.                         if (!(hData
  266.                              = GlobalAlloc(GMEM_MOVEABLE, GlobalSize (hText))))
  267.                         {
  268.                             OutOfMemory();
  269.                             return (TRUE);
  270.                         }
  271.                         if (!(lpData = GlobalLock(hData)))
  272.                         {
  273.                             OutOfMemory();
  274.                             return (TRUE);
  275.                         }
  276.                         if (!(lpszText = GlobalLock (hText)))
  277.                         {
  278.                             OutOfMemory();
  279.                             return (TRUE);
  280.                         }
  281.                         lstrcpy(lpData, lpszText);
  282.                         GlobalUnlock(hData);
  283.                         GlobalUnlock (hText);
  284.  
  285.                         // Clear the current contents of the clipboard, and set
  286.                         // the data handle to the new string.
  287.  
  288.                         if (OpenClipboard(hWnd))
  289.                         {
  290.                             EmptyClipboard();
  291.                             SetClipboardData(CF_TEXT, hData);
  292.                             CloseClipboard();
  293.                         }
  294.                         hData = NULL;
  295.  
  296.                         if (wParam == IDM_CUT)
  297.                         {
  298.                             GlobalFree (hText);
  299.                             hText = NULL;
  300.                             EnableMenuItem(GetMenu (hWnd), IDM_CUT, MF_GRAYED);
  301.                             EnableMenuItem(GetMenu(hWnd), IDM_COPY, MF_GRAYED);
  302.                             InvalidateRect (hWnd, NULL, TRUE);
  303.                             UpdateWindow (hWnd);
  304.                         }
  305.                     }
  306.  
  307.                     return (TRUE);
  308.  
  309.                 case IDM_PASTE:
  310.                     if (OpenClipboard(hWnd))
  311.                     {
  312.                         // get text from the clipboard
  313.  
  314.                         if (!(hClipData = GetClipboardData(CF_TEXT)))
  315.                         {
  316.                             CloseClipboard();
  317.                             break;
  318.                         }
  319.                         if (hText != NULL)
  320.                             GlobalFree(hText);
  321.                         if (!(hText = GlobalAlloc(GMEM_MOVEABLE,
  322.                                                   GlobalSize(hClipData))))
  323.                         {
  324.                             OutOfMemory();
  325.                             CloseClipboard();
  326.                             break;
  327.                         }
  328.                         if (!(lpClipData = GlobalLock(hClipData)))
  329.                         {
  330.                             OutOfMemory();
  331.                             CloseClipboard();
  332.                             break;
  333.                         }
  334.                         if (!(lpszText = GlobalLock(hText)))
  335.                         {
  336.                             OutOfMemory();
  337.                             CloseClipboard();
  338.                             break;
  339.                         }
  340.                         lstrcpy(lpszText, lpClipData);
  341.                         GlobalUnlock(hClipData);
  342.                         CloseClipboard();
  343.                         GlobalUnlock(hText);
  344.                         EnableMenuItem(GetMenu(hWnd), IDM_CUT, MF_ENABLED);
  345.                         EnableMenuItem(GetMenu(hWnd), IDM_COPY, MF_ENABLED);
  346.  
  347.                         // copy text to the application window
  348.  
  349.                         InvalidateRect(hWnd, NULL, TRUE);
  350.                         UpdateWindow(hWnd);
  351.                         return (TRUE);
  352.                     }
  353.                     else
  354.                         return (FALSE);
  355.             }
  356.             break;
  357.  
  358.         case WM_SIZE:
  359.             InvalidateRect(hWnd, NULL, TRUE);
  360.             break;
  361.  
  362.         case WM_PAINT:
  363.             hDC = BeginPaint (hWnd, &ps);
  364.             if (hText != NULL)
  365.             {
  366.                 if (!(lpszText = GlobalLock (hText)))
  367.                 {
  368.                     OutOfMemory();
  369.                 }
  370.                 else
  371.                 {
  372.                     GetClientRect (hWnd, &rectClient);
  373.                     DrawText (hDC, lpszText, -1, &rectClient,
  374.                               DT_EXTERNALLEADING | DT_NOPREFIX | DT_WORDBREAK);
  375.                     GlobalUnlock (hText);
  376.                 }
  377.             }
  378.             EndPaint (hWnd, &ps);
  379.             break;
  380.  
  381.         case WM_DESTROY:
  382.             PostQuitMessage(0);
  383.             break;
  384.  
  385.         default:
  386.             return (DefWindowProc(hWnd, message, wParam, lParam));
  387.     }
  388.     return (NULL);
  389. }
  390.  
  391.  
  392. /****************************************************************************
  393.  
  394.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  395.  
  396.     PURPOSE:  Processes messages for "About" dialog box
  397.  
  398.     MESSAGES:
  399.  
  400.     WM_INITDIALOG - initialize dialog box
  401.     WM_COMMAND    - Input received
  402.  
  403. ****************************************************************************/
  404.  
  405. BOOL FAR PASCAL __export About(hDlg, message, wParam, lParam)
  406. HWND hDlg;
  407. unsigned message;
  408. WORD wParam;
  409. LONG lParam;
  410. {
  411.     switch (message)
  412.     {
  413.         case WM_INITDIALOG:
  414.             return (TRUE);
  415.  
  416.         case WM_COMMAND:
  417.             if (wParam == IDOK || wParam == IDCANCEL)
  418.             {
  419.                 EndDialog(hDlg, TRUE);
  420.                 return (TRUE);
  421.             }
  422.             break;
  423.     }
  424.     return (FALSE);
  425. }
  426.  
  427.  
  428. /****************************************************************************
  429.  
  430.     FUNCTION: OutOfMemory(void)
  431.  
  432.     PURPOSE:  Displays warning message
  433.  
  434. ****************************************************************************/
  435. void OutOfMemory(void)
  436. {
  437.     MessageBox(GetFocus(),
  438.                "Out of Memory",
  439.                NULL,
  440.                MB_ICONHAND | MB_SYSTEMMODAL
  441.               );
  442.     return;
  443. }
  444.