a) GridView中任何一个Cell增加右键复制功能
GridHitInfo gridHitInfo = new GridHitInfo(); //用户接收GridView中单元格数据
void gridView_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
Point point = new Point(e.X, e.Y);
gridHitInfo = this.gridView.CalHitInfo(point);
}
}
tsmiCopy_Click(object sender, EventArgs e)
{
if(gridHitInfo.Column == null) return;
string copiedData = this.gridView.GetRowCellDisplayText(gridHitInfo.RowHandle, gridHitInfo.Column);
if(string.IsNullOrEmpty(copiedData)) return;
Clipboard.Clear();
Clipboard.SetData(DataFormats.Text, copiedData);
}
b) 控件增加右键复制功能(control可以是任何显示数据控件)
control.ContextMenuStrip = CreateCopyMenu(context.Text);
ContextMenuStrip CreateCopyMenu(string copiedData){
var menu = new ContextMenuStrip();
var tsmi = new ToolStripMenuItem();
tsmi.Text = "Copy";
tsmi.Click += new EventHander((sender, args)=>{
Clipboard.Clear();
Clipboard.SetData(DataFormats.Text, copiedData);
}
menu.Items.Add(tsmi);
return menu;
}