WPF标尺自定义控件,用于跟踪鼠标并移动网格线

时间:2013-11-10 23:23:05

标签: c# wpf wpf-controls custom-controls onrender

我正致力于绘图应用程序。我做了自定义标尺控制,但我坚持网格线支持。 我的标尺看起来像这样:  http://i.stack.imgur.com/dRUE3.jpg

RulerControl OnRender方法:

protected override void OnRender(DrawingContext drawingContext)
{
   RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
   double psuedoStartValue = StartValue;
      #region Vertical Ruler
            psuedoStartValue = -(StartValue);
            for (int i = 0; i < this.ActualHeight / MajorInterval; i++)
            {
                var ft = new FormattedText((psuedoStartValue * MajorInterval).ToString(), System.Globalization.CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, new Typeface("Tahoma"), 10, Brushes.Black);
                drawingContext.DrawText(ft, new Point(0, i * MajorInterval));
                drawingContext.DrawLine(new Pen(new SolidColorBrush(Colors.Red), 1), new Point(MarkLength, i * MajorInterval), new Point(0, i * MajorInterval));
                drawingContext.DrawLine(new Pen(new SolidColorBrush(Colors.Red), 1), new Point(MarkLength, i * MajorInterval), new Point(0, i * MajorInterval));
                drawingContext.DrawLine(new Pen(new SolidColorBrush(Colors.Green), 1),
                    new Point(MiddleMarkLength, i * MajorInterval + (MajorInterval / 2)),
                    new Point(0, i * MajorInterval + (MajorInterval / 2)));
                for (int j = 1; j < 10; j++)
                {
                    if (j==5)
                    {
                        continue;
                    }
                    drawingContext.DrawLine(new Pen(new SolidColorBrush(Colors.Blue), 1),
                    new Point(LittleMarkLength, i * MajorInterval + (((MajorInterval*j) / 10))),
                    new Point(0, i * MajorInterval + (((MajorInterval*j) / 10))));
                }
                psuedoStartValue--;
            }
        #endregion

        #region Mouse Tracking

        if (this.Orientation == enumOrientation.Horizontal)
        {
            drawingContext.DrawLine(mouseTrackPen,
                new Point(mousePosition.X, this.ActualHeight), new Point(mousePosition.X, 0));
        }
        #endregion
  }

当用户在窗口中移动鼠标时,标尺应显示网格线。我用Mouse.GetPosition(this)方法获得鼠标位置。但我无法在标尺中绘制动态网格线。 我是如何实现这个问题的?

0 个答案:

没有答案