gpt4 book ai didi

c# - 如何在 C# 中右键单击时保存 x 和 y 坐标

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:55 25 4
gpt4 key购买 nike

我的目标是在右键单击时打开一个 ContextMenuStrip,记住单击 x 和 y,然后单击给定的项目之一来做某事,直到现在这就是我所做的:

public delegate void mydelegate(string s);

public Form1()
{
InitializeComponent();
m_MyContextMenuStrip = new ContextMenuStrip();
m_MyContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(cms_Opening);
this.ContextMenuStrip = m_MyContextMenuStrip;
}

void cms_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
m_MyContextMenuStrip.Items.Clear();

location1 = m_MyContextMenuStrip.PointToClient(new Point(0, 0));
location = m_MyContextMenuStrip.PointToScreen(new Point(0, 0));
// Populate the ContextMenuStrip control with its default items.
m_MyContextMenuStrip.Items.Add("-");
m_MyContextMenuStrip.Items.Add("Apples");
m_MyContextMenuStrip.Items.Add(location.X.ToString() + "and " + location.Y.ToString());

m_MyContextMenuStrip.Items.Add("aaaa", null, new EventHandler(onaaaClick));
// Set Cancel to false.
// It is optimized to true based on empty entry.
e.Cancel = false;
}

private void onaaaClick(object sender, EventArgs e)
{
//will handle the click on aaa

Form2 f2 = new Form2(functodel);
f2.Show();
}

void functodel(string s)
{
Label l = new Label();
l.Text = s;

l.Top = location.Y - 108;
l.Left = location.X - 100;
//l.Left = this.Location.X - location.X;

l.BorderStyle = BorderStyle.FixedSingle;
this.Controls.Add(l);
l.BringToFront();
}

其中 Form2 是:

private mydelegate m_del;

public Form2(mydelegate del)
{
m_del = del;
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
m_del(textBox1.Text);
this.Close();
}

我的问题是当我点击右键时我没有得到我想要的位置,我得到了另一个位置。有人可以向我解释我做错了什么吗?

  1. 如何获得正确的位置。

  2. 如何获得我只有一次项目的 ContextMenuStrip?

  3. 假设在给定的标签上我正在纠正我想单击右键并获得另一个选项,然后在 m_ContextMenuStrip(删除项目并更改颜色)中我该怎么做?

编辑:

我会尝试更好地解释我的动机

我的目标是一张图片,您可以在其中右键单击坐标(x 和 y)为您打开一个菜单(menu1),就像您在 Windows 桌面上所做的一样,左上角的菜单是您单击的坐标,从这个坐标打开一个菜单(就像我想在桌面上更改或打开某些东西它没有在桌面的左上角打开它在我点击的地方打开)。

单击 aaa 项目时,我希望打开另一个表单,并从那里与代表一起打开新标签 form1现在,当右键单击此标签时,我想打开另一个菜单(我们称之为 menu2)

menu1和menu2是不同的生物

我可能错过了主要概念?

最佳答案

您可以访问鼠标坐标并将其计算到您的图片框(在此测试中是一个面板)的点吗?

您可以在菜单条的打开事件上计算它。

MousePositionControl 类的静态属性。

public partial class Form1 : Form {

private void pnlClickOnMe_MouseClick(object sender, MouseEventArgs e) {
Point mouseLocation = MousePosition;
// get point of mouse relative to pnlClickOnMe
Point pointYouAreInterestedIn = pnlClickOnMe.PointToClient(mouseLocation);
lblShowCoordinates.Text = string.Format("{0} - {1}", pointYouAreInterestedIn.X, pointYouAreInterestedIn.Y);
}

public Form1() {
InitializeComponent();
}

#region designer
private void InitializeComponent() {
this.lblShowCoordinates = new System.Windows.Forms.Label();
this.pnlClickOnMe = new System.Windows.Forms.Panel();
this.SuspendLayout();
this.lblShowCoordinates.AutoSize = true;
this.lblShowCoordinates.Location = new System.Drawing.Point(32, 18);
this.lblShowCoordinates.Size = new System.Drawing.Size(35, 13);
this.pnlClickOnMe.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.pnlClickOnMe.Location = new System.Drawing.Point(31, 64);
this.pnlClickOnMe.Size = new System.Drawing.Size(206, 152);
this.pnlClickOnMe.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pnlClickOnMe_MouseClick);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.pnlClickOnMe);
this.Controls.Add(this.lblShowCoordinates);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label lblShowCoordinates;
private System.Windows.Forms.Panel pnlClickOnMe;
}

关于c# - 如何在 C# 中右键单击时保存 x 和 y 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4231957/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com