所以我有一个图片框,我需要绘制矩形或声明区域,以便用户可以单击图片的某个区域并执行某些操作。我对此进行了搜索和搜索,得出的结论是,我确实需要矩形来定位或区域。如果这简化了它,我真的不希望在地图上显示可见的项目,只是一个供用户单击并执行操作的地方。
最重要的是,地图将发生变化,当它发生变化时,我需要将框更改为其他位置。 下面是我想出的,它不起作用。我也对其他方式持开放态度。
public Rectangle Location1;
public Rectangle Location2;
public String CharacterLocation == "WorldMap";
private void GenerateRegions ()
{
Pen blankPen = new Pen(Color.Transparent, 3);
if (CharacterLocation == "WorldMap")
{
Rectangle[] rects =
{
Location1 = new Rectangle(100, 200, 250, 50),
Location2 = new Rectangle(50, 100, 250, 50)
};
MapBox.CreateGraphics().DrawRectangles(blankPen, rects);
}
}
private void MapBox_MouseDown(object sender, MouseEventArgs e)
{
if (Location1.Contains(e.Location))
{
}
}
好的,这就是我的原因。它在错误的位置绘制矩形,但我假设其原因是图像被缩放。
private void GenerateRegions ()
{
MapBox.Invalidate();
Pen blankPen = new Pen(Color.Transparent, 3);
Console.WriteLine(CharacterLocation);
if (CharacterLocation == "WorldMap")
{
Console.WriteLine(CharacterLocation);
Rectangle[] rects =
{
Location1 = new Rectangle(64, 25, 20, 20),
Location2 = new Rectangle(68, 70, 20, 20)
};
using (var g = Graphics.FromImage(MapBox.Image))
{
g.DrawRectangles(blankPen, rects);
MapBox.Refresh();
}
}
private void MapBox_MouseDown(object sender, MouseEventArgs e)
{
if (Location1.Contains(e.Location))
{
arealbl.Text = "You clicked it 1";
}
if (Location2.Contains(e.Location))
{
arealbl.Text = "You clicked it 2";
}
}