2009-04-17 24 views
0

我在使用GDI +時遇到自定義繪製虛線矩形的問題。當擴展Winform窗口時,虛線矩形顯示爲實心

當窗口大小增加或向上/向下滾動時,虛線矩形的垂直部分顯示爲實線,實線。快速移動鼠標會導致固體部分越來越少。奇怪的是,水平線不顯示這種行爲,並按預期顯示。

到目前爲止,在OnResize()OnScroll()期間,兩個非最佳解決方案已經設置了ResizeRedraw = true或致電Invalidate()。我當然想避免這種情況,因爲我真正繪製的是更復雜的,這些緩慢的要求破壞了流體體驗。我也試圖使無效,只有新顯示的區域無濟於事 - 只有一個完整的無效似乎工作。

任何關於如何解決這個問題的指針?

演示代碼:

using System.Drawing; 
using System.Drawing.Drawing2D; 
using System.Windows.Forms; 

public class Form1 : Form 
{ 
    static void Main() 
    { 
     Application.Run(new Form1()); 
    } 

    public Form1() 
    { 
     this.ClientSize = new System.Drawing.Size(472, 349); 

     DoubleBuffered = true; 
     //ResizeRedraw = true; 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 

     int dimensions = 70; 

     using (Pen pen = new Pen(Color.Gray)) 
     { 
      pen.DashStyle = DashStyle.Dash; 

      for (int x = 0; x < 20; ++x) 
      { 
       for (int y = 0; y < 20; ++y) 
       { 
        Rectangle rect = new Rectangle(x * dimensions, y * dimensions, dimensions, dimensions); 

        e.Graphics.DrawRectangle(pen, rect); 
       } 
      } 
     } 
    } 
} 

回答

1

我認爲有兩個問題:似乎存在於其中的矩形沒有被正確繪製的窗口的邊緣的區域;並且你正在相互繪製矩形,所以莽撞無法正常工作。

用以下替換您的OnPaint循環:

for (int y = 0; y < Height; y += dimensions) 
    { 
     e.Graphics.DrawLine(pen, 0, y, Width, y); 
    } 
    for (int x = 0; x < Width; x += dimensions) 
    { 
     e.Graphics.DrawLine(pen, x, 0, x, Height); 
    } 
+0

感謝,該訣竅馬克。這兩個問題似乎都成立。只在左邊繪製一個矩形而不是在右邊時出現實線。此外,任何透支垂直部分也會顯示此行爲(只有最右邊的矩形的右邊看起來是正確的)。 由於我的代表太低,無法編輯帖子,有人可以將第一個for循環中的條件修改爲'y 2009-04-17 15:51:25