Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在鼠标点击C#中的同一点时添加标签?_C#_.net_Coordinates_Mouseclick Event_Onmouseclick - Fatal编程技术网

如何在鼠标点击C#中的同一点时添加标签?

如何在鼠标点击C#中的同一点时添加标签?,c#,.net,coordinates,mouseclick-event,onmouseclick,C#,.net,Coordinates,Mouseclick Event,Onmouseclick,我的程序有一个picturebox,我想,点击鼠标,或者在ContextMenuStrip选项上,让一些东西出现在点击的同一点上 如图所示,我想在特定的点击日期区域添加一些注释(可能添加一个用户控件) 我该怎么做呢?我如何发送click cordinate(x,y)并使某些内容显示在这些相同的坐标上 谢谢 您可以使用工具提示或使用mousemove事件。此事件将为您提供鼠标的当前xy位置,然后您可以在该位置通过可见的true/false显示内容,或者使用标签设置其文本,然后根据鼠标的xy设置其x

我的程序有一个picturebox,我想,点击鼠标,或者在ContextMenuStrip选项上,让一些东西出现在点击的同一点上

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

我该怎么做呢?我如何发送click cordinate(x,y)并使某些内容显示在这些相同的坐标上

谢谢


您可以使用工具提示或使用mousemove事件。此事件将为您提供鼠标的当前xy位置,然后您可以在该位置通过可见的true/false显示内容,或者使用标签设置其文本,然后根据鼠标的xy设置其xy位置。然后在mouseleave事件中,将标签移动到屏幕外或隐藏,我将创建一个类,该类将提供菜单项并捕获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);
}

一个示例代码为您的要求,在我下面的代码中,我添加了鼠标点击按钮控制。您可以根据需要修改代码

    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);
    }

也许工具提示是一个更好的选择…我最想做的就是在同一天做一个记录(比如待办事项记录)。(坚持住)你能详细说明我应该如何使用工具提示吗?