C#:在 PictureBox 内创建可编辑对象

发布于 2024-08-29 02:23:18 字数 200 浏览 5 评论 0原文

我的目标是让用户单击地图上的特定位置来添加地标,然后通过单击其图标来编辑地标(更改其名称、移动它等)。 我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清眉祭 2024-09-05 02:23:18

您可以拥有一个对象列表,其中每个对象都是屏幕中的地标。这些对象至少有 2 个属性 X 和 Y 以及一个方法 public bool Contain(int x, int y) 表示该对象包含或不包含该点。

 public class placemark
 {
      public int X;
      public int Y;

      public bool Contain(int x, int y)
      {
           // some logic here
           return true;
      }
 }

当用户单击屏幕时,通过 foreach 循环检查每个对象是否包含鼠标位置,以找到用户想要选择它的对象。

 foreach(var placemark in placeMarkList)
 {
      if (placemark.Contain(e.x,e.y))
      {
           placemark.X+=e.x-oldx;
           placemark.X+=e.y-oldy;
      }
 }

因此,您可以更改该对象和 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.

 public class placemark
 {
      public int X;
      public int Y;

      public bool Contain(int x, int y)
      {
           // some logic here
           return true;
      }
 }

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.

 foreach(var placemark in placeMarkList)
 {
      if (placemark.Contain(e.x,e.y))
      {
           placemark.X+=e.x-oldx;
           placemark.X+=e.y-oldy;
      }
 }

So you can change the properties X and Y of that object and Invalidate() Picturebox.

就此别过 2024-09-05 02:23:18

您需要有一个将项目分层的概念。当您将该图标放置在图像上时,您需要将其坐标/大小存储在数组中。如果用户单击该图标,您可以根据存储的坐标检测其位置,然后允许用户选择/移动它,并在执行此操作时根据您的主背景图像 + 分层图标数组重新绘制图像。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文