// StringGrid1 OnMouseUp
void __fastcall TForm1::StringGrid1MouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
if (Button != mbRight) return;
int Col, Row;
StringGrid1->MouseToCell(X, Y, Col, Row);
if (Col < 1 || Row < 1) return; // 標題列/空白處不處理
StringGrid1->Col = Col; StringGrid1->Row = Row; // 選中
g_ptSelect = Mouse->CursorPos; // 記錄螢幕座標
PopupMenu1->Popup(g_ptSelect.x, g_ptSelect.y);
}
// miCopy OnClick
void __fastcall TForm1::miCopyClick(TObject *Sender)
{
TPoint pt = StringGrid1->ScreenToClient(g_ptSelect);
int Col, Row;
StringGrid1->MouseToCell(pt.x, pt.y, Col, Row);
Clipboard()->AsText = StringGrid1->Cells[Col][Row];
}
// miPaste OnClick
void __fastcall TForm1::miPasteClick(TObject *Sender)
{
TPoint pt = StringGrid1->ScreenToClient(g_ptSelect);
int Col, Row;
StringGrid1->MouseToCell(pt.x, pt.y, Col, Row);
StringGrid1->Cells[Col][Row] = Clipboard()->AsText;
}
若 `Options` 含 `goRangeSelect`,可用 `Selection` 屬性 (TRect) 取得範圍,迴圈複製/貼上多儲存格。