2017-05-18 266 views
-2

我有一個包含多個圖片框的面板。圖片框圖像閃爍

我想給用戶選擇任何圖片框的任何部分的選項。

用戶將通過鼠標進行選擇。

我想繪製一個半透明的矩形的圖片框,而鼠標移動的選擇。

代碼工作正常,但矩形閃爍。 我想停止閃爍。

我嘗試使用how to stop flickering C# winforms

而且雙緩衝,使用How to force graphic to be redrawn with the invalidate method

但不工作添加Invalide。請幫忙。

我的代碼:

private Brush selectionBrush = new SolidBrush(Color.FromArgb(70, 76, 255, 0)); 

private void Picture_MouseMove(object sender, MouseEventArgs e) 
{ 
    if (e.Button != MouseButtons.Left) return; 

    PictureBox pb = (PictureBox)(sender); 

    Point tempEndPoint = e.Location; 
    Rect.Location = new Point(
     Math.Min(RecStartpoint.X, tempEndPoint.X), 
     Math.Min(RecStartpoint.Y, tempEndPoint.Y)); 
    Rect.Size = new Size(
     Math.Abs(RecStartpoint.X - tempEndPoint.X), 
     Math.Abs(RecStartpoint.Y - tempEndPoint.Y)); 

    pb.CreateGraphics().FillRectangle(selectionBrush, Rect); 
    pb.Invalidate(Rect); 
} 
+3

,如果你刪除哪些'pb.Invalidate(矩形);' –

+0

@LeiYang繪製的矩形是缺乏透明度和不消失鼠標向上。 –

+0

我想你需要在'Paint'事件中繪製所有/部分,並以這種方式可以在其他按鈕事件中調用'Invalidate'。 –

回答

0

您表示雙緩衝解決方案並不爲你工作,你發現了,爲什麼?因爲下面的自定義控制繪製一個矩形可觀的PictureBox的無閃爍:

public class TryDoubleBufferAgain : PictureBox 
{ 
    public TryDoubleBufferAgain() 
    { 
     this.DoubleBuffered = true; 
     this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); 
     this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); 
     this.UpdateStyles(); 
    } 
    protected override void OnMouseMove(MouseEventArgs e) 
    { 
     this.Refresh(); 
     base.OnMouseMove(e); 
    } 
    protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 

     // Edit below to actually draw a usefull rectangle 
     e.Graphics.DrawRectangle(System.Drawing.Pens.Red, new System.Drawing.Rectangle(0, 0, Cursor.Position.X, Cursor.Position.Y)); 
    } 
}