以编程方式在图像上创建可点击区域 [英] Programmatically creating a clickable area on an image
本文介绍了以编程方式在图像上创建可点击区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用代码隐藏在wpf中的图像上创建图像映射。
I'm trying to create "imagemaps" on an image in wpf using codebehind.
请参见以下XML:
<Button Type="Area">
<Point X="100" Y="100"></Point>
<Point X="100" Y="200"></Point>
<Point X="200" Y="200"></Point>
<Point X="200" Y="100"></Point>
<Point X="150" Y="150"></Point>
</Button>
我正在尝试将其转换为WPF应用程序中特定图像上的按钮。
I'm trying to translate this to a button on a certain image in my WPF app.
我已经做过一部分,但是我仍然坚持将Polygon设置为按钮的模板:
I've already did a part of this, but I'm stuck at setting the Polygon as the button's "template":
private Button GetAreaButton(XElement buttonNode)
{
// get points
PointCollection buttonPointCollection = new PointCollection();
foreach (var pointNode in buttonNode.Elements("Point"))
{
buttonPointCollection.Add(new Point((int)pointNode.Attribute("X"), (int)pointNode.Attribute("Y")));
}
// create polygon
Polygon myPolygon = new Polygon();
myPolygon.Points = buttonPointCollection;
myPolygon.Stroke = Brushes.Yellow;
myPolygon.StrokeThickness = 2;
// create button based on polygon
Button button = new Button();
?????
}
我也不确定如何在此按钮中添加/删除该按钮我的图片,但我正在调查。
I'm also unsure on how to add/remove this button to/from my image, but I'm looking into that.
我们将提供任何帮助。
推荐答案
请参见 Rob Relyea的文章,我相信它可以回答您的问题。
See this article by Rob Relyea here, I believe it answers your question.
//Create a button from scratch
Button perhapsButton = new Button();
perhapsButton.Content = "Perhaps"
perhapsButton.Click += new RoutedEventHandler(perhapsButton_Click);
container.Children.Add(perhapsButton);
考虑一下,您可以将按钮的不透明度设置为0,使其不可见。
Consider you could set the button opacity to 0 to make it invisible.
这篇关于以编程方式在图像上创建可点击区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文