Click here to Skip to main content
16,020,381 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Nowadays, when copying text to the clipboard, is it any better to SetClipboardData as CF_UNICODETEXT instead of CF_TEXT ? Will it make a difference for the pasting entity ?

Any argument ?
Posted

Sorry, I'm only know a little English.
I just read the title.

MSDN:
The system implicitly converts data between certain clipboard formats: if a window requests data in a format that is not on the clipboard, the system converts an available format to the requested format. The system can convert data as indicated in the following table.
Clipboard Format | Conversion Format | Platform Support 

CF_OEMTEXT       | CF_TEXT           | Windows NT/Windows 2000, Windows 95/Windows 98/Windows Me 
CF_OEMTEXT       | CF_UNICODETEXT    | Windows NT/Windows 2000 
CF_TEXT          | CF_OEMTEXT        | Windows NT/Windows 2000, Windows 95/Windows 98/Windows Me 
CF_TEXT          | CF_UNICODETEXT    | Windows NT/Windows 2000 
CF_UNICODETEXT   | CF_OEMTEXT        | Windows NT/Windows 2000 
CF_UNICODETEXT   | CF_TEXT           | Windows NT/Windows 2000 


Let me have a simple try on the windows xp:
C++
SetClipboardData(/*an ansi string*/, CF_TEXT);
// then
IsClipboardFormatAvailable(CF_TEXT); // success
IsClipboardFormatAvailable(CF_UNICODETEXT); // success
GetClipboardData(CF_TEXT); // success and read right string
GetClipboardData(CF_UNICODETEXT); // success and read right string
// unicode string also can test passed


but I test on the windows 98, if you SetClipboardData with CF_TEXT, then you must GetClipboardData with CF_TEXT, and if you SetClipboardData with CF_UNICODETEXT, you must GetClipboardData with CF_UNICODETEXT. when I SetClipboardData with CF_UNICODETEXT, the notepad(win98) can't paste text from clipboard.

so, if you wanna support the windows 98, you must SetClipboardData with CF_TEXT, else you can use any one of them. I recommend you use unicode.
 
Share this answer
 
Comments
YvesDaoust 1-Dec-11 2:58am    
Wonderful, this closes my question. Thank you for providing this precious info.
It is better just because you can use Unicode. If your application allows to enter Unicode at all, CF_TEXT is absolutely unacceptable, use CF_UNICODETEXT.

This reduces the problem to this one: do you want to support Unicode at all? It really depends on your application and requirements. An application can really be non-Unicode when it supports only ASCII (not even ANSI beyond the code point of 127). Some applications like that do have some right to exist.

As a rule of thumb, though, I think its the best to assume that the time of non-Unicode texts has gone.

—SA
 
Share this answer
 
Comments
YvesDaoust 28-Nov-11 3:11am    
Thanks for the answer.

My question is more about the client applications: do they all recognize Unicode content ? (the classical MS applications do)
Sergey Alexandrovich Kryukov 30-Nov-11 12:24pm    
You do not have to worry about it. You cannot make it worse by using Unicode. Some applications handle only ANSI. So, what's the problem? They will copy correctly (and your Unicode application can pick up non-Unicode text from clipboard), and they will paste only ASCII subset part of your Unicode characters, others merged under '?'. But this is what you want, really. So, a practical rule would be: you can do all new development supporting Unicode.
--SA
YvesDaoust 28-Nov-11 6:40am    
By the way, nobody's perfect: when printing Unicode characters to a Windows console, two bad things happen:
- printing with wprintf does not use the Unicode character set (€ appears as ?);
- printing with _putws just stops writing when it meets a non-ascii character !
Sergey Alexandrovich Kryukov 30-Nov-11 12:21pm    
This is weird, I would like to see it...
Thank you for the note.
--SA
C++
if ( OpenClipboard(NULL) )
	{
		// 
		// EmptyClipboard sets the clipboard owner to NULL		        // 
		if ( !EmptyClipboard() ) goto OnError1;
		TCHAR chValue[] = _T("Hello Clipboard.");
		size_t nLength = _tcslen(chValue);
		size_t nByteOfBuffer = (nLength + 1) * sizeof(TCHAR);
		HGLOBAL hGlobal = NULL;
		hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT,  nByteOfBuffer);
		if (NULL == hGlobal) goto OnError1;
		LPTSTR pBuf = (LPTSTR)GlobalLock(hGlobal); 
		if (NULL == pBuf)
		{
			GlobalFree(hGlobal);
			hGlobal = NULL;
		}
		
		_tcscpy_s(pBuf, nLength + 1, chValue);
#ifdef _UNICODE
		SetClipboardData(CF_UNICODETEXT, hGlobal);
#else
		SetClipboardData(CF_TEXT, hGlobal);
#endif
		GlobalUnlock(hGlobal);
OnError1:
		CloseClipboard();
	}
 
Share this answer
 
Comments
YvesDaoust 30-Nov-11 7:03am    
Did you read the question ? The question is about the formats to support, not about coding.
Easy Way :
C++
// store_clipboard.cpp
// compile with: /clr
#using <system.dll>
#using <system.drawing.dll>
#using <system.windows.forms.dll>

using namespace System;
using namespace System::Windows::Forms;

[STAThread] int main()
{
   String^ str = "This text is copied into the Clipboard.";

   // Use 'true' as the second argument if
   // the data is to remain in the clipboard
   // after the program terminates.
   Clipboard::SetDataObject(str, true);

   Console::WriteLine("Added text to the Clipboard.");

   return 0;
}

</system.windows.forms.dll></system.drawing.dll></system.dll>
 
Share this answer
 
Comments
YvesDaoust 30-Nov-11 6:59am    
Did you read the question ? The question is about the formats to support, not about coding.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900