2012-04-17 56 views
7

我不得不實施一個解決方案,因爲我不知道如何繼續,因爲我不太瞭解這種語言(C#)。在圖像中創建可點擊區域?

讓我解釋一下: 目標是讓東西允許用戶從一個項目(它有一個圓形)中選擇一個區域。 所以這個想法被放在一個區域上的數字圖像(所以最終它看​​起來像一個時鐘),並從用戶點擊的號碼獲得區域。

所以我的問題是:是否有可能在圖像中創建可點擊區域? (或者如果你有這個功能性的另一個解決方案,我很開明)

在此先感謝!

編輯>>這是一個WinForm應用程序,而不是一個網站。

+3

這是一個網站或WinForms應用程序? – user7116 2012-04-17 15:21:02

+0

這是一個winforms應用程序,我確實忘了提及 – BPruvost 2012-04-18 07:46:39

回答

2

非常感謝大家的回答,其中有些有趣的東西。

不幸的是,我不得不開發這個速度非常快,我剛剛創建的,我會趕上的onclick事件,然後趕區使用Cursor.Position這樣用戶點擊圖像:

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

也許不是「乾淨」的方式來做到這一點,但它的工作原理! 無論如何,謝謝!

0

調查Html imagemaps。

將花約5分鐘學習如何使用。

你可以將標籤和地圖屬性 比定義你的地圖區域和鏈接

這裏有一個快速谷歌迴應 http://www.javascriptkit.com/howto/imagemap.shtml

+0

同上,我忘了提及我正在構建一個winforms應用程序,而不是一個網站,所以這不能用:s – BPruvost 2012-04-18 07:47:45

1

雖然不是所有的流行了,you can use the <map> 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

+0

感謝您的幫助。不幸的是,我正在構建一個WinForms應用程序,沒有一個網站(因爲我忘了在第一篇文章中提到) – BPruvost 2012-04-18 07:47:20

+0

我已經更新了我的答案。 – mgnoonan 2012-04-18 14:33:43

2

你爲什麼不只是實現這樣的事情?

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 Designer中也是如此。