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

}

posted @ 2018-11-14 13:01  xunyiHe  阅读(4034)  评论(2编辑  收藏  举报