读书人

StringGrid选中多个单元格,该怎么解决

发布时间: 2012-04-08 14:38:30 作者: rapoo

StringGrid选中多个单元格
能否用鼠标选中StringGrid中的单元格并进行复制粘贴到另外选中的多个单元格中?

[解决办法]
//Copy1 和 Paste1是弹出菜单PopupMenu1里的项,细节自己写吧

TGridRect CpyR;

void __fastcall TForm1::Copy1Click(TObject *Sender)
{
CpyR=StringGrid1-> Selection;
}
//---------------------------------------
void __fastcall TForm1::Paste1Click(TObject *Sender)
{
TPoint cp=StringGrid1-> ScreenToClient(PopupMenu1-> PopupPoint);
TGridCoord gc=StringGrid1-> MouseCoord(cp.x,cp.y);
if(gc.X <0||gc.Y <0)return;

int xl=CpyR.Right-CpyR.Left+1;
int yl=CpyR.Bottom-CpyR.Top+1;

if(gc.X+xl> StringGrid1-> ColCount) xl=StringGrid1-> ColCount-gc.X;
if(gc.Y+yl> StringGrid1-> RowCount) yl=StringGrid1-> RowCount-gc.Y;

TStringList *sl=new TStringList;
try{
for(int x=0;x <xl;x++)
for(int y=0;y <yl;y++)
{
sl-> AddObject(StringGrid1-> Cells[CpyR.Left+x][CpyR.Top+y],StringGrid1-> Objects[CpyR.Left+x][CpyR.Top+y]);
}
for(int x=0;x <xl;x++)
for(int y=0;y <yl;y++)
{
StringGrid1-> Cells[gc.X+x][gc.Y+y]=sl-> Strings[x*yl+y];
StringGrid1-> Objects[gc.X+x][gc.Y+y]=sl-> Objects[x*yl+y];
}
}
__finally{
delete sl;
}
}
//---------------------------------------

读书人网 >C++ Builder

热点推荐