如何在c中实现编辑->复制菜单

如何在用 C 编写的 Windows 应用程序中实现复制菜单项

请先 登录 后评论
本文连接: http://www.china-sunrider.com.cn/question/7428
source: https://stackoverflow.com/questions

2 个回答

Community

要确定打开的是哪个窗口,您可以查询 Form.ActiveMDIChild 属性以获取对当前活动窗口的引用。从那里,您可以执行以下两项操作之一:

1) 如果您创建自己的自定义表单类(例如 FormFoo),它有一个新的公共成员函数 GetCopiedData(),然后从该类继承您应用程序的所有子表单,您可以执行以下操作:< /p>

((FormFoo)this.ActiveMDIChild).GetCopiedData();

假设 GetCopiedData 函数将具有特定于表单的实现来检测应将哪些文本复制到剪贴板。

2)你可以使用继承来检测激活的表单类型,然后根据表单类型做一些事情来获取复制的数据:

Form f = this.ActiveMDIChild;
if(f is FormGrid)
{
    ((FormGrid)f).GetGridCopiedData();
} else if(f is FormText) {
    ((FormText)f).GetTextCopiedData();
}

等等

这应该让您开始寻找活动窗口以及如何实现复制功能。如果您在复制 GridView 时需要更多帮助,最好发布另一个问题。

请先 登录 后评论
Community

如果表单是选项卡式的,并且目标控件是 DataGridView,则当右键单击 DataGridView 时,有时可以使用上述方法将表单的 TabControl 作为活动控件返回。

我通过为我的 DataGridView 实现以下处理程序解决了这个问题:-

private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)

{

 if (e.Button == MouseButtons.Right)
 {
      dataGridView.Focus();

      dataGridView.CurrentCell = dataGridView[e.ColumnIndex, e.RowIndex];
 }

}

请先 登录 后评论
  • 11 关注
  • 0 收藏,247 浏览
  • Petteri 提出于 2022-09-24 05:20
user contributions licensed under CC BY-SA.