以编程方式在图像上创建可单击区域

时间:2010-03-28 13:25:57

标签: wpf code-behind imagemap

我正在尝试使用代码隐藏在wpf中的图像上创建“图像地图”。

请参阅以下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应用中某个图片上的按钮。

我已经做了一部分,但我坚持将Polygon设置为按钮的“模板”:

    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();
        ?????
    }

我也不确定如何向我/我的图像添加/删除此按钮,但我正在研究它。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

请参阅此article by Rob Relyea here,我相信它会回答您的问题。

 //Create a button from scratch
        Button perhapsButton = new Button();
        perhapsButton.Content = "Perhaps"
        perhapsButton.Click += new RoutedEventHandler(perhapsButton_Click);
        container.Children.Add(perhapsButton);

考虑您可以将按钮不透明度设置为0以使其不可见。

相关问题