这是关于我在同一个项目中遇到的问题的后续问题:在我的图形方法中(我使用它作为更新方法)我正在绘制一个地图图像,我已经在本地存储在调试文件夹中. 然而,当点击地图上的某个区域时,地图应该消失并被 3 张图像取代。但是我遇到的问题是,实际上一次只绘制一张图像(最后一张)。无论如何,这是我的图形方法:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
//load the map image
Image map = Image.FromFile("map.png");
SolidBrush b = new SolidBrush(Color.Red);
//now I need to add the paint controls for is hit and not hit
if (isHit == true && goBack == false)
{
g.DrawImage(backButton, new Point(0, 0));
for (int i = 0; i < trees.Length; i++)
{
if (trees[i].isHit == true)
{
Tree t = trees[i];
Image treeImage1, treeImage2;
treeImage1 = Image.FromFile(t.EnglishName + ".png");
treeImage2 = Image.FromFile(t.EnglishName + "leaf.png");
g.DrawImage(treeImage1, new Point(10,20);
g.DrawImage(treeImage2, new Point((treeImage1.Width + 10), 20);
loadTree(trees[i]);
}
}
//update labels for text and info
// draw a back button(add control to click, use image)
}
else if(isHit == false)
{
g.DrawImage(backButton, new Point(0, 0));
g.DrawImage(map, new Point(0, 0));
for (int i = 0; i < trees.Length; i++)
{
g.FillRectangle(b, trees[i].X, trees[i].Y, 3, 3);
}
}
this.Refresh();
}
到目前为止我发现的一个问题是,只要语句为真(由于某种未知原因),就永远不会到达第一个 if 语句。这些变量在我的鼠标单击事件中控制如下:
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
mouseX = e.X;
mouseY = e.Y;
bool isClicked = false;
for (int i = 0; i < trees.Length; i++)
{
isClicked = trees[i].checkClick(mouseX, mouseY);
if (isClicked)
{
isHit = isClicked;
if(mouseX >= 5 && mouseX <= backButton.Width)
{
if(mouseY >= 5 & mouseY <= backButton.Height)
{
goBack = true;
trees[i].isHit = false;
isHit = false;
HideLabels();
}
}
for (int j = 0; j < trees.Length; j++)
{
if (trees[j] != trees[i])
{
trees[j].isHit = false;
}
}
trees[i].isHit = true;
loadTree(trees[i]);
}
}
}
任何和所有的帮助将不胜感激。如果您需要更多信息,请告诉我,我会尽快发布谢谢 :)