我想在图片框中画一些东西并删除它们。在这种情况下,我需要绘制一个填充圆,一段时间后将其移除并绘制一个不填充的圆。
我使用下面的代码,但是当我想删除一个形状时,我需要用新的形状再次绘制图片框,这会导致图片框发生敏感的变化。现在我知道我应该使用 invalidate() 方法。但我不知道我应该在哪里以及如何使用它。
void pbmapDo()
{
Graphics graphicPBMap = pbMap.CreateGraphics();
// usually Values : gridNeedUpdate = true; rulersNeedUpdate = true; rulerNeedUpdate = true; backGroundNeedUpdate = true; nodesNeedUpdate = true;
if (backGroundNeedUpdate)
{
Bitmap srce = new Bitmap(BackGround);
Bitmap dest = new Bitmap(pbMap.Width, pbMap.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
Graphics gr = Graphics.FromImage(dest);
gr.DrawImage(srce, new Rectangle(Point.Empty, dest.Size));
graphicPBMap.DrawImage(dest, 0, 0);
}
backGroundNeedUpdate = false;
if (isGridShow && gridNeedUpdate)
{
for (int i = 0; i < pbMap.Width / 60 + 1; i++)
{
graphicPBMap.DrawLine(gridPen, pbMap.Width - i * 60, 0, pbMap.Width - i * 60, pbMap.Height);
}
for (int i = 0; i < pbMap.Height / 60 + 1; i++)
{
graphicPBMap.DrawLine(gridPen, pbMap.Width, i * 60, 0, i * 60);
}
}
gridNeedUpdate = false;
if (isShowRulers && rulersNeedUpdate )
{
graphicPBMap.DrawLine(new Pen(Brushes.Black, 46), pbMap.Width - 1, pbMap.Height - 1, 0 - 1, pbMap.Height - 1);
graphicPBMap.DrawLine(new Pen(Brushes.Black, 49), 0, 0, 0, pbMap.Height);
for (int i = 0; i < pbMap.Width / 60 + 1; i++)
{
graphicPBMap.DrawString(XPixelToLong((double)pbMap.Width - i * 60).ToString(), new Font("Tahoma", 7), Brushes.White, pbMap.Width - i * 60, pbMap.Height - 15);
graphicPBMap.DrawLine(gridPen, pbMap.Width - i * 60, pbMap.Height - 24, pbMap.Width - i * 60, pbMap.Height);
}
for (int i = 0; i < pbMap.Height / 60 + 1; i++)
{
graphicPBMap.DrawLine(gridPen, 0, i * 60, 25, i * 60);
graphicPBMap.DrawString(YPixelToLat(i * 60).ToString(), new Font("Tahoma", 7), Brushes.White, 0, i * 60);
}
}
rulersNeedUpdate = false;
if (rulerNeedUpdate)
{
if (x0ruler != -1 && y0ruler != -1)
{
if (x1ruler != -1 && y1ruler != -1)
{
Rectangle rectAngle = new Rectangle((int)(x1ruler - 1), (int)(y1ruler - 1), 2, 2);
graphicPBMap.DrawEllipse(rulerPen, rectAngle);
graphicPBMap.DrawLine(rulerPen, x0ruler, y0ruler, x1ruler, y1ruler);
rectAngle = new Rectangle((int)(x0ruler - 1), (int)(y0ruler - 1), 2, 2);
graphicPBMap.DrawEllipse(rulerPen, rectAngle);
}
else
{
Rectangle rectAngle = new Rectangle((int)(x0ruler - 1), (int)(y0ruler - 1), 2, 2);
graphicPBMap.DrawEllipse(rulerPen, rectAngle);
}
}
}
rulerNeedUpdate = false;
if (nodesNeedUpdate)
{
nodesNeedUpdate = false;
Node node;
for (int i = 0; i < nodes.Count; i++)
{
node = (Node)(nodes[i]);
if (node.IsOn)
{
drawOnCircle(graphicPBMap,node.Longitude, node.Latitude, node.RInMeter, node.IsSelected);
}
else
{
drawOffCircle(graphicPBMap, node.Longitude, node.Latitude, node.RInMeter, node.IsSelected);
}
}
}
}
编辑 1:
我改变了一次代码。