如何在C#中鼠标单击同一位置时添加标签?

发布于 2024-10-03 16:49:20 字数 230 浏览 9 评论 0原文

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

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

我该怎么做?我如何发送点击坐标(x,y)并使某些内容出现在这些相同的坐标处?

谢谢 !

替代文本

my program has a picturebox, and I want, apon a mouse click, or on ContextMenuStrip choice, to make something appear at the same spot of the click.

as seen in the picture, i would like to add some kind of a note on the specific clicked date area (probably add a user control)

How do i go at it ? how can i send the click cordinates (x,y) and make something appear at these same coordinates ?

Thanks !

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小巷里的女流氓 2024-10-10 16:49:20

我将创建一个类,该类将提供菜单项并捕获 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);
}

I would create a class which would provide menu items and capture x,y coordinates to have them ready when the item is clicked. Or you can capture these coordinates in an anonymous delegate.

Something like this:

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);
}
你另情深 2024-10-10 16:49:20

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

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

An sample code for your requirement, In my below code i am adding button control on mouse click. You can modify the code as per ur need.

    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);
    }
挖鼻大婶 2024-10-10 16:49:20

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

you can use either tool tip or use mousemove event. this event will provide you current xy postion of mouse and then you can show your content by either visible true/false at that place or take a label and set its text and then set its xy position according to xy of mouse. and then on mouseleave event move that label to off screen or hide

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文