将文本复制到剪贴板 [英] Copying text to the clipboard

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

问题描述

如今,将文本复制到剪贴板时,将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 ?

推荐答案

对不起,我只会一点英语.
我刚刚读了标题.

MSDN:
系统在某些剪贴板格式之间隐式转换数据:如果窗口以不在剪贴板上的格式请求数据,则系统将可用格式转换为请求的格式.系统可以如下表所示转换数据.
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 



让我在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.


最好是因为可以使用Unicode.如果您的应用程序完全允许输入Unicode,则绝对不接受CF_TEXT,请使用CF_UNICODETEXT.

这将问题简化为:您是否要完全支持Unicode?这实际上取决于您的应用程序和要求.当应用程序仅支持ASCII(甚至不超过127的代码点的ANSI)时,它实际上可以是非Unicode的.诸如此类的某些应用程序确实具有某些权利.

不过,根据经验,我认为最好是假设非Unicode文本的时代已经过去了.

-SA
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


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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆