当我仅以Rectangle对象作为参数调用Invalidate方法时,我无法弄清为什么整个Control都会重绘自身。当我为2D游戏制作地图编辑器工具时,我绝对只想重绘用户绘制的任何区域。一直重画整个地图是不可行的,也不是好的编程设计,但是这种情况目前正在发生,似乎无法理解原因。
这是我编写的相关代码:
private void mapPanel_Mouseclick(object sender,MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && rbtnPaintMode.Checked)
{
mapEditorHandler.SetTileIdAtLocation((e.Location.X - 10) / 48,(e.Location.Y - 10) / 48,selectedTileId);
mapPanel.Invalidate(new Rectangle((e.Location.X - 10) / 48,48,48));
}
else if (e.Button == MouseButtons.Left && rbtnEventMode.Checked)
{
mapEditorHandler.AddGameDataObject((e.Location.X - 10) / 48,new MapEditorHandler.GameDataObject(selectedGameDataObjectCategoryId,"TODO")); //TODO selectedGameDataObjectCategoryId);
mapPanel.Invalidate(new Rectangle((e.Location.X - 10) / 48,48));
}
}
private void mapPanel_Paint(object sender,PaintEventArgs e)
{
Pen gridPen = new Pen(Color.FromArgb(255,255,255),1);int width = (int)numMapWitdh.Value;
int height = (int)numMapHeight.Value;
graphicsMapEditorPane.Clear(mapPanel.BackColor);
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
{
graphicsMapEditorPane.DrawImage(mapEditorHandler.TileImages[mapEditorHandler.GetTileIdAtLocation(x,y)],10 + (x * 48),10 + (y * 48),48);
graphicsMapEditorPane.DrawImage(mapEditorHandler.GameDataObjectImages[mapEditorHandler.GetGameDataObjectCategoryIdAtLocation(x,48);
graphicsMapEditorPane.DrawRectangle(gridPen,10 + x * 48,10 + y * 48,48);
}
}