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);
}
Run Code Online (Sandbox Code Playgroud)
当我点击你可以在附加屏幕上看到的位置时,点出现在另一个地方.如何解决这个问题呢?
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);
}
Run Code Online (Sandbox Code Playgroud)