LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Copy & Paste with RTF?

I'm currently using a Microsoft RichTextbox control v6.0 in my CVI 7.1 app. It's name is RICHTX32. I'm running into a problem cutting and pasting using CVI's default clipboard commands.

Here's the code I use for a simple copy-selection-to-clipboard...

    char *temp;
    RichTextLib_IRichTextGetSelRTF (m_rtf_handle, NULL, &temp);
    ClipboardPutText (temp);
    CA_FreeMemory(temp);

The problem is that "ClipboardPutText (temp)" puts the entire selection AND all the metadata onto the clipboard as text.

Here's exactly what gets copied when I try to copy **FAILED**

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Lucida Console;}}
{\colortbl ;\red255\green0\blue0;}
\uc1\pard\cf1\b\f0\fs18 **FAILED**}

What I'd like it to do is to copy the RTF string and paste into Word as RTF rather than a bunch of plain-text metadata.
0 Kudos
Message 1 of 6
(4,555 Views)
Hi, I've duplicated your issue and see the problem, but so far I've found no way around this except to use a CVI textbox and simply copy the plain text to the clipboard.

Is it imperative to use the RichTextBox in this application?
Regards,


Marty H.
National Instruments
0 Kudos
Message 2 of 6
(4,527 Views)
I'm able to get the plain text already by using RichTextLib_IRichTextGetSelText and ClipboardPutText. I was hoping to get the entire RTF data, though.

Do you know if there's something in the Windows API SDK that would help me?
0 Kudos
Message 3 of 6
(4,510 Views)
I have two suggestions for you to paste your text into Word without all the metadata.

If you want to maintain all the formatting, you can paste your text (with the metadata) just like you have been doing, into a plain text editor like Notepad.  Then, instead of saving your file as a .TXT file, save it as .RTF or .DOC.  When you open it with Microsoft Word, all the formatting will be preserved.

If you want to simply paste the plain text without any formatting and without the metadata into Word, then I would recommend writing some code in CVI to parse the metadata out of your "temp" char array, prior to putting the text to the clipboard.  I hope that helps.
Regards,


Marty H.
National Instruments
0 Kudos
Message 4 of 6
(4,506 Views)
The following code should do what you want on Windows. I have not added error checking for simplicity but you really should check for errors. You can find the documentation for all these functions on msdn2.microsoft.com.

#include <windows.h>

void PutRTFInClipboad(char *rtf)
{
    UINT format;
    HANDLE data;
    LPSTR dataStr;
   
    data = GlobalAlloc(GMEM_MOVEABLE, strlen(rtf)+1);
    dataStr = GlobalLock(data);
    strcpy(dataStr, rtf);
    GlobalUnlock(data);
   
    format = RegisterClipboardFormat("Rich Text Format");
    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(format, data);
    CloseClipboard();
}

0 Kudos
Message 5 of 6
(4,494 Views)
That did it. The only thing I needed to add was something to free the heap.
0 Kudos
Message 6 of 6
(4,470 Views)