2017-06-21 52 views
2

這是更多的方法問題。我知道如何創建一個可以從各個方面擴展的控件,但是我不明白爲什麼這個代碼在沒有重繪問題(抖動)的情況下不能流暢地工作。它僅從頂部縮放面板,但在此過程中抖動。我錯過了什麼?尺寸從上到下

public partial class Form1 : Form 
{ 
    Point MousePoint = new Point(); 
    public Form1() 
    { 
     InitializeComponent(); 
     panel1.MouseMove += Panel1_MouseMove; 
     panel1.MouseDown += Panel1_MouseDown; 
     panel1.Width = 100; 
     panel1.Height = 100; 
    } 

    private void Panel1_MouseDown(object sender, MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) 
     { 
      MousePoint = e.Location; 
     } 
    } 

    private void Panel1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if(e.Button == MouseButtons.Left) 
     { 
      panel1.Top = e.Location.Y + panel1.Location.Y - MousePoint.Y; 
      panel1.Height = panel1.Height - e.Y + MousePoint.Y; 
     } 
    } 
} 
+0

將窗體設置爲雙緩衝 – Aaron

回答

1

您正在改變兩個不同的屬性:頂部,然後是高度。這可能會導致你看到的「抖動」。

嘗試使用調用的setBounds改爲:

panel1.SetBounds(panel1.Left, 
        e.Location.Y + panel1.Location.Y - mousePoint.Y, 
        panel1.Width, 
        panel1.Height - e.Y + mousePoint.Y); 

如果包含該錨定板等內部控制,可能影響大小調整的是平滑了。

+0

就是這樣!我從來沒有想過SetBounds()或曾經使用它!非常感謝您啓發我。 – MBasic