StringGrid에서 복사 붙여넣기 관련된 질문이 종종 오라와서
Clipboard를 이용해 한번 만들어 보았습니다.
충분히 테스트 해본것은 아니니 문제점이 있으면 댓글 달아주시고
수정해서 쓰세요
복사 붙여넣기 함수
#include "Clipbrd.hpp"
//----------------------------------------------------------------------
// StringGrid의 선택된 Cell의 내용을 CR과 Tab으로 구분해 Clipboard 에 복사
//----------------------------------------------------------------------
void __fastcall CopyGridToClipboard(TStringGrid *Grid)
{
TGridRect sRec=Grid->Selection;
TStringList *sLst=new TStringList;
String s;
for(int iRow=sRec.Top;iRow<=sRec.Bottom;iRow++)
{
s="";
for(int iCol=sRec.Left ; iCol < sRec.Right ; iCol++)
{
s=s+Grid->Cells[iCol][iRow]+'\t';
}
s=s+Grid->Cells[sRec.Right][iRow];
sLst->Add(s);
}
Clipboard()->AsText=sLst->Text;
delete sLst;
}
//---------------------------------------------------------------------
// CR과 Tab으로 구분된 Clipboard.Text를 StringGrid에 붙여넣기
//---------------------------------------------------------------------
void __fastcall PasteClipboardToGrid(TStringGrid *Grid)
{
String str=Clipboard()->AsText;
if(str=="")return;
TStringList *sLowLst=new TStringList;
TStringList *sCols=new TStringList;
TGridRect sRec=Grid->Selection;
sLowLst->Text=str;
sCols->StrictDelimiter=true;
sCols->Delimiter='\t';
for(int i=0;iCount;i++)
{
if( (sRec.Top+i)>=Grid->RowCount)break;
String sRowStr=sLowLst->Strings[i];
sCols->DelimitedText=sRowStr;
for(int c=0;cCount;c++)
{
if( (sRec.Left+c)>=Grid->ColCount)break;
Grid->Cells[sRec.Left+c][sRec.Top+i]=sCols->Strings[c];
}
}
delete sLowLst;
delete sCols;
}
사용방법은?
다음과 같이 쓰시면 될듯합니다.
void __fastcall TForm6::StringGrid1KeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
if(Shift.Contains(ssCtrl))
{
if( Key=='C' || Key=='c')
{
CopyGridToClipboard((TStringGrid *)Sender);
}
else if(Key=='V' || Key=='v')
{
PasteClipboardToGrid((TStringGrid *)Sender);
}
}
}
추신:
copy해서 Excel에 붙여넣기 해도 잘 됩니다.
그럼...
|