C#:在 PictureBox 内创建可编辑对象
我的目标是让用户单击地图上的特定位置来添加地标,然后通过单击其图标来编辑地标(更改其名称、移动它等)。 我使用 PictureBox 来显示地图,并通过注册 MouseDoubleClick 事件,使用 GDI+ DrawImage() 方法在地图上绘制图像。问题是,绘制地标图像后,它不可编辑:用户无法单击图标并移动它、更改其名称等。我可以遵循其他设计模式吗?也许使用其他控件...?
My goal is to let the user click on a specific location on a map to add a Placemark, and then edit the placemark by click its icon (change its name, move it around, etc).
I am using a PictureBox to show the map, and by registering the MouseDoubleClick event I am drawing an Image on the map with GDI+ DrawImage() method. The problem is that after the placemark's Image was drawn, it does not editable: the user cant click the icon and move it around, change its name etc. Is there any other design pattern I can follow? maybe using other controls... ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以拥有一个对象列表,其中每个对象都是屏幕中的地标。这些对象至少有 2 个属性 X 和 Y 以及一个方法
public bool Contain(int x, int y)
表示该对象包含或不包含该点。当用户单击屏幕时,通过 foreach 循环检查每个对象是否包含鼠标位置,以找到用户想要选择它的对象。
因此,您可以更改该对象和 Invalidate() Picturebox 的属性 X 和 Y。
You can have a list of objects that each of them is a placemark in your screen. these objects have at least 2 properties X and Y and a method
public bool Contain(int x, int y)
that say you this object contains this point or no.When user clicks on the screen, by a foreach loop check that each object contains the mouse position to find the object that user wants to select it.
So you can change the properties X and Y of that object and Invalidate() Picturebox.
您需要有一个将项目分层的概念。当您将该图标放置在图像上时,您需要将其坐标/大小存储在数组中。如果用户单击该图标,您可以根据存储的坐标检测其位置,然后允许用户选择/移动它,并在执行此操作时根据您的主背景图像 + 分层图标数组重新绘制图像。
You need to have a concept of layering the items. When you place that icon on the image, you need to store its coordinates/size in an array. If the user clicks the icon, you can detect its location based on the stored coordinates and then allow the user to select/move it around, redrawing the image as they do this, based on your main background image + the layered array of icons.