public Form1()
{
InitializeComponent();
this.imgRoom.Click += new EventHandler(this.pictureBox1_Click);
}
private void pictureBox1_Click(object sender, EventArgs e)
{
var label1 = new LabelControl();
label1.Location = MousePosition;
label1.BackColor = Color.Red;
label1.Parent = imgRoom;
label1.Text = "Point";
imgRoom.Controls.Add(label1);
}
当我在所附屏幕上看到的地方单击时,点出现在另一个地方。如何解决这个问题呢?
最佳答案
MousePosition
在屏幕坐标中。您需要将其转换为客户端坐标。
private void pictureBox1_Click(object sender, EventArgs e)
{
var label1 = new LabelControl();
label1.Location = imgRoom.PointToClient(MousePosition); // changed here.
label1.BackColor = Color.Red;
label1.Parent = imgRoom;
label1.Text = "Point";
imgRoom.Controls.Add(label1);
}
关于c# - 如何在图像框上添加标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45977013/