schangxiang@126.com
2025-11-04 ca398287db3f5ea01f03aac4a85edb13b28100a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
 
namespace XHandler.Controls
{
    public class GraduatedCanvas:Canvas
    {
        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
 
            double width = ActualWidth;
            double height = ActualHeight;
 
            // 绘制横向刻度
            for (int i = 0; i <= height; i += 25)
            {
                if (i % 50 == 0)
                {
                    drawingContext.DrawLine(new Pen(Brushes.LightGray, 1), new Point(-5, i), new Point(width, i));
                }
                else
                {
                    drawingContext.DrawLine(new Pen(Brushes.LightGray, 1), new Point(0, i), new Point(width, i));
                }
 
                // 创建文字格式
                Typeface typeface = new Typeface(new FontFamily("Arial"), FontStyles.Italic, FontWeights.Normal, FontStretches.Normal);
 
                string strDisplay = (i * 2 / 10).ToString();
                if(i==height)
                {
                    strDisplay = (i * 2 / 10).ToString() + "(cm)";
                }
                FormattedText formattedText = new FormattedText(strDisplay, System.Globalization.CultureInfo.CurrentCulture,FlowDirection.LeftToRight,
                    typeface,10,Brushes.Black,1.25d);
 
                // 指定文字的位置
                Point textPosition = new Point(-10, i-8);
                if (i == height)
                {
                    textPosition = new Point(-20, i - 8);
                }
 
                if (i % 50 == 0)
                {
                    // 绘制文本
                    drawingContext.DrawText(formattedText, textPosition);
                }
            }
 
            // 绘制纵向刻度
            for (int i = 0; i <= width; i += 25)
            { 
                if (i % 50 == 0)
                {
                    drawingContext.DrawLine(new Pen(Brushes.LightGray, 1), new Point(i, 0), new Point(i, height+5));
                }
                else
                {
                    drawingContext.DrawLine(new Pen(Brushes.LightGray, 1), new Point(i, 0), new Point(i, height));
                }
 
                // 创建文字格式
                Typeface typeface = new Typeface(new FontFamily("Arial"), FontStyles.Italic, FontWeights.Normal, FontStretches.Normal);
                string strDisplay = (i * 2 / 10).ToString();
                if (i == width)
                {
                    strDisplay = (i * 2 / 10).ToString() + "(cm)";
                }
                FormattedText formattedText = new FormattedText(strDisplay, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
                    typeface, 10, Brushes.Black, 1.25d);
 
                // 指定文字的位置
                Point textPosition = new Point(i-8, height + 10);
 
                if (i % 50 == 0&&i!=0)
                {
                    // 绘制文本
                    drawingContext.DrawText(formattedText, textPosition);
                }
            }
        }
    }
}