clipboard

This is a discussion on clipboard within the C++ Programming forums, part of the General Programming Boards category; im quite a newbie, so forgive my lack of knowledge. is it possible (stupid question, ok how easy is it) ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    11

    clipboard

    im quite a newbie, so forgive my lack of knowledge.

    is it possible (stupid question, ok how easy is it) to write a c++ app that can write a string to the clipboard, and paste it from the clipboardto wherever the cursor happens to be targeted. so for example i could run my app and have it paste the string "hello world!" every second to wherever the cursor is, so if i had notepad running, it would start filling up with the text "hello world!"

    sorry if im asking too general a question. i would *really* apprieciate help tho

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Post #include<windows.h>

    Charles Petzold dedicates an entire chapter on how to use the clipboard in his latest book on win32 api (5th edition). Here is an example program taken from the cd that came with the book. It is a simple notepad type text editor. Pay particular attention to how the program opens and closes the clipboard.. and handles the IDM_EDIT_PASTE, IDM_EDIT_CUT, IDM_EDIT_CLEAR, and IDM_EDIT_RESET messages. Although win32 api is language independant, I have highlighted C specific syntax in blue. Maybe someone can edit this with the C++ equivilant.. (I am not sure of the C++ equivilant for GlobalAlloc( ) for example)

    Code:
    /*-----------------------------------------
       CLIPTEXT.C -- The Clipboard and Text
                     (c) Charles Petzold, 1998
      -----------------------------------------*/
    
    #include <windows.h>
    #include "resource.h"
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    #ifdef UNICODE
    
    #define CF_TCHAR CF_UNICODETEXT
    TCHAR szDefaultText[] = TEXT ("Default Text - Unicode Version") ;
    TCHAR szCaption[]     = TEXT ("Clipboard Text Transfers - Unicode Version") ;
    
    #else
    
    #define CF_TCHAR CF_TEXT
    TCHAR szDefaultText[] = TEXT ("Default Text - ANSI Version") ;
    TCHAR szCaption[]     = TEXT ("Clipboard Text Transfers - ANSI Version") ;
    
    #endif
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         static TCHAR szAppName[] = TEXT ("ClipText") ;
         HACCEL       hAccel ;
         HWND         hwnd ;
         MSG          msg ;
         WNDCLASS     wndclass ;
         
         wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
         wndclass.lpszMenuName  = szAppName ;
         wndclass.lpszClassName = szAppName ;
         
         if (!RegisterClass (&wndclass))
         {
              MessageBox (NULL, TEXT ("This program requires Windows NT!"),
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
         
         hwnd = CreateWindow (szAppName, szCaption,
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              NULL, NULL, hInstance, NULL) ;
         
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
    
         hAccel = LoadAccelerators (hInstance, szAppName) ;
    
         while (GetMessage (&msg, NULL, 0, 0))
         {
              if (!TranslateAccelerator (hwnd, hAccel, &msg))
              {
                   TranslateMessage (&msg) ;
                   DispatchMessage (&msg) ;
              }
         }
         return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
         static PTSTR pText ;
         BOOL         bEnable ;
         HGLOBAL      hGlobal ;
         HDC          hdc ;
         PTSTR        pGlobal ;
         PAINTSTRUCT  ps ;
         RECT         rect ;
         
         switch (message)
         {
         case WM_CREATE:
              SendMessage (hwnd, WM_COMMAND, IDM_EDIT_RESET, 0) ;
              return 0 ;
    
        case WM_INITMENUPOPUP:
              EnableMenuItem ((HMENU) wParam, IDM_EDIT_PASTE,
                   IsClipboardFormatAvailable (CF_TCHAR) ? MF_ENABLED : MF_GRAYED) ;
    
              bEnable = pText ? MF_ENABLED : MF_GRAYED ;
    
              EnableMenuItem ((HMENU) wParam, IDM_EDIT_CUT,   bEnable) ;
              EnableMenuItem ((HMENU) wParam, IDM_EDIT_COPY,  bEnable) ;
              EnableMenuItem ((HMENU) wParam, IDM_EDIT_CLEAR, bEnable) ;
              break ;
              
         case WM_COMMAND:
              switch (LOWORD (wParam))
              {
              case IDM_EDIT_PASTE:
                   OpenClipboard (hwnd) ;
    
                   if (hGlobal = GetClipboardData (CF_TCHAR))
                   {
                        pGlobal = GlobalLock (hGlobal) ;
    
                        if (pText)
                        {
                             //delete pText;
                             free (pText) ;
                             pText = NULL ;
                        }
                        //pText = new PTSTR;
                        pText = malloc (GlobalSize (hGlobal)) ;
                        lstrcpy (pText, pGlobal) ;
                        InvalidateRect (hwnd, NULL, TRUE) ;
                   }
                   CloseClipboard () ;
                   return 0 ;
    
              case IDM_EDIT_CUT:
              case IDM_EDIT_COPY:
                   if (!pText)
                        return 0 ;
    
                   //There might be a c++ equivilant for this
                   hGlobal = GlobalAlloc (GHND | GMEM_SHARE, 
                                         (lstrlen (pText) + 1) * sizeof (TCHAR)) ;
                   pGlobal = GlobalLock (hGlobal) ;
                   lstrcpy (pGlobal, pText) ;
                   GlobalUnlock (hGlobal) ;
    
                   OpenClipboard (hwnd) ;
                   EmptyClipboard () ;
                   SetClipboardData (CF_TCHAR, hGlobal) ;
                   CloseClipboard () ;
    
                   if (LOWORD (wParam) == IDM_EDIT_COPY)
                        return 0 ;        
                   // fall through for IDM_EDIT_CUT
              case IDM_EDIT_CLEAR:
                   if (pText)
                   {
                        //delete pText;
                        free (pText) ;
                        pText = NULL ;
                   }
                   InvalidateRect (hwnd, NULL, TRUE) ;
                   return 0 ;
    
              case IDM_EDIT_RESET:
                   if (pText)
                   {
                        //delete pText;
                        free (pText) ;
                        pText = NULL ;
                   }
    
                   //pText = new PTSTR;
                   pText = malloc ((lstrlen (szDefaultText) + 1) * sizeof (TCHAR)) ;
                   lstrcpy (pText, szDefaultText) ;
                   InvalidateRect (hwnd, NULL, TRUE) ;
                   return 0 ;
              }
              break ;
    
         case WM_PAINT:
              hdc = BeginPaint (hwnd, &ps) ;
    
              GetClientRect (hwnd, &rect) ;
              
              if (pText != NULL)
                   DrawText (hdc, pText, -1, &rect, DT_EXPANDTABS | DT_WORDBREAK) ;
    
              EndPaint (hwnd, &ps) ;
              return 0 ;
              
         case WM_DESTROY:
              if (pText)
                   //delete pText;
                   free (pText) ;
    
              PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    *note: you may need "resource.h" in order to compile this.. this can be provided on request.
    Last edited by The Brain; 08-05-2005 at 09:35 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copying to clipboard
    By bballzone in forum Windows Programming
    Replies: 24
    Last Post: 09-30-2004, 03:24 PM
  2. Clipboard and Custom Types
    By McClamm in forum C# Programming
    Replies: 1
    Last Post: 09-16-2004, 04:43 PM
  3. Clipboard Modifier
    By Korhedron in forum Windows Programming
    Replies: 2
    Last Post: 01-03-2004, 01:32 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. OLE Clipboard :: Win32 API vs. MFC
    By kuphryn in forum Windows Programming
    Replies: 3
    Last Post: 08-11-2002, 05:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21