2012-09-08 20 views
3

我一直在使用雙緩衝面板將圖像繪製到自身上,但是當我將一個pictureBox移過它時,它會閃爍並滯後。如何將畫框移動到有塗層的面板上而沒有閃爍?

我一直在使用移動pictureBoxes的代碼是:

private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
{ 
    x = e.X; 
    y = e.Y; 
    panel1.Invalidate(); 
} 

private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
{ 
    if (e.Button == MouseButtons.Left) 
    { 
     pictureBox1.Left += (e.X - x); 
     pictureBox1.Top += (e.Y - y); 
    } 
} 

private void panel1_Paint(object sender, PaintEventArgs e) 
{ 
    e.Graphics.DrawImage(pictureBox2.BackgroundImage, new Rectangle(pictureBox2.Location, pictureBox2.Size));  
} 

,你可以看到pictureBox2這是不可見的被塗到doublebuffered面板。當我移動pictureBox1時,它會在面板上閃爍。是的,我一直在使用的Invalidate()面板

我使用的doublebuffered面板類代碼:

public class DoubleBufferPanel : Panel 
{ 


    public DoubleBufferPanel() 
    { 

     // Set the value of the double-buffering style bits to true. 
     this.DoubleBuffered = true; 



     this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | 
     ControlStyles.AllPaintingInWmPaint, true); 

     this.UpdateStyles(); 

    } 

} 

這裏是什麼,我想實現無閃爍的圖片(。沒有被鼠標移動的pictureBoxes被繪製到面板上)。沒有看到閃爍 enter image description here

+0

這是不可避免的,雙緩衝實際上使情況變得更糟。完全擺脫它的唯一方法是不使用額外的控制。你不需要它們,你已經有一個可以做這幅畫的面板。然而,您將不得不添加代碼來進行鼠標點擊測試並跟蹤圖像位置。 –

+1

你是說只使用panel1的Mouse_down和Mouse_move事件嗎? – kev

回答

0

它滯後因爲你叫Invalidate,而不是Refresh我不能做到這一點。嘗試在Form上設置雙緩衝。如果它沒有幫助,請閱讀有關自定義控件的一些教程。 LINK