我的班级有问题,它是为了:
- 从剪贴板中获取文本
- 将它扔进矢量表(1文本行== 1表记录)
- 将它连接回一个字符串
- 将其粘贴到剪贴板中
不幸的是,我在使用→(Alt + 26)这样的字符时出现问题,每当我加载包含此类字符的字符串时,将其粘贴回剪贴板后我就会在其中收到损坏的字符和' #39;的地方
我在Windows 7上使用Code Block和GNU GCC Compiler
此外,→char使用cout在控制台中显示(使用默认设置)。
library.hpp:
#ifndef library
#define library
#include <iostream>
#include <fstream>
#include <conio.h>
#include <windows.h>
#include <string>
#include <vector>
#include <boost/regex.hpp>
#include <boost/algorithm/string_regex.hpp>
using namespace std;
//used in string.find function
const size_t NotFound=(size_t)-1;
#endif
copy_paste.hpp
#ifndef copy_paste_hpp
#define copy_paste_hpp
#include "library.hpp"
class copy_paste
{
private:
vector < string > text;
string fullText;
//splitting and joining fullText
bool prepareText();
bool prepareFullText();
//integration with Windows Clipboard
bool setClip();
string getClip();
public:
copy_paste();
vector <string> getText();
void setText(vector<string> a);
};
#endif
copy_paste.cpp
#ifndef copy_paste_cpp
#define copy_paste_cpp
#include "copy_paste.hpp" //#include "declaration.hpp"
copy_paste::copy_paste()
{
fullText="d";
};
bool copy_paste::prepareText()
{
if(text.empty()==false)
text.clear();
if(fullText=="")
return 0;
split( text, fullText, boost::algorithm::is_any_of("\n")); // (1)
return true;
};
bool copy_paste::prepareFullText()
{
fullText="";
for(unsigned int i=0; i<text.size();i++)
{
fullText+=text[i];
fullText+="\n";
}
// cout<<fullText;
return true;
};
string copy_paste::getClip()
{
string res;
if( !OpenClipboard( 0 ) )
return res;
HANDLE hMem = GetClipboardData( CF_OEMTEXT );
if( !hMem )
{
CloseClipboard();
return res;
}
char * p =( char * ) GlobalLock( hMem );
if( !p )
{
CloseClipboard();
return res;
}
res = p;
GlobalUnlock( hMem );
CloseClipboard();
return res;
}
bool copy_paste::setClip()
{
HANDLE hMem = GlobalAlloc( GMEM_MOVEABLE, fullText.length() + 1 );
if( !hMem )
return false;
char * p =( char * ) GlobalLock( hMem );
if( !p )
{
GlobalFree( hMem );
return false;
}
for( size_t i = 0; i <= fullText.length(); ++i )
p[ i ] = fullText[ i ];
GlobalUnlock( hMem );
if( !OpenClipboard( 0 ) )
{
GlobalFree( hMem );
return false;
}
EmptyClipboard();
SetClipboardData( CF_OEMTEXT, hMem );
CloseClipboard();
return true;
}
vector<string> copy_paste::getText()
{
fullText=getClip();
prepareText();
return text;
};
void copy_paste::setText(vector<string> a)
{
text=a;
prepareFullText();
setClip();
};
#endif