【问题标题】:Create clickable zones in image?在图像中创建可点击区域?
【发布时间】:2012-06-13 23:04:54
【问题描述】:

我必须实现一个解决方案,因为我不太了解这种语言 (C#),所以我不太确定如何进行。

让我解释一下: 目标是让用户从项目(具有圆形形式)中选择区域。 所以想法是在区域上放一个带有数字的图像(所以最后它看起来像一个时钟),并从用户点击的数字中获取区域。

所以我的问题是:是否可以在图像中创建可点击区域? (或者如果您对此功能有其他解决方案,我很开放)

提前致谢!

EDIT >> 这是一个 WinForm 应用程序,而不是一个网站。

【问题讨论】:

  • 这是一个网站还是 WinForms 应用程序?
  • 这是一个winforms应用,我确实忘了说

标签: c# winforms image


【解决方案1】:

你为什么不直接实现这样的东西?

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

public class Zone
{
    public Region Region { get; private set; }
    public Action<MouseEventArgs> Click { get; private set; }

    public Zone(Region zoneRegion, Action<MouseEventArgs> click)
    {
        this.Region = zoneRegion;
        this.Click = click;
    }
}

public class PictureBoxWithClickableZones : PictureBox
{
    private readonly List<Zone> zones = new List<Zone>();

    public void AddZone(Zone zone)
    {
        if (zone == null)
            throw new ArgumentNullException("zone");

        this.zones.Add(zone);
    }

    protected override void OnMouseClick(MouseEventArgs e)
    {
        base.OnMouseClick(e);

        foreach (var zone in this.zones.Where(zone => zone.Region.IsVisible(e.Location)))
            zone.Click(e);
    }
}

如果您有足够的时间,您甚至可以在 Visual Studio 的 WinForms 设计器中制作并使用适当的组件。

【讨论】:

    【解决方案2】:

    谢谢大家的回答,里面有很多有趣的东西。

    不幸的是,由于我必须非常快速地开发它,我刚刚创建了一个图像,我将在该图像上捕捉 onclick 事件,然后使用 Cursor.Position 捕捉用户点击的区域,如下所示:

    int X = pictureBox.PointToClient(Cursor.Position).X;
    int Y = pictureBox.PointToClient(Cursor.Position).Y;
    

    也许不是“更清洁”的方式,但它有效! 还是谢谢!

    【讨论】:

      【解决方案3】:

      虽然它不再那么流行了,you can use the &lt;map&gt; tag 还是这样做的。

      <img src="planets.gif" width="145" height="126" alt="Planets" usemap="#planetmap" />
      
      <map name="planetmap">
        <area shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun" />
        <area shape="circle" coords="90,58,3" href="mercur.htm" alt="Mercury" />
        <area shape="circle" coords="124,58,8" href="venus.htm" alt="Venus" />
      </map> 
      

      编辑:

      由于这需要 WinForms 解决方案,或许本文可以帮助您。

      C# Windows Forms ImageMap Control

      【讨论】:

      • 感谢您的帮助。不幸的是,我正在构建一个 WinForms 应用程序,没有网站(正如我在第一篇文章中忘记提到的)
      【解决方案4】:

      查看 Html 图像映射。

      大约需要 5 分钟来学习如何使用 is。

      在标签中你可以应用和映射属性 比定义你的地图区域和链接

      这是一个快速的谷歌响应 http://www.javascriptkit.com/howto/imagemap.shtml

      【讨论】:

      • 同上,忘了说我是在建winforms应用,不是网站,所以这个不能用:s
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      • 2015-12-01
      • 2014-07-12
      • 2023-01-05
      • 1970-01-01
      相关资源
      最近更新 更多