C# 单击图像的一部分
很抱歉,如果以前有人问过这个问题,我试图搜索,但没有找到任何东西 我正在创建一个C#WPF应用程序 例如,我有一个男人的形象 有没有办法检测出鼠标点击了该男子身体的哪个部位?例如,如果鼠标点击他的手,我们会看到一个消息框:“你点击了他的手” 我想把它们分成随机形状,而不仅仅是矩形,所以鼠标点击必须精确C# 单击图像的一部分,c#,wpf,image,image-processing,C#,Wpf,Image,Image Processing,很抱歉,如果以前有人问过这个问题,我试图搜索,但没有找到任何东西 我正在创建一个C#WPF应用程序 例如,我有一个男人的形象 有没有办法检测出鼠标点击了该男子身体的哪个部位?例如,如果鼠标点击他的手,我们会看到一个消息框:“你点击了他的手” 我想把它们分成随机形状,而不仅仅是矩形,所以鼠标点击必须精确 非常感谢您的帮助如果我是您,我会在图像顶部创建多个多边形并将其放置到正确的位置/形状,然后使其透明并处理每个多边形的单击事件。如果我是您,我会在图像顶部创建多个多边形并将其放置到正确的位置/形状,
非常感谢您的帮助如果我是您,我会在图像顶部创建多个多边形并将其放置到正确的位置/形状,然后使其透明并处理每个多边形的单击事件。如果我是您,我会在图像顶部创建多个多边形并将其放置到正确的位置/形状,然后使其透明并处理每个多边形的单击事件。您可以将图像裁剪成单独的片段,然后将其加载到窗口中,就像它是一个完整的图像一样。这不是最有效的方法,它只适用于静态图像(flash视频非常适合动画“图像”),但它可以快速轻松地实现。如果要使用HTML,窗口将必须使用web浏览器工具箱项(除非您以某种方式编写自己的工具箱项)。您可以将图像裁剪成单独的片段,然后将其加载到窗口中,就像它是一个完整的图像一样。这不是最有效的方法,它只适用于静态图像(flash视频非常适合动画“图像”),但它可以快速轻松地实现。如果要使用HTML,窗口将必须使用web浏览器工具箱项(除非您自己编写了代码)。当然,没问题 使用如下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);
}
}
我从中提取了一个小代码:很明显,你想要使用不同于边框的东西——你想要一个图像。你想要鼠标下而不是鼠标移动。但这就是你需要的逻辑
要实现这一点,您需要知道自定义图像上的X和Y(例如,“头部”或“手臂”的位置)。如果截面不是矩形,则需要计算多边形。如果他们是,那么这应该让你的方式90%
祝你好运 当然,没问题
使用如下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);
}
}
我从中提取了一个小代码:很明显,你想要使用不同于边框的东西——你想要一个图像。你想要鼠标下而不是鼠标移动。但这就是你需要的逻辑
要实现这一点,您需要知道自定义图像上的X和Y(例如,“头部”或“手臂”的位置)。如果截面不是矩形,则需要计算多边形。如果他们是,那么这应该让你的方式90%
祝你好运 图像是静态的还是经常变化的?图像是静态的还是经常变化的?你能告诉我怎么做吗?我不是在寻找完整的代码,只要一个简单的例子就好了。谢谢你能告诉我怎么做吗?我不是在寻找完整的代码,只要一个简单的例子就好了。非常感谢。