2011-08-18 54 views
1

對不起,如果之前詢問過,我試圖搜索,但沒有找到任何東西。點擊圖片的一部分

我正在創建一個C#WPF應用程序。

我有一個圖像,例如一個男人。

有沒有辦法檢測到鼠標點擊了人體的哪個部位?例如,如果鼠標點擊他的手,我們會得到一個消息框:「你點了手」?

我想分成隨機形狀不只是矩形,所以鼠標點擊必須是精確的。

非常感謝您的幫助

+0

圖像是靜態還是會經常變化? – Ali

回答

3

如果我是你,我會在圖像的頂部創建多個多邊形,並得到它的正確位置/形狀,然後讓他們透明和處理單擊事件每個聚。

+0

你能告訴我怎麼做嗎? 我不是在尋找完整的代碼,只是一個簡單的例子會很棒。謝謝 – Youssef

1

您可以將圖像裁剪成單獨的部分,然後將它們加載到窗口中,就好像它是完整的圖像。這不是最有效的方法,它只適用於靜態圖像(Flash視頻對動畫「圖像」來說是完美的),但它可以快速輕鬆地實現。如果您要使用HTML,那麼該窗口必須使用Web瀏覽器工具箱項目(除非您以某種方式編寫代碼)。

2

好的,沒問題。

有了XAML這樣的:

<Border BorderBrush="Gray" 
     BorderThickness="1" 
     Height="200" Width="200" 
     MouseMove="Border_MouseMove"> 
    <TextBlock Name="Jerry" /> 
</Border> 

做這樣的事情:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Border_MouseMove(object sender, MouseEventArgs e) 
    { 
     var _Control = (sender as System.Windows.Controls.Border); 
     var _ControlLocation = _Control.PointToScreen(new Point(0, 0)); 
     var _MousePosition = MouseInfo.GetMousePosition(); 
     var _RelativeLocation = _MousePosition - _ControlLocation; 
     this.Jerry.Text = _RelativeLocation.ToString(); 
    } 
} 

internal class MouseInfo 
{ 
    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    [return: System.Runtime.InteropServices.MarshalAs(
     System.Runtime.InteropServices.UnmanagedType.Bool)] 
    internal static extern bool GetCursorPos(ref Win32Point pt); 

    [System.Runtime.InteropServices.StructLayout(
     System.Runtime.InteropServices.LayoutKind.Sequential)] 

    internal struct Win32Point 
    { 
     public Int32 X; 
     public Int32 Y; 
    }; 

    public static Point GetMousePosition() 
    { 
     Win32Point w32Mouse = new Win32Point(); 
     GetCursorPos(ref w32Mouse); 
     return new Point(w32Mouse.X, w32Mouse.Y); 
    } 
} 

我舉一個小代碼:Location of WPF control in window?顯然,要使用不同的東西比一個邊境 - 你想要的圖片。而你想要MouseDown而不是MouseMove。但這是你需要的邏輯!

爲了完成這項工作,您需要了解自定義圖像上的X和Y(比如,「head」或「arm」)。如果部分不是矩形,則需要計算多邊形。如果他們是,那麼這應該讓你90%的方式。

祝你好運!