本文介绍了将文本复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如今,将文本复制到剪贴板时,将SetClipboardData用作CF_UNICODETEXT而不是CF_TEXT更好吗?粘贴实体会有所不同吗?

有任何论点吗?

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 ?

推荐答案

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 



让我在Windows XP上进行简单尝试:



Let me have a simple try on the windows xp:

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



但是我在Windows 98上进行了测试,如果使用CF_TEXT设置SetClipboardData,则必须使用CF_TEXT设置GetClipboardData,如果使用CF_UNICODETEXT设置SetClipboardData,则必须使用CF_UNICODETEXT设置GetClipboardData.当我使用CF_UNICODETEXT SetClipboardData时,记事本(win98)无法从剪贴板粘贴文本.

因此,如果要支持Windows 98,则必须使用CF_TEXT设置SetClipboardData,否则可以使用其中任何一个.我建议您使用unicode.



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.



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();
	}


这篇关于将文本复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-31 09:08