c# 画刻度尺(支持缩放)

 

// 画标尺

private void DrawRule(double scale)//scale缩放的倍数
{

if (cvRuler.Children != null)
{
cvRuler.Children.Clear();
}

System.Windows.Shapes.Line _line;
TextBlock _textBlock;

const double _minPixel = 30;
string _unit = "mm";
double _scientificF;
int _scientificE;
string[] _strTemp = (_minPixel / 1).ToString("E").Split('E');
double.TryParse(_strTemp[0], out _scientificF);
int.TryParse(_strTemp[1], out _scientificE);
if (_scientificE >= 2 || (_scientificE >= 1 && _scientificF >= 5))
{
_unit = "m";
_scientificE -= 3;
}

_textBlock = new TextBlock();
_textBlock.Text = _unit;
_textBlock.FontSize = 8;
_textBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
_textBlock.VerticalAlignment = System.Windows.VerticalAlignment.Top;
_textBlock.Margin = new Thickness(10, 0, 0, 0);
cvRuler.Children.Add(_textBlock);

int _lineIndex = 0;
//double _width = cvRuler.ActualWidth - 0;
double _width = grid_Loy.ActualWidth - 0;
if (4000 * scale > grid_main.ActualWidth)
{
_width = 4000 * scale;
}

DPI dpi = new DPI();
float dpi_x = dpi.Dpi_x;
double mm = _width / dpi_x * 25.4;
//_width = mm;
double mmTopx = 1 * dpi_x / 25.4 * scale;

if (scale < 1)//缩放<100%
{
mmTopx = 10 * mmTopx;
}

for (double i = 0; i < _width; i += mmTopx)
{
_line = new System.Windows.Shapes.Line();
if (_lineIndex % 10 == 0)
{
_line.Stroke = Brushes.Black;
_line.StrokeThickness = 1;
_line.X1 = i;
_line.Y1 = 0;
_line.X2 = i;
_line.Y2 = 20;
_line.VerticalAlignment = VerticalAlignment.Bottom;

_textBlock = new TextBlock();
_textBlock.Text = _lineIndex.ToString();
if (scale < 1)
{
_textBlock.Text = (_lineIndex*10).ToString();
}
_textBlock.FontSize = 8;
_textBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
_textBlock.VerticalAlignment = System.Windows.VerticalAlignment.Top;
_textBlock.Margin = new Thickness(i, 0, 0, 0);
_textBlock.Foreground = Brushes.Black;
cvRuler.Children.Add(_textBlock);
}
else if (_lineIndex % 5 == 0)
{
_line.Stroke = Brushes.Black;
_line.StrokeThickness = 1;
_line.X1 = i;
_line.Y1 = 0;
_line.X2 = i;
_line.Y2 = 10;
_line.VerticalAlignment = VerticalAlignment.Bottom;

if (scale < 1)
{
_textBlock = new TextBlock();
_textBlock.Text = (_lineIndex * 10).ToString();
_textBlock.FontSize = 8;
_textBlock.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
_textBlock.VerticalAlignment = System.Windows.VerticalAlignment.Top;
_textBlock.Margin = new Thickness(i, 0, 0, 0);
_textBlock.Foreground = Brushes.Black;
cvRuler.Children.Add(_textBlock);
}
}
else
{
_line.Stroke = Brushes.DimGray;
_line.StrokeThickness = 1;
_line.X1 = i;
_line.Y1 = 0;
_line.X2 = i;
_line.Y2 = 5;
_line.VerticalAlignment = VerticalAlignment.Bottom;
}
cvRuler.Children.Add(_line);

_lineIndex++;
}

}

转载于:https://www.cnblogs.com/xunyiHe/p/9957412.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值