Skip to main content
 首页 » 编程设计

C# Winforms 使用 Drawline 方法更新屏幕超慢

2024年02月23日19txw1958

我已经在网上浏览了这方面的内容,但没有找到我正在寻找的确切答案,或者我已经尝试了建议的内容,但它不起作用!

我遇到的问题是我有一个屏幕,它在一个矩阵中有大约 72 个复选框,我使用我存储在列表中的坐标的线连接在一起。

为了绘制线条,我在 OnPaint 的重写方法中使用 Drawline 方法来循环访问列表,如下所示:-

protected override void OnPaint(PaintEventArgs e) 
    { 
        Pen myPen = new Pen(System.Drawing.Color.Black); 
        Graphics g = this.CreateGraphics(); 
        myPen.Width = 5; 
 
       foreach(ConnectionLine cl in connectionLines) 
       { 
        g.DrawLine(myPen, cl.xStart, cl.yStart, cl.xStop, cl.yStop); 
       } 
 
        myPen.Dispose(); 
        g.Dispose(); 
 
    } 

奇怪的是,它似乎不是花时间绘制的线条 - 现在是复选框,如果我删除线条功能,这些会在眨眼间刷新。

非常感谢任何想法。

谢谢,

戴夫

请您参考如下方法:

部分问题可能是每次绘制控件时都在重新创建 Graphics 对象。相反,您应该使用 PaintEventArgs 中提供的 e.Graphics 对象。您也可以尝试只使用一个 Pen 实例。

 private readonly Pen _myPen = new Pen(System.Drawing.Color.Black) {Width = 5}; 
 protected override void OnPaint(PaintEventArgs e) 
 { 
     foreach (var cl in connectionLines) 
         e.Graphics.DrawLine(_myPen, cl.xStart, cl.yStart, cl.xStop, cl.yStop); 
 }