代码之家  ›  专栏  ›  技术社区  ›  RanH

如何在C_中的同一点添加标签apon鼠标?

  •  1
  • RanH  · 技术社区  · 14 年前

    我的程序有一个PictureBox,我想单击鼠标,或单击ContextMenuStrip选项,使某些内容出现在单击的同一个位置。

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

    我该怎么办?如何发送单击坐标(x,y)并使其在相同坐标下显示?

    谢谢!

    或者在ContextMenuStrip选项上,使某个内容出现在单击的同一点上。

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

    我该怎么办?如何发送单击坐标(x,y)并使其在相同坐标下显示?

    谢谢!

    3 回复  |  直到 14 年前
        1
  •  1
  •   vgru    14 年前

    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);
    }
    
        2
  •  1
  •   Nitish Katare    14 年前

        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);
        }
    
        3
  •  0
  •   Dr. Rajesh Rolen    14 年前