13,826
社区成员
发帖
与我相关
我的任务
分享
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
// 往StringGrid添加一些数据
for (int i = 1; i < StringGrid1->RowCount; i++)
{
for (int j = 1; j < StringGrid1->ColCount; j++)
{
StringGrid1->Cells[i][j] = IntToStr(i * 10 + j);
}
}
// StringGrid的DragMode默认属性不要改动
StringGrid1->DragMode = dmManual;
// Memo需要将滚动条显示出来,否则内容显示超过一屏时计算Y坐标有误差
Memo1->ScrollBars = ssBoth;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Memo1DragDrop(TObject *Sender, TObject *Source,
int X, int Y)
{
// 拖放到Edit中时,读取拖动源StringGrid的当前格子内容
TStringGrid *sg = (TStringGrid *)Source;
if (!sg) return;
TMemo *mmo = (TMemo *)Sender;
if (!mmo) return;
int x, y;
SIZE Size;
HFONT hOldFont;
TEXTMETRIC tm;
HDC hDC = GetDC(mmo->Handle);
hOldFont = SelectObject(hDC, mmo->Font->Handle);
GetTextMetrics(hDC, &tm);
y = GetScrollPos(mmo->Handle, SB_VERT) + Y / tm.tmHeight;
if (y < mmo->Lines->Count)
{
AnsiString str = mmo->Lines->Strings[y];
LPCSTR s = str.c_str();
for(LPCSTR t=s; *t!=NULL; s=t)
{
t = CharNext(t);
GetTextExtentPoint(hDC, str.c_str(), t-str.c_str(), &Size);
if(X < Size.cx)
break;
}
x = int(s - str.c_str());
}
SelectObject(hDC, hOldFont);
ReleaseDC(mmo->Handle, hDC);
Caption = AnsiString("X=") + x + " Y=" + y;
String strLine = mmo->Lines->Strings[y];
mmo->Lines->Strings[y] = strLine.SubString(1, x)
+ sg->Cells[sg->Col][sg->Row]
+ strLine.SubString(x + 1, strLine.Length());
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Memo1DragOver(TObject *Sender, TObject *Source,
int X, int Y, TDragState State, bool &Accept)
{
// 鼠标经过Edit时,判断拖动源,接受来自属于TStringGrid的组件的拖放
Accept = Source->InheritsFrom(__classid(TStringGrid));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
// 在StringGrid中按下鼠标时判断当前是否在有内容的格子中
int nCol, nRow;
StringGrid1->MouseToCell(X, Y, nCol, nRow);
if (nCol > 0 && nRow > 0)
{
StringGrid1->BeginDrag(true);
}
}
void __fastcall TForm1::Edit1DragDrop(TObject *Sender, TObject *Source,
int X, int Y)
{
// 拖放到Edit中时,读取拖动源StringGrid的当前格子内容
TStringGrid *sg = (TStringGrid *)Source;
if (!sg) return;
TEdit *edt = (TEdit *)Sender;
if (!edt) return;
TControlCanvas *cc = new TControlCanvas;
try
{
cc->Control = edt;
String strText = edt->Text;
// 计算Edit中字符串的宽度(注意不是字符串的长度)
int nTextWidth = cc->TextWidth(strText);
// 计算每个字符地宽度
int nCharWidth = nTextWidth / strText.Length();
// 根据当前鼠标所在的X坐标计算大概是第几个字符的位置
int nCharIndex = (X - 2) / nCharWidth;
// 将StringGrid当前格式的内容添加到Edit文本中
edt->Text = strText.SubString(1, nCharIndex)
+ sg->Cells[sg->Col][sg->Row]
+ strText.SubString(nCharIndex + 1, 256);
}
__finally
{
delete cc;
}
}
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
// 往StringGrid添加一些数据
for (int i = 1; i < StringGrid1->RowCount; i++)
{
for (int j = 1; j < StringGrid1->ColCount; j++)
{
StringGrid1->Cells[i][j] = IntToStr(i * 10 + j);
}
}
// StringGrid的DragMode默认属性不要改动
StringGrid1->DragMode = dmManual;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1DragDrop(TObject *Sender, TObject *Source,
int X, int Y)
{
// 拖放到Edit中时,读取拖动源StringGrid的当前格子内容
TStringGrid *sg = (TStringGrid *)Source;
if (sg)
{
((TEdit *)Sender)->Text = sg->Cells[sg->Col][sg->Row];
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit1DragOver(TObject *Sender, TObject *Source,
int X, int Y, TDragState State, bool &Accept)
{
// 鼠标经过Edit时,判断拖动源,接受来自属于TStringGrid的组件的拖放
Accept = Source->InheritsFrom(__classid(TStringGrid));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
// 在StringGrid中按下鼠标时判断当前是否在有内容的格子中
int nCol, nRow;
StringGrid1->MouseToCell(X, Y, nCol, nRow);
if (nCol > 0 && nRow > 0)
{
StringGrid1->BeginDrag(true);
}
}