WPF中六边形填充颜色 [英] Filling Color in a hexagon in WPF

查看:67
本文介绍了WPF中六边形填充颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用此代码填充六边形时遇到问题,当此代码运行时,它只绘制白色"六边形的轮廓,我想用颜色填充六边形,但它不起作用.

I'm having problem in filling the hexagon using this code, when this code runs it draws only the outline of the hexagon that is "White", I want to fill the hexagon with a color but it is not working.

我搜索了很多并尝试了很多东西,例如 drawingContext.Drawing()drawingBrush

I have searched a lot and tried many things like drawingContext.Drawing() , drawingBrush, etc.

我在这段代码中遗漏了什么吗?这是代码:

Am I missing something in this code? This is the code:

public void DrawHexagon(DrawingContext drawingContext)
{
   GeometryGroup hexaKey = new GeometryGroup();

        //making lines for hexagon 
        hexaKey.Children.Add(
           new LineGeometry(new Point(X1, Y1), new Point(X2, Y2)));

        hexaKey.Children.Add(
            new LineGeometry(new Point(X2, Y2), new Point(X3, Y3)));

        hexaKey.Children.Add(
            new LineGeometry(new Point(X3, Y3), new Point(X4, Y4)));

        hexaKey.Children.Add(
            new LineGeometry(new Point(X4, Y4), new Point(X5, Y5)));

        hexaKey.Children.Add(
            new LineGeometry(new Point(X5, Y5), new Point(X6, Y6)));

        hexaKey.Children.Add(
            new LineGeometry(new Point(X6, Y6), new Point(X1, Y1)));

        //
        // Create a GeometryDrawing.
        //
        GeometryDrawing hexaKeyDrawing = new GeometryDrawing();
        hexaKeyDrawing.Geometry = hexaKey;

        // Paint the drawing with a gradient.
        hexaKeyDrawing.Brush =new SolidColorBrush(Colors.Red);


        // Outline the drawing with a solid color.
        hexaKeyDrawing.Pen = new Pen(Brushes.White, 2);


        drawingContext.DrawGeometry(hexaKeyDrawing.Brush, hexaKeyDrawing.Pen, hexaKeyDrawing.Geometry);

}

推荐答案

LineGeometry 无法填充……它们只是线条.你需要一条路.MSDN 有一个例子

LineGeometry doesn't have a way to fill... they're just lines. You need a path. The MSDN has an example

这篇关于WPF中六边形填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆