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