c++ 获取windows剪切板的富文本

 

先上一下可以直接运行的代码:

新建控制台可以直接运行,显示当前剪切板中的富文本。字符串为utf-8,可以视情况转GBK 

#include <windows.h>
#include <iostream>

std::wstring  StringToWString(const std::string& s)
{
	std::wstring wszStr;
	int nLength = MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, NULL, NULL);
	wszStr.resize(nLength);
	LPWSTR lpwszStr = new wchar_t[nLength];
	MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, lpwszStr, nLength);
	wszStr = lpwszStr;
	delete[] lpwszStr;
	return wszStr;
}

std::string getClipBoardHtml()
{
	std::string strResult = "";
	if (::OpenClipboard(NULL))
	{
		UINT uFormat = 0;
		std::string stdString = "HTML Format";
		std::wstring strHtml = StringToWString(stdString);
		UINT htmlFormat = ::RegisterClipboardFormat(strHtml.c_str());
		while (uFormat = ::EnumClipboardFormats(uFormat))
		{
			if (htmlFormat == uFormat)
			{
				if (::IsClipboardFormatAvailable(uFormat))
				{
					HANDLE handle = ::GetClipboardData(uFormat);
					CHAR* data = (CHAR*)GlobalLock(handle);
					::GlobalUnlock(handle);
					strResult = data;
				}
			}
		}
		::CloseClipboard();
	}
	return strResult;
}
int main()
{
	std::string strHtml = getClipBoardHtml();
	std::cout << strHtml.c_str();
	getchar();
	return 0;
}

你只想获取富文本的话下面就不用看了,用上面的代码就行了。下面是获取剪切板的所有的内容的代码

#include "StdAfx.h"
#include "MainDlg.h"

TCHAR *StrClipboardFormats[17]={
	_T("CF_TEXT"),
	_T("CF_BITMAP"),
	_T("CF_METAFILEPICT"),
	_T("CF_SYLK"),
	_T("CF_DIF"),
	_T("CF_TIFF"),
	_T("CF_OEMTEXT"),
	_T("CF_DIB"),
	_T("CF_PALETTE"),
	_T("CF_PENDATA"),
	_T("CF_RIFF"),
	_T("CF_WAVE"),
	_T("CF_UNICODETEXT"),
	_T("CF_ENHMETAFILE"),
	_T("CF_HDROP"),
	_T("CF_LOCALE"),
	_T("CF_DIBV5")
};

CString GetClipFormatName(int uFormat,int htmlFormat)
{
	if (uFormat <= 17)
	{
		return StrClipboardFormats[uFormat-1];
	}
	else if (uFormat == htmlFormat)
	{
		return _T("CF_HTML");
	}
	else
	{
		TCHAR szFormatName[256];
		if (GetClipboardFormatName(uFormat, szFormatName, 
			sizeof(szFormatName))) 
		{
			return szFormatName; 
		}
		return _T("NONE");
	}
}

CString ConvertUtf8(char* strUtf8)
{
	int len = MultiByteToWideChar( CP_UTF8, 0, strUtf8, -1, NULL, 0 );
	if ( len == 0 )
		return CString();
	CString ret;
	LPWSTR lpBuffer = ret.GetBuffer( len );
	MultiByteToWideChar( CP_UTF8, 0, strUtf8, -1, lpBuffer, len );
	ret.ReleaseBuffer();
	return ret;
}

LRESULT CMainDlg::OnBnClickedOk(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	m_richEditFormat.SetWindowText(_T(""));
	m_richEditText.SetWindowText(_T(""));
	m_richEditHTML.SetWindowText(_T(""));
	m_richEditRaw.SetWindowText(_T(""));
	m_pictureBox.SetHBitmap(NULL);
	if (::OpenClipboard(NULL))
	{
		UINT uFormat = 0;
		char format[256] = {0};
		UINT htmlFormat = ::RegisterClipboardFormat(_T("HTML Format"));
		while(uFormat = ::EnumClipboardFormats(uFormat))
		{
			CString formatText;
			formatText.Format(_T("%d:%s\n"),uFormat,GetClipFormatName(uFormat,htmlFormat));
			m_richEditFormat.AppendText(formatText);
			if (htmlFormat == uFormat)
			{
				if (::IsClipboardFormatAvailable(uFormat))
				{
					HANDLE handle = ::GetClipboardData(uFormat);
					CHAR* data = (CHAR*)GlobalLock(handle);

					CString unicodeData = ConvertUtf8(data);
					m_richEditHTML.AppendText(unicodeData);
					GlobalUnlock(handle);
				}
			}
			else if (uFormat == CF_UNICODETEXT)
			{
				if (::IsClipboardFormatAvailable(uFormat))
				{
					HANDLE handle = ::GetClipboardData(uFormat);
					TCHAR* data = (TCHAR*)GlobalLock(handle);
					m_richEditText.AppendText(data);
					GlobalUnlock(handle);

				}
			}
			else if (uFormat == CF_BITMAP)
			{
				if (::IsClipboardFormatAvailable(uFormat))
				{
					HBITMAP hBitmap = (HBITMAP)GetClipboardData(uFormat);
					m_pictureBox.SetHBitmap(hBitmap);
				}
			}
		}
		int selectFormat = GetDlgItemInt(IDC_EDITFormat);
		if (selectFormat > 0)
		{
			if (::IsClipboardFormatAvailable(selectFormat))
			{
				HGLOBAL hMem = ::GetClipboardData(selectFormat);
				if (IsDlgButtonChecked(IDC_CHECKBinary))
				{
					LPVOID pvdata = GlobalLock(hMem);
					DWORD uDataSize = GlobalSize(hMem);
					BYTE* pbyData  = new BYTE [ uDataSize ];
					// Copy the data to the newly-allocated memory.
					CopyMemory ( pbyData, pvdata, uDataSize );
					UINT        uOffset;
					UINT        uBytesInThisLine;
					UINT        uLineOffset;

					CString     sLine (_T(' '), 64);            // preallocate enough space for a line
					CString     sWork;
					for ( uOffset = 0; uOffset < uDataSize; uOffset += 8 )
					{
						// How many bytes will be in the next line?  Max of 8.
						uBytesInThisLine = uDataSize - uOffset;
						if ( uBytesInThisLine > 8 )
						{
							uBytesInThisLine = 8;
						}
						// First part of the line - the starting offset.
						sLine.Format ( _T("%08X  "), uOffset );
						// Now loop through the data and add on the hex value of each byte.
						for ( uLineOffset = 0; uLineOffset < uBytesInThisLine; uLineOffset++ )
						{
							sWork.Format ( _T("%02X "), pbyData[uOffset + uLineOffset] );
							sLine += sWork;
							if ( 3 == uLineOffset || 7 == uLineOffset )
								sLine += ' ';
						}

						// If there were less than 8 bytes in this line, pad the line with
						// spaces so the ASCII representation will be in the right column.
						if ( uBytesInThisLine < 8 )
						{
							sLine += CString(_T(' '), 1 + 3 * (8 - uBytesInThisLine) );

							if ( uBytesInThisLine < 4 )
								sLine += _T(' ');
						}

						// Add on the ASCII representation
						for ( uLineOffset = 0; uLineOffset < uBytesInThisLine; uLineOffset++ )
						{
							// If the next byte isn't printable, show a period instead.
							if ( isprint ( pbyData[uOffset + uLineOffset] ))
								sLine += (TCHAR) pbyData[uOffset + uLineOffset];
							else if (uLineOffset+1 < uBytesInThisLine)
							{
								BYTE b[3];
								b[0] = pbyData[uOffset + uLineOffset];
								b[1] = pbyData[uOffset + uLineOffset+1];
								b[2] = 0;
								CString chinese(b);
								uLineOffset++;
								sLine += chinese;
							}
							else
								sLine += _T('.');
						}
						sLine += _T('\n');
						m_richEditRaw.AppendText(sLine);
					}
				}
				else if (selectFormat == CF_UNICODETEXT)
				{
					m_richEditRaw.AppendText((TCHAR*)GlobalLock(hMem));
				}
				else
				{
					char* data = (char*)GlobalLock(hMem);
					CString unicodeData(data);
					if (IsDlgButtonChecked(IDC_CHECKUnicode))
					{
						unicodeData= (TCHAR*)data;//ConvertUtf8(data);
					}
					m_richEditRaw.AppendText(unicodeData);
				}
				
				GlobalUnlock(hMem);
			}
		}

		
		CloseClipboard();
	}
	return 0;
}

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值