WPF 빗금 만들기

2024. 4. 13. 15:58C# WPF

 

WPF에서 위와 같이 사각형을 만들고 그 안에 빗금을 만드는 코드

 


    <Grid>
        <!-- Path로 만들어진 사각형 -->
        <Path Stroke="Black" StrokeThickness="2" Margin="258,83,204,166" Stretch="Fill" >
            <Path.Data>
                <!-- 사각형의 Path 데이터 -->
                <RectangleGeometry Rect="0,0,200,200"/>
            </Path.Data>
            <!-- VisualBrush를 사용한 빗금 모양 -->
            <Path.Fill>
                <VisualBrush TileMode="Tile" Viewport="0,0,10,10" ViewportUnits="Absolute" Viewbox="0,0,10,10" ViewboxUnits="Absolute">
                    <VisualBrush.Visual>
                        <!-- 빗금을 그리는 Path -->
                        <Path Data="M 0 0 L 10 10" Stroke="Black" StrokeThickness="1"/>
                    </VisualBrush.Visual>
                </VisualBrush>
            </Path.Fill>
        </Path>
    </Grid>

위와 같이 그리드 안에 Path를 이용해 사각형을 만들고 

VisualBrush를 이용해 빗금으로 추가하는 형식이다.

 

 

 

위를 cs로 만들고 싶으면 아래와 같다

점들을 List<Point>로 받아와 폴리곤을 만든 후 빗금으로 채우는 코드이다.

 

        public Path CreatePath(List<Point> localPath, bool addBlurEffect)
        {
            // Create a StreamGeometry to use to specify myPath.
            StreamGeometry geometry = new StreamGeometry();
            using (StreamGeometryContext ctx = geometry.Open())
            {
                ctx.BeginFigure(localPath[0], true, true);
                // Draw a line to the next specified point.
                ctx.PolyLineTo(localPath, true, true);
            }

            // Freeze the geometry (make it unmodifiable)
            // for additional performance benefits.
            geometry.Freeze();
            // Create a path to draw a geometry with.
            Path myPath = new Path();
            {
                // Specify the shape of the Path using the StreamGeometry.
                myPath.Data = geometry;


                myPath.Stroke = StrokeColor;
                myPath.StrokeThickness = StrokeThickness;
                myPath.StrokeLineJoin = PenLineJoin.Round;
                myPath.StrokeStartLineCap = PenLineCap.Triangle;
                myPath.StrokeEndLineCap = PenLineCap.Square;
                myPath.Fill = FillColor;
                myPath.Opacity = Opacity;
                myPath.IsHitTestVisible = false;
            }


        //여기까지는 사각형을 만드는 코드

 

      아래는 빗금을 만드는 코드
            if (Ishatch)
            {
                Path path = new Path
                {
                    Data = Geometry.Parse("M 0 0 L 10 10"),
                    Stroke = StrokeColor,
                    StrokeThickness = 1
                };

                // VisualBrush 생성 및 설정
                VisualBrush visualBrush = new VisualBrush
                {
                    TileMode = TileMode.Tile,
                    Viewport = new Rect(0, 0, 10, 10),
                    ViewportUnits = BrushMappingMode.Absolute,
                    Viewbox = new Rect(0, 0, 10, 10),
                    ViewboxUnits = BrushMappingMode.Absolute,
                    Visual = path
                };

                // myPath에 VisualBrush 적용 여기서 사각형 안에 채운다.
                myPath.Fill = visualBrush;
            }


            return myPath;
        }

'C# WPF' 카테고리의 다른 글

xml 오류  (0) 2024.05.23
람다식  (0) 2023.06.23
Delegate(인강)  (0) 2023.06.20
ICommand 사용방법  (0) 2023.06.20
Command & Data Binding  (0) 2023.06.20