【问题标题】:Click on image and show message box depending on the clicked place单击图像并根据单击的位置显示消息框
【发布时间】:2016-06-02 04:51:22
【问题描述】:

我对这张世界地图图像有这个想法,当我点击一个国家/地区时,它会在MessageBox 中显示该国家/地区的信息,例如,有人知道怎么做吗?

我有一个矩形和一个按钮,当我单击按钮时,它会在矩形中显示图像,但我想如果我使用多边形来塑造国家的形状,但我有点卡住了。

我想让每个国家都分开,也许点击时边框会亮起

【问题讨论】:

标签: c# wpf


【解决方案1】:

您可以使用 WPF 轻松做到这一点:

  1. 查找精美的 SVG 格式世界地图。我使用了来自维基百科的this one

  1. 下载并安装Inkscape,然后打开您刚刚下载的SVG。 Inkscape 有一个很好的功能,可以将 SVG 保存为 XAML。

  1. 将 XAML 文件中的 ViewBox 导入 WPF 窗口/等:

  1. 对于 XAML 中的每个 Path,您可以添加一个 MouseEnter/MouseLeave 事件处理程序,或者您可以为所有 Paths 使用相同的事件处理程序

示例代码:

 private void CountryMouseEnter(object sender, MouseEventArgs e)
 {
     var path = sender as Path;
     if (path != null)
     {
         path.Fill = new SolidColorBrush(Colors.Aqua);
     }
 }

 private void Country_MouseLeave(object sender, MouseEventArgs e)
 {
     var path = sender as Path;
     if (path != null)
     {
         path.Fill = new SolidColorBrush(Colors.Black);
     }
 }
  1. 现在只需更改颜色/显示MessageBoxes 等即可。

GitHub

【讨论】:

  • 老兄,非常感谢,这是我使用 stackoverflow 以来看到的最好的答案
  • @Sanscull 很高兴你喜欢它。快乐编码:)
【解决方案2】:

我的第一个想法:您可以将命令绑定到将由单击位置触发的视图。如果您使用的是 WPF,则可以将命令参数绑定到命令以获取单击的 x 和 y... 之后,您必须根据 xy 位置处理消息框的内容和边框的突出显示。

玩得开心:D

【讨论】:

    【解决方案3】:

    选项 1

    有人创建的 Code Project 上有一个项目,它定义了可通过事件点击的热点。您可以使用它来覆盖您的地图并定义您需要的热点。

    C# Windows Forms ImageMap Control

    选项 2

    您可以绑定到图像上的MouseUp 事件并使用以下内容查看它是否在Rectangle

    private void mapMouseUp(object sender, MouseEventArgs e) {
        Rectangle rect1 = new Rectangle(0, 0, 100, 100);
        Rectangle rect2 = new Rectangle(0, 100, 100, 100);
        if (rect1.Contains(e.Location)) {
            // Do your stuff
        } else if (rect2.Contains(e.Location)) {
            // Do your stuff
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多