Programmatically creating a clickable area on an image

三世轮回 提交于 2019-12-11 14:53:49

问题


I'm trying to create "imagemaps" on an image in wpf using codebehind.

See the following 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>

I'm trying to translate this to a button on a certain image in my WPF app.

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.

Any help is appreciated.


回答1:


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);

Consider you could set the button opacity to 0 to make it invisible.



来源:https://stackoverflow.com/questions/2533040/programmatically-creating-a-clickable-area-on-an-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!