问题描述
我怎么做下面的我不要求特定的代码,但我需要一些方向我已经伤透这个我的大脑几个星期。只要我想打一个地图的如的。美国每个州是一个不同的图像或区域,可以将鼠标悬停和点击。我试着用PNG和透明胶片打不过我在一个死胡同。更长远的目标,我想在每个州拖动与各州首府的标签并将其拖放到那里再有如果标签/资本状况是否正确否则不是。
How do I do the following I'm not asking for specific code but I need some direction as I have been racking my brains on this for weeks. Simply I want to make a map of the eg. united states and each state is a different picture or area that can be moused over and clicked. I tried playing with png and transparencies but I'm at a dead end. More ambitiously I'd like to drag labels with state capitals over each state and drop them there then have a process where if the label/capital matches the state it correct else it's not.
我试过GIS(?)我想这样做,C#,但我不能让牵引如何做到这一点至今。任何人都可以帮忙吗?这是在C#中太困难了?我应该使用另一种方法?请有什么办法
I've tried GIS(?) I want to do this C# but I can't get traction how to do it so far. Can anyone help? Is this too difficult in C#? SHould i be using another approach? Please what is the approach?
推荐答案
好消息第一:编程部分不那么辛苦,甚至好老的WinForms你可在几行的核心内容检查。该可点击区域不能在按钮的WinForms不过。
The good news first: The programming part is not so hard and even with good old Winforms you can do the core checks in a few lines. The clickable areas can't be Buttons in Winforms, however.
下面有两种解决方案:
解决方案1 :您可以定义一个列表,区域,称为地区和测试,如果一钻进点击
Solution 1 : You can define a list of areas, called regions and test if one got clicked.
下面是一个开始:我定义了一个很简单的矩形区域
和一个不是那么简单的多边形区域
和测试每个点击如果被击中。如果被击中我的输出它的数据到窗体的标题文字:
Here is a start: I define a very simple Rectangle Region
and a not so simple Polygon Region
and test on each click if one was hit. If it was hit I output its data into the Form's title text:
//..
using System.Drawing.Drawing2D;
//..
public Form1()
{
InitializeComponent();
// most real states don't look like rectangles
Region r = new Region(new Rectangle(0, 0, 99, 99));
regions.Add(r);
List<int> coords = new List<int>() {23,137, 76,151, 61,203,
117,283, 115,289, 124,303, 112,329, 76,325, 34,279, 11,162};
List<Point> points = new List<Point>();
for (int i = 0; i < coords.Count ; i += 2)
points.Add(new Point(coords[i], coords[i+1]));
byte[] fillmodes = new byte[points.Count];
for (int i = 0 ; i < fillmodes.Length; i++) fillmodes[i] = 1;
GraphicsPath GP = new GraphicsPath(points.ToArray(), fillmodes);
regions.Add(r);
}
List<Region> regions = new List<Region>();
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
using (Graphics G = pictureBox1.CreateGraphics() )
foreach(Region r in regions)
{
Region ri = r.Clone();
ri.Intersect(new Rectangle(e.X, e.Y, 1, 1));
if (!ri.IsEmpty(G)) // hurray, we got a hit
{ this.Text = r.GetBounds(G).ToString(); break; }
}
}
在测试PROGRAMM的地区是不可见的。测试你可能要添加的东西是这样的:
The regions in the test programm are not visible. For testing you may want to add something like this:
private void button1_Click(object sender, EventArgs e)
{ // paint a non-persistent filling
using (Graphics G = pictureBox1.CreateGraphics())
foreach (Region r in regions)
{ G.FillRegion(Brushes.Red, r); }
}
但是,现在的坏消息:您可以定义的区域为复杂的多边形看起来像真正的状态,但就是大量工作STRONG>!有现成的影像地图编辑的那里在网络上制作网站点击地图。你最好的办法可能是使用其中的一个来创建所有的多边形,然后将HTML输出转换为点的列表,你可以阅读到你的程序。你甚至可以得到幸运,找到现成图像映射为美国,但我没有看过..
But now for the bad news: You can define regions to be complicated polygons that look like real states, however that is a lot of work! There are ready-made image map editors out there in the web for making clickable maps on websites. Your best course might be to use one of these to create all those polygons and then convert the html output to a list of points you can read into your program. You may even get lucky and find ready image maps for the USA, but I haven't looked..
更新:有是许多图像的美国地图那里。一是即使在。转换这些坐标,以适应你的地图将是一个任务,但很多比从头创建它们更容易,海事组织。我已经改变了代码,包括来自维基百科来源cordinates的一个列表。猜猜状态!
Update: There are many image maps of the USA out there. One is even on wikipedia. Converting these coordinates to fit your map will be a task but lot easier than creating them from scratch, imo. I have changed the code to include one list of cordinates from the wikipedia source. Guess the state!
我implmented一个程序,让你点击的状态,并在78行代码显示姓名,但不包括在50个州的文本文件。
I have implmented a program that lets you click on a state and displays the name in 78 lines of code, excluding the text file with the 50 states.
解决方案2
而不是多边形的列表,你可以准备彩色地图,并使用颜色键进入词典<颜色,串>
或词典<颜色,将stateInfo>
。你必须确保每个国家都有一个唯一颜色和图像不压缩为JPEG格式,这将引入文物和陷入困境的键映射。
Instead of a list of polygons you can prepare a colored map and use the colors as keys into a Dictionary<Color, string>
or Dictionary<Color, StateInfo>
. You must make sure that each state has one unique color and that the image is not compressed as jpeg, which would introduce artifacts and mess up the key mapping.
接下来,您映射每个颜色的相关信息;这才是真正的工作,因为你必须知道这些国家创造词典; - )
Next you map each color to the related info; this is the real work because you must know those states to create the Dictionary ;-)
最后,你可以看一下点击的颜色在字典已创建的:
Finally you can look up the clicked color in the Dictionary you have created:
Color c = ((Bitmap) pictureBox1.Image).GetPixel(e.X, e.Y)
string StateName = stateDictionary[c];
如果您使用的是类或结构是可以在字典你的价值,你可以包括资本等。
If you are using a class or struct as your value in the dictionary you can include the capital etc..
您可以放大,但必须比例点击相应的位置;如果您您甚至可以显示一个地域形象查找键颜色未在图片框,但一种无形的颜色代码位图。
所有的一切更简单的解决方案IMO ..你挑
You can zoom but must scale the click location accordingly; you can even display a geographical Image if you look up the key color not in the PictureBox but in an invisible color code bitmap.All in all an even simpler solution imo.. your pick!
一旦你明白我的工作,你可以通过拖放放玩;!下降。在这里,你会想测试上的DragDrop或者对鼠标松开看到你已经下降了标签。
Once you got that working, you can play with drag&drop. Here you'll want to test on DragDrop or maybe on Mouseup to see where you have dropped the Label.
这是一个很好的项目,非常值得创建一些类使事情变得更容易,更可扩展..!
This is a nice project and well worth of creating a few classes to make things easier and more expandable..!
对于真正的按钮,你可能会得到幸运与WPF。
For real Buttons you may get lucky with WPF.
这篇关于制作地图的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!