home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / clipbord / clipdata.c < prev    next >
C/C++ Source or Header  |  1988-08-10  |  4KB  |  121 lines

  1. /*
  2.  *  This program demonstrates the use of GetClipboardData function.
  3.  *  This function retieves data from the clipboard in the format given by
  4.  *  an unsigned short interger value that specifies a data format. The
  5.  *  clipboard must have been previously opened.
  6.  *  
  7.  */
  8.  
  9. #include <windows.h>
  10.  
  11. static char    szAppName[] = "clipdata";
  12. static char     szFuncName[] = "GetClipboardData";
  13.  
  14. LPSTR FAR PASCAL lstrcpy (LPSTR, LPSTR);
  15.  
  16. long    FAR PASCAL WndProc (HANDLE, unsigned, WORD, LONG);
  17.  
  18. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  19. HANDLE    hInstance, hPrevInstance;
  20. LPSTR     lpszCmdLine;
  21. int       cmdShow;
  22.   {
  23.   HWND     hWnd;
  24.   WNDCLASS rClass;
  25.   HMENU    hMenu;
  26.   MSG      msg;
  27.  
  28.   if (!hPrevInstance)
  29.     {
  30.     rClass.style         = CS_HREDRAW | CS_VREDRAW;
  31.     rClass.lpfnWndProc   = WndProc;
  32.     rClass.cbClsExtra    = 0;
  33.     rClass.cbWndExtra    = 0;
  34.     rClass.hInstance     = hInstance;
  35.     rClass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  36.     rClass.hIcon         = LoadIcon (hInstance, IDI_APPLICATION);
  37.     rClass.hbrBackground = GetStockObject (WHITE_BRUSH);
  38.     rClass.lpszMenuName  = (LPSTR) NULL;
  39.     rClass.lpszClassName = (LPSTR) szAppName;
  40.  
  41.     if (!RegisterClass (&rClass))
  42.       return FALSE;
  43.     }
  44.  
  45.   hMenu = CreateMenu ();
  46.   ChangeMenu (hMenu, NULL, (LPSTR)"Read From Clipboard", 100, MF_APPEND);
  47.  
  48.   hWnd = CreateWindow (szAppName,            /* window class name       */
  49.                       szFuncName,            /* window caption          */
  50.                       WS_OVERLAPPEDWINDOW,   /* window style            */
  51.                       CW_USEDEFAULT,         /* initial x position      */
  52.                       0,                     /* initial y position      */
  53.                       CW_USEDEFAULT,         /* initial x size          */
  54.                       0,                     /* initial y size          */
  55.                       NULL,                  /* parent window handle    */
  56.                       hMenu,                 /* window menu handle      */
  57.                       hInstance,             /* program instance handle */
  58.                       NULL);                 /* create parameters       */
  59.  
  60.   ShowWindow (hWnd, cmdShow);
  61.   UpdateWindow (hWnd);
  62.  
  63.   while (GetMessage (&msg, NULL, 0, 0))
  64.     {
  65.     TranslateMessage (&msg);
  66.     DispatchMessage (&msg);
  67.     }
  68.   return (msg.wParam);
  69.   }
  70.  
  71. long    FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  72. HWND     hWnd;
  73. unsigned    iMessage;
  74. WORD     wParam;
  75. LONG     lParam;
  76.   {
  77.   HMENU  hMenu;
  78.   HANDLE hClipData;
  79.   HANDLE hMem;
  80.   LPSTR  lpText;
  81.   char    szBuffer[80];
  82.  
  83.   switch (iMessage)
  84.     {
  85.     case WM_CREATE:
  86.       OpenClipboard (hWnd);
  87.       hMem = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT,
  88.                          (long) strlen (szFuncName));
  89.       lpText = (LPSTR) GlobalLock (hMem);
  90.       lstrcpy (lpText, (LPSTR) szFuncName);
  91.       SetClipboardData (CF_TEXT, hMem);
  92.       GlobalUnlock (hMem);
  93.       CloseClipboard ();
  94.  
  95.     case WM_COMMAND:
  96.       if (wParam == 100)
  97.         {
  98.         OpenClipboard (hWnd);
  99.         MessageBox (hWnd, (LPSTR)"Getting clipboard data", (LPSTR)szFuncName,
  100.             MB_OK);
  101.         hClipData = GetClipboardData (CF_TEXT);
  102.         if (hClipData != NULL)
  103.           MessageBox (hWnd, (LPSTR)"Data Received", (LPSTR)szFuncName, MB_OK);
  104.         else
  105.           MessageBox (hWnd, (LPSTR)"There is no data", (LPSTR)szFuncName,
  106.               MB_OK);
  107.         CloseClipboard ();
  108.         }
  109.       break;
  110.  
  111.     case WM_DESTROY:
  112.       GlobalFree (hMem);        /*  Free up the memory  */
  113.       PostQuitMessage (0);
  114.       break;
  115.  
  116.     default:
  117.       return (DefWindowProc (hWnd, iMessage, wParam, lParam));
  118.     }
  119.   return (0L);
  120.   }
  121.