2017-08-31 45 views
1
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); 
} 

enter image description here如何在圖像框中添加標記?

當我在的地方,你可以連接的屏幕上看到的點擊,點出現在另一個地方。如何解決這個問題呢?

回答

5

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

太好了。萬分感謝 :) –

相關問題