扩大WinForm的窗口时,虚线矩形出现固体
题
我遇到的GDI +问题,而风俗画虚线矩形。
当窗口尺寸增加或滚动向上/向下时虚线矩形的垂直部分显示为实心的,连续的线。移动鼠标速度会更快导致越来越少的实体部分。奇怪的水平线不出现此行为和出现预期。
到目前为止两个非最佳解决方案已经设置ResizeRedraw = true
或Invalidate()
和OnResize()
期间调用OnScroll()
。我当然希望避免这是什么我真的画更复杂,这些缓慢的通话破坏流畅的体验。我也试着只有新显示区域无济于事无效 - 只有一个完整的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);
}
}
}
}
}
解决方案
我认为有两个问题:似乎存在于其中的矩形没有被正确绘制的窗口的边缘的区域;和你正在绘制在彼此的矩形,因此请速将无法正常工作。
用下面的替换您的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);
}
不隶属于 StackOverflow