WPF의 붓 기능으로 직선, 구부러진 실선, 직선 점선, 구부러진 점선을 실현한다

6774 단어 WPF
1. InkCanvas 클래스.
화판을 구현하려면 InkCanvas가 필요합니다.일반적으로 어떤 코드도 필요 없이 그 위에 선을 그을 수 있다.
펜 색상, 펜촉 크기 등을 설정하려면 DefaultDrawingAttributes를 설정해야 합니다. 예를 들어 다음과 같습니다.
DrawingAttributes attributes = new DrawingAttributes();

attributes.Color = Colors.Black;

attributes.Height = 50;

attributes.Width = 50;

attributes.FitToCurve = true;


2. InkCanvas에 선을 그립니다
방법: StrokeCollected 이벤트에서 수정하고, StrokeCollected 이벤트는 한 획이 끝난 후에 터치합니다.획의 시작점(BeginPoint)과 끝점(EndPoint)을 제거한 다음 두 점을 사용하여 새 획을 만들 수 있습니다.
코드는 다음과 같습니다.
 private void InkCanvas_StrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)

        {

            if (e.Stroke.StylusPoints.Count > 1)

            {

                UpdateLine(e.Stroke);

            }

        }

private void UpdateLine(Stroke currentStroke)

        {

            StylusPoint beginPoint = currentStroke.StylusPoints[0];//   

            StylusPoint endPoint = currentStroke.StylusPoints.Last();//  

            packageCanvas.Strokes.Remove(currentStroke);//       

            List<Point> pointList = new List<Point>();

            pointList.Add(new Point(beginPoint.X, beginPoint.Y));

            pointList.Add(new Point(endPoint.X, endPoint.Y));

            StylusPointCollection point = new StylusPointCollection(pointList);

            Stroke stroke = new Stroke(point);//       

            stroke.DrawingAttributes = packageCanvas.DefaultDrawingAttributes.Clone();

            InkCanvas.Strokes.Add(stroke);           

}


3. InkCanvas에 직선 점선 그리기
방법: 위와 같이 시작점과 끝점을 취하고, 다른 점은 두 점 사이에 많은 점을 그린 다음에 서로 인접한 두 점을 하나의 획으로 연결하는 것이다.이렇게 직선선이 좋아졌다.
코드는 다음과 같습니다.
private void UpdateLine(Stroke currentStroke)

{

      InkCanvas.Strokes.Remove(currentStroke);//      

                int dotTime = 0;
                int intervalLen=6;//  

                double lineLen = Math.Sqrt(Math.Pow(beginPoint.X - endPoint.X, 2) + Math.Pow(beginPoint.Y - endPoint.Y, 2));//    

                Point currentPoint = new Point(beginPoint.X, beginPoint.Y);

                double relativaRate = Math.Abs(endPoint.Y - beginPoint.Y) * 1.0 / Math.Abs(endPoint.X - beginPoint.X);

                double angle = Math.Atan(relativaRate) * 180 / Math.PI;//       ,      

                int xOrientation = endPoint.X > beginPoint.X ? 1 : -1;//       X   

                int yOrientation = endPoint.Y > beginPoint.Y ? 1 : -1;

                if (lineLen < intervalLen)

                {

                    return;

                }

                while (dotTime * intervalLen < lineLen)

                {

                    double x = currentPoint.X + dotTime * intervalLen * Math.Cos(angle * Math.PI / 180) * xOrientation;

                    double y = currentPoint.Y + dotTime * intervalLen * Math.Sin(angle * Math.PI / 180) * yOrientation; 

                    List<Point> pL = new List<Point>();

                    pL.Add(new Point(x, y));

                    x += intervalLen * Math.Cos(angle * Math.PI / 180) * xOrientation;

                    y += intervalLen * Math.Sin(angle * Math.PI / 180) * yOrientation;

                    pL.Add(new Point(x, y));

                    StylusPointCollection spc = new StylusPointCollection(pL);//          

                    Stroke stroke = new Stroke(spc);

                    stroke.DrawingAttributes = packageCanvas.DefaultDrawingAttributes.Clone();

                    InkCanvas.Strokes.Add(stroke);

                    dotTime+=2;

                }

}


4. InkCanvas에 점선을 그립니다
방법: InkCanvas에서StrokeCollected 메서드의 매개변수 속성에서 선의 점 좌표를 사용할 수 있습니다.
2)StylusPointCollection collection=currentStroke 생성현재 획의 모든 점을 저장하는 StylusPoints
2) 최종 생성 지점을 저장할 List allPointList 생성
2) 시작점 = allPointList, 시작점 - currentPoint
3)collection, IF currentPoint와 찾기점(item)의 거리 = = 보폭THEN item=>allPointList;item=>currentPoint, 다음 점
IF currentPoint와 찾기점(item)의 거리 > 보폭THEN은currentPoint와 item 라인에서 점을 찾고currentPoint와의 거리 = 보폭, item=>allPointList;item=>currentPoint, 현재 점 계속
IF currentPoint와 찾은 점(item)의 거리 <스텝에서 다음 점 가져오기
코드는 다음과 같습니다.
private void UpdateLine(Stroke currentStroke)

{

InkCanvas.Strokes.Remove(currentStroke);

                StylusPointCollection collection = currentStroke.StylusPoints;

                List<Point> allSelectedPoint = new List<Point>();

                Point currentPoint = new Point(collection[0].X, collection[0].Y);

                allSelectedPoint.Add(currentPoint);

                for (int i = 0; i < collection.Count; i++)

                {

                    var item = collection[i];

                    double length = Math.Sqrt(Math.Pow(item.X - currentPoint.X, 2) + Math.Pow(item.Y - currentPoint.Y, 2));

                    if ((int)(length + 0.5) == (int)intervalLen || length == intervalLen)

                    {



                        currentPoint = new Point(item.X, item.Y);

                        allSelectedPoint.Add(currentPoint);

                    }

                    else if (length > intervalLen)

                    {

                        double relativaRate = Math.Abs(item.Y - currentPoint.Y) * 1.0 / Math.Abs(item.X - currentPoint.X);

                        double angle = Math.Atan(relativaRate) * 180 / Math.PI;

                        int xOrientation = item.X > currentPoint.X ? 1 : -1;

                        int yOrientation = item.Y > currentPoint.Y ? 1 : -1;

                        double x = currentPoint.X + intervalLen * Math.Cos(angle * Math.PI / 180) * xOrientation;

                        double y = currentPoint.Y + intervalLen * Math.Sin(angle * Math.PI / 180) * yOrientation;

                        currentPoint = new Point(x, y);

                        allSelectedPoint.Add(currentPoint);

                        i--;//   ,     

                    }

                }

                for (int j = 0; j < allSelectedPoint.Count; j++)

                {

                    List<Point> p = new List<Point>();

                    p.Add(allSelectedPoint[j]);

                    if (j < allSelectedPoint.Count - 1)

                    {

                        j++;

                    }

                    p.Add(allSelectedPoint[j]);

                    StylusPointCollection spc = new StylusPointCollection(p);

                    Stroke stroke = new Stroke(spc);

                    InkCanvas.Strokes.Add(stroke);

                }

}


이상은 테스트된 코드입니다. 부족한 점은 현재 획이 완성된 후에만 수정할 수 있습니다. 코드는 InkCanvas 에 쓰여 있기 때문입니다.StrokeCollected 에 있습니다.

좋은 웹페이지 즐겨찾기