獲取剪下板有格式的陣列

qq_1769172560發表於2020-11-02

void GetClipboardStr(BSTR *o_pbsText)
{
	CString strText;

	if (IsClipboardFormatAvailable(CF_UNICODETEXT))
	{
		OpenClipboard(NULL);
		if (IsClipboardFormatAvailable(CF_UNICODETEXT))
		{
			HANDLE hGlobal = GetClipboardData(CF_UNICODETEXT);
			strText = (LPCTSTR)GlobalLock(hGlobal);
			GlobalUnlock(hGlobal);
		}
		CloseClipboard();
	}

	*o_pbsText = strText.AllocSysString();;
}

void GetFormatData(BSTR i_bsText, vector<double> &o_vFormatData)
{
	o_vFormatData.clear();

	CString sText((LPCTSTR)i_bsText);

	int i(0);
	while (!sText.IsEmpty())
	{
		// 資料分隔符
		CString sDiv;
		if (i % 2 == 0)
		{
			int iDot(0), iTab(0);
			iDot = sText.Find(_T(","));
			iTab = sText.Find(_T("\t"));

			if (-1 != iDot && -1 != iTab)
			{
				if (iDot < iTab)
					sDiv.Format(_T(","));
				else
					sDiv.Format(_T("\t"));
			}
			else if (-1 != sText.Find(_T(",")))
				sDiv.Format(_T(","));
			else
				sDiv.Format(_T("\t"));
		}
		else
			sDiv.Format(_T("\r\n"));

		// 獲取當前資料
		CString sCurData;

		int iPos = sText.Find(sDiv);
		if (-1 == iPos)
		{
			if (sText.SpanIncluding(_T("+-.0123456789")) != sText)  // 有時候最後一個資料不包含分隔符
			{
				o_vFormatData.clear();
				return;
			}
			o_vFormatData.push_back(_ttof(sText));
			return;
		}
		else
			sCurData = sText.Left(iPos);

		// 判斷當前資料是否正確
		if (sCurData.SpanIncluding(_T("+-.0123456789")) != sCurData)
		{
			o_vFormatData.clear();
			return;
		}

		o_vFormatData.push_back(_ttof(sCurData));

		int nCount = sText.GetLength() - iPos - (i % 2 ? 2 : 1);
		sText = sText.Right(nCount);
		i++;
	}

	if (o_vFormatData.size() % 2)
		o_vFormatData.clear();

	return;
}

相關文章