1

我的程序有一个图片框,我希望通过鼠标单击或 ContextMenuStrip 选项使某些内容出现在单击的同一位置。

如图所示,我想在特定的点击日期区域添加某种注释(可能添加一个用户控件)

我该怎么做?我怎样才能发送点击坐标(x,y)并使某些东西出现在这些相同的坐标上?

谢谢 !

替代文字

4

3 回答 3

1

我将创建一个类,该类将提供菜单项并捕获 x,y 坐标,以便在单击该项目时准备好它们。或者您可以在匿名委托中捕获这些坐标。

像这样的东西:

public Form1()
{
    InitializeComponent();
    MouseClick += new MouseEventHandler(Form1_MouseClick);
}

private void Form1_MouseClick (object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        ContextMenuStrip ctxMenu = new ContextMenuStrip();

        // following line creates an anonymous delegate
        // and captures the "e" MouseEventArgs from 
        // this method
        ctxMenu.Items.Add(new ToolStripMenuItem(
           "Insert info", null, (s, args) => InsertInfoPoint(e.Location)));

        ctxMenu.Show(this, e.Location);
    }
}

private void InsertInfoPoint(Point location)
{
    // insert actual "info point"
    Label lbl = new Label()
    {
        Text = "new label",
        BorderStyle = BorderStyle.FixedSingle,
        Left = location.X, Top = location.Y
    };
    this.Controls.Add(lbl);
}
于 2010-11-19T10:16:35.330 回答
1

满足您要求的示例代码,在下面的代码中,我在鼠标单击时添加按钮控件。您可以根据需要修改代码。

    int xValue=0, yValue=0;
    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        xValue = e.X;
        yValue = e.Y;
        Button btn = new Button();
        btn.Name = "Sample Button";
        this.Controls.Add(btn);
        btn.Location = new Point(xValue, yValue);
    }
于 2010-11-19T10:21:23.537 回答
0

您可以使用工具提示或使用 mousemove 事件。此事件将为您提供鼠标的当前 xy 位置,然后您可以在该位置通过可见的真/假来显示您的内容,或者获取标签并设置其文本,然后根据鼠标的 xy 设置其 xy 位置。然后在 mouseleave 事件中将该标签移到屏幕外或隐藏

于 2010-11-19T10:05:48.900 回答