我在自定义绘制虚线矩形时遇到了 GDI+ 问题。
当窗口大小增加或向上/向下滚动时,虚线矩形的垂直部分显示为实线、连续线。更快地移动鼠标会导致实体部分越来越少。奇怪的是,水平线没有表现出这种行为并按预期显示。
到目前为止,两个非最佳解决方案是在和期间设置ResizeRedraw = true
或调用。我当然想避免这种情况,因为我真正绘制的内容更复杂,而且这些缓慢的调用会破坏流畅的体验。我也尝试过仅使新显示的区域无效,但无济于事 - 只有完整的 Invalidate 似乎有效。Invalidate()
OnResize()
OnScroll()
关于如何解决这个问题的任何指示?
演示代码:
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);
}
}
}
}
}