none
copy screen to clipboard

    Question

  • In my project I have a printout, no problem

    all of the output is text, so is there any way to copy the entire enchilada to the clipboard without using a matching container for the output?

     

     


    My MVP is for the Windows Desktop Experience, i.e. Windows XP, Vista and Windows 7 IT

    Remote Assistance is available for a fee.

    I am best with C++ and I am learning C# using Visual Studio 2010. My page on Video Card Problems is now my most popular landing page.

    Developer | Windows IT | Chess | Economics | Hardcore Games | Vegan Advocate | PC Reviews


    Monday, June 20, 2011 3:58 PM

Answers

All replies

  • MSDN explains the process adequately.  In particular pay attention to the EditCopy function example which places text in the clipboard in the CF_TEXT format, which sounds like what you want.

    All you need beyond that is the actual text to put into the clipboard

    Monday, June 20, 2011 8:22 PM
  • I have some code frtom MSDN now to put text on the clipboard, same page you have

    BOOL WINAPI EditCopy(VOID) 
    { 
      PLABELBOX pbox; 
      LPTSTR lptstrCopy; 
      HGLOBAL hglbCopy; 
      int ich1, ich2, cch; 
     
      if (hwndSelected == NULL) 
        return FALSE; 
     
      // Open the clipboard, and empty it. 
     
      if (!OpenClipboard(hwndMain)) 
        return FALSE; 
      EmptyClipboard(); 
     
      // Get a pointer to the structure for the selected label. 
     
      pbox = (PLABELBOX) GetWindowLong(hwndSelected, 0); 
     
      // If text is selected, copy it using the CF_TEXT format. 
     
      if (pbox->fEdit) 
      { 
        if (pbox->ichSel == pbox->ichCaret)   // zero length
        {  
          CloseClipboard();          // selection 
          return FALSE; 
        } 
     
        if (pbox->ichSel < pbox->ichCaret) 
        { 
          ich1 = pbox->ichSel; 
          ich2 = pbox->ichCaret; 
        } 
        else 
        { 
          ich1 = pbox->ichCaret; 
          ich2 = pbox->ichSel; 
        } 
        cch = ich2 - ich1; 
     
        // Allocate a global memory object for the text. 
     
        hglbCopy = GlobalAlloc(GMEM_MOVEABLE, 
          (cch + 1) * sizeof(TCHAR)); 
        if (hglbCopy == NULL) 
        { 
          CloseClipboard(); 
          return FALSE; 
        } 
     
        // Lock the handle and copy the text to the buffer. 
     
        lptstrCopy = GlobalLock(hglbCopy); 
        memcpy(lptstrCopy, &pbox->atchLabel[ich1], 
          cch * sizeof(TCHAR)); 
        lptstrCopy[cch] = (TCHAR) 0;  // null character 
        GlobalUnlock(hglbCopy); 
     
        // Place the handle on the clipboard. 
     
        SetClipboardData(CF_TEXT, hglbCopy); 
      } 
     
      // If no text is selected, the label as a whole is copied. 
     
      else 
      { 
        // Save a copy of the selected label as a local memory 
        // object. This copy is used to render data on request. 
        // It is freed in response to the WM_DESTROYCLIPBOARD 
        // message. 
     
        pboxLocalClip = (PLABELBOX) LocalAlloc( 
          LMEM_FIXED, 
          sizeof(LABELBOX) 
        ); 
        if (pboxLocalClip == NULL) 
        { 
          CloseClipboard(); 
          return FALSE; 
        } 
        memcpy(pboxLocalClip, pbox, sizeof(LABELBOX)); 
        pboxLocalClip->fSelected = FALSE; 
        pboxLocalClip->fEdit = FALSE; 
     
        // Place a registered clipboard format, the owner-display 
        // format, and the CF_TEXT format on the clipboard using 
        // delayed rendering. 
     
        SetClipboardData(uLabelFormat, NULL); 
        SetClipboardData(CF_OWNERDISPLAY, NULL); 
        SetClipboardData(CF_TEXT, NULL); 
      } 
     
      // Close the clipboard. 
     
      CloseClipboard(); 
     
      return TRUE; 
    }
    

     


    Is this all I need to bolt on to support ctrl-c as show on a menu now?

    I have a handler now, so I can send it to that code easy

     

     

     


    My MVP is for the Windows Desktop Experience, i.e. Windows XP, Vista and Windows 7 IT

    Remote Assistance is available for a fee.

    I am best with C++ and I am learning C# using Visual Studio 2010. My page on Video Card Problems is now my most popular landing page.

    Developer | Windows IT | Chess | Economics | Hardcore Games | Vegan Advocate | PC Reviews


    Monday, June 20, 2011 8:30 PM
  • I should also mention that there is only a main window and no special features outside TextOut stuffing it with data results

     


    My MVP is for the Windows Desktop Experience, i.e. Windows XP, Vista and Windows 7 IT

    Remote Assistance is available for a fee.

    I am best with C++ and I am learning C# using Visual Studio 2010. My page on Video Card Problems is now my most popular landing page.

    Developer | Windows IT | Chess | Economics | Hardcore Games | Vegan Advocate | PC Reviews

    Monday, June 20, 2011 8:35 PM
  • Mostly you just have to hook up Ctrl-C (or whatever method you want to get it into the clipboard)

    I imagine you would want to replace most of what is in the big if/else statement with your own code, since most of that deals with getting the selection and/or the text from an edit box, which doesn't apply in your case.

    I would probably scrap the else clause, keep the if clause, but substitute code to copy whatever text you printed with TextOut into the memory allocated with GlobalAlloc/GlobalLock.

    Monday, June 20, 2011 10:00 PM
  • Will that code copy the entire text even if its 10,000 lines of text?

     


    My MVP is for the Windows Desktop Experience, i.e. Windows XP, Vista and Windows 7 IT

    Remote Assistance is available for a fee. Visit my IT site for information.

    I am best with C++ and I am learning C# using Visual Studio 2010. My page on Video Card Problems is now my most popular landing page.

    Developer | Windows IT | Chess | Economics | Hardcore Games | Vegan Advocate | PC Reviews

    Monday, June 20, 2011 10:08 PM
  • lots of errors

    had to add 20 lines to #define the PLABELBOX alone

     

    seenms VOID is not the right calling convention as it want HWND etc

     

     


    My MVP is for the Windows Desktop Experience, i.e. Windows XP, Vista and Windows 7 IT

    Remote Assistance is available for a fee. Visit my IT site for information.

    I am best with C++ and I am learning C# using Visual Studio 2010. My page on Video Card Problems is now my most popular landing page.

    Developer | Windows IT | Chess | Economics | Hardcore Games | Vegan Advocate | PC Reviews

    Monday, June 20, 2011 11:49 PM
  • #define BOX_ELLIPSE 0
    #define BOX_RECT   1
    #define CCH_MAXLABEL 80
    #define CX_MARGIN  12
    
    typedef struct tagLABELBOX { // box
      RECT rcText;  // coordinates of rectangle containing text
      BOOL fSelected; // TRUE if the label is selected
      BOOL fEdit;   // TRUE if text is selected
      int nType;   // rectangular or elliptical
      int ichCaret;  // caret position
      int ichSel;   // with ichCaret, delimits selection
      int nXCaret;  // window position corresponding to ichCaret
      int nXSel;   // window position corresponding to ichSel
      int cchLabel;  // length of text in atchLabel
      TCHAR atchLabel[CCH_MAXLABEL];
    } LABELBOX, *PLABELBOX;
    


    My MVP is for the Windows Desktop Experience, i.e. Windows XP, Vista and Windows 7 IT

    Remote Assistance is available for a fee. Visit my IT site for information.

    I am best with C++ and I am learning C# using Visual Studio 2010. My page on Video Card Problems is now my most popular landing page.

    Developer | Windows IT | Chess | Economics | Hardcore Games | Vegan Advocate | PC Reviews

    Monday, June 20, 2011 11:52 PM
  • Hi Vegan,

     

    I think you can follow the sample in this document.

    Using the Clipboard, Part I : Transferring Simple Text

     

    Call SetClipboardData(CF_TEXT,hClipboardData);

    The hClipboardData is the handle to the global memory, which contains the text in your label.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 21, 2011 8:34 AM
    Moderator
  • I use the variable hwnd for the application handle

    is that of any use to cut to the cliipboard?

     

     


    My MVP is for the Windows Desktop Experience, i.e. Windows XP, Vista and Windows 7 IT

    Remote Assistance is available for a fee. Visit my IT site for information.

    I am best with C++ and I am learning C# using Visual Studio 2010. My page on Video Card Problems is now my most popular landing page.

    Developer | Windows IT | Chess | Economics | Hardcore Games | Vegan Advocate | PC Reviews

    Tuesday, June 21, 2011 12:52 PM
  • I think you need to find the handle of Edit Control.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 22, 2011 8:53 AM
    Moderator
  • I do not have an edit control. there is no dislog its simply the most basic window available.

    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {
      HWND hWnd;
    
      hInst = hInstance; // Store instance handle in our global variable
    
      hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
    
      if (!hWnd) return FALSE;
    
      ShowWindow(hWnd, nCmdShow);
      UpdateWindow(hWnd);
    
      return TRUE;
    }
    



    My MVP is for the Windows Desktop Experience, i.e. Windows XP, Vista and Windows 7 IT

    Remote Assistance is available for a fee. Visit my IT site for information.

    I am best with C++ and I am learning C# using Visual Studio 2010. My page on Video Card Problems is now my most popular landing page.

    Developer | Windows IT | Chess | Economics | Hardcore Games | Vegan Advocate | PC Reviews

    Wednesday, June 22, 2011 1:43 PM
  • It's fine, where did you put your string into this Window? As far as I know, If there are some Edit controls, we can get the selected strings in these controls. If there is no Edit control, we may get the whole strings in this Window, not the select string.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 23, 2011 6:33 AM
    Moderator
  • 		case WM_PAINT:
    			hdc = BeginPaint(hWnd, &ps);
    			// SetThreadPriority can control thead priority
    			//if(!SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)) {
    			//	DWORD dwError = GetLastError();
    			//	std::cerr << "Failed to enter real-time mode: " << dwError;
    			//	return 1;
    			//}
    			// Display the copyright
    			output.clear();
    			output = _TEXT("Fibonacci 3.0.0 for Windows");
    			// f << name << ": " << time << " seconds" << std::endl << std::endl;
    			TextOut(hdc, 0, curpos, output.c_str(), output.size()); // Display the product name
    			curpos += 2 * rowheight;
    			output.clear();
    			output = _TEXT("(c) 2010-1 583727 BC Ltd, All Rights Reserved.");
    			// f << name << ": " << time << " seconds" << std::endl << std::endl;
    			TextOut(hdc, 0, curpos, output.c_str(), output.size());
    			curpos += rowheight;
    			output.clear();
    			output = _TEXT("Free for peronsal use only");
    			TextOut(hdc, 0, curpos, output.c_str(), output.size());
    			curpos += 2 * rowheight; // skip a line
    			output.clear();
    
    ...
    
    


    My MVP is for the Windows Desktop Experience, i.e. Windows XP, Vista and Windows 7 IT

    Remote Assistance is available for a fee. Visit my IT site for information.

    I am best with C++ and I am learning C# using Visual Studio 2010. My page on Video Card Problems is now my most popular landing page.

    Developer | Windows IT | Chess | Economics | Hardcore Games | Vegan Advocate | PC Reviews

    Thursday, June 23, 2011 12:48 PM
  • I test a project with TextOut function, and I found that the text are printed can not be select, so that they can not be coped by Ctrl+C. As I said before, we need to know which text is selected, after we get these text, we can call the SetClipboardData function.

     

    However, we meet problem now is how to get the Text which customer want to copy. Customer can not select these text, if we use TextOut function. If we do not know which text are selected, we cannot use  SetClipboardData directly.

     

    I hope my suggestions can help you to solve this problem.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, June 24, 2011 4:54 AM
    Moderator
  • I am thinking about a shadown outstring to accumulate a prinout that can be pasted into notepad or other editor

    Sot sur ewht notepad is doing to make the text selectable etc

     


    My MVP is for the Windows Desktop Experience, i.e. Windows XP, Vista and Windows 7 IT

    Remote Assistance is available for a fee. Visit my IT site for information.

    I am best with C++ and I am learning C# using Visual Studio 2010. My page on Video Card Problems is now my most popular landing page.

    Developer | Windows IT | Chess | Economics | Hardcore Games | Vegan Advocate | PC Reviews

    Friday, June 24, 2011 1:32 PM
  • I think you can create a Edit Control in your application.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 27, 2011 2:42 AM
    Moderator
  • How would I bolt on an edit control?

     

    Right now I am using a tstring object to contain the results so I can print to a file and copy to the clipboard as text, clumsey but not not sure what else I can do.

     

    How would I change my windows to and edit control?

     


    My MVP is for the Windows Desktop Experience, i.e. Windows XP, Vista and Windows 7 IT

    Remote Assistance is available for a fee. Visit my IT site for information.

    I am best with C++ and I am learning C# using Visual Studio 2010. My page on Video Card Problems is now my most popular landing page.

    Developer | Windows IT | Chess | Economics | Hardcore Games | Vegan Advocate | PC Reviews

    Monday, June 27, 2011 2:47 AM
  • Please follow this document to create a Multiline Edit Control in native C++

     

    How to Create a Multiline Edit Control

     

    I hope my suggestions can help you to solve this problem.

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, June 28, 2011 3:13 AM
    Moderator