2011-11-17 31 views
1

我知道這應該是非常簡單的,尤其是考慮到所有的在線信息和示例代碼,但由於某種原因,我無法讓這個工作。如何使用MouseMove事件在面板內移動標籤?

我在winform上有一個標籤和一個面板。我希望標籤只在鼠標指針進入並在面板內移動時才遵循。

我知道它的工作原理,但它的位置完全移動了,並且在移動winform時它總是在變化。

這裏是的MouseMove和其他事件:

constructor MainForm; 
begin 
    InitializeComponent(); 
    label2.Visible:=false; 
end; 

method MainForm.panel1_MouseMove(sender: System.Object; e: System.Windows.Forms.MouseEventArgs); 
begin 
    //label2.Location := panel1.PointToScreen(e.Location); 
    label2.Location := self.PointToScreen(e.Location); 
    label2.Invalidate; 
end; 

method MainForm.panel1_MouseEnter(sender: System.Object; e: System.EventArgs); 
begin 
    label2.Visible:=true; 
end; 

method MainForm.panel1_MouseLeave(sender: System.Object; e: System.EventArgs); 
begin 
    label2.Visible:=false; 
end; 

更新如所建議的通過larstech,我修改了的代碼,但該標籤仍然顯示框外如下圖像被示出。

myimage

感謝,

+0

是標籤PANEL1的子控件?這不是很清楚。 – LarsTech

+0

@LarsTech不,它不是。該標籤不在面板內的Winform上。 Panel1文本是包含文本「Panel1. – ThN

回答

4

我不知道delphi-prism,但不會是僅僅是:

label2.Location := e.Location; 

因爲標籤是不是孩子的控制,試試這個:

label2.Location := new Point(panel1.Left + e.Location.X, panel1.Top + e.Location.Y); 

顯然,我可能沒有正確的語法。

如果你只是試圖移動工具提示,這部作品在C#:

private ToolTip _tips = new ToolTip(); 

private void panel1_MouseMove(object sender, MouseEventArgs e) { 
    _tips.Show("test", panel1, e.Location.X + 10, e.Location.Y + 10); 
} 
+0

」的標籤,我將該標籤作爲面板的一個子項,現在它正在工作,但我必須保留mousedown以顯示標籤 – ThN

+0

@digitalanalog更新的答案。我認爲計時器可以工作可能會更好,因爲標籤會導致面板的MouseLeave事件在鼠標移過標籤時觸發,您可以禁用該標籤,但隨後會禁用該標籤。 – LarsTech

+0

爲什麼MouseLeave每次發生MouseEnter觸發時都會觸發這種情況 – ThN

相關問題