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