本文主要是介绍wpf实现类似word文档的标尺功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
需求:实现类似word文档的标尺功能。
技术背景:模板,依赖属性
技术细节:
1、操作模板控件
在做WPF开发的时候,我们通常因为满足不同的需求会开发一些自定义控件来满足需要,我们会自定义模板来定义控件的外观,添加命令和路由事件来给控件添加行为,那如何在模板中查找元素并关联事件处理程序或添加数据绑定表达式呢,WPF有一个专用的OnApplyTemplate()方法,在该方法中,可以使用GetTemplateChild或FindName来查找所需的元素。
2、添加部件名称
为了查找做需要的元素,WPF控件通过名称定为他们需要的元素,所以,元素的名称变成了自定义控件公有接口的一部分,并且需要恰当的描述性名称。为此。WPF给出了约定:这些名称以PART_开头,后跟元素名称。元素名称的首字母要大写,就像一个属性名称:比如PART_Slider
3、显示自定义控件
通过OnRender来显示自定义控件时,必须在相应的依赖属性加入AffectsRender枚举值,通过DependencyProperty的AddOwner或者OverrideMetadata来改写依赖属性的元数据。
public static readonly DependencyProperty ZoomProperty =DependencyProperty.Register("Zoom", typeof(double),typeof(Ruler),new FrameworkPropertyMetadata((double) 1.0,FrameworkPropertyMetadataOptions.AffectsRender));
4、wpf的分辨率。wpf 规定其坐标单位是1/96 inch,即1个设备无关单位(DIU),对于屏幕为1600*1200的分辨率,假如屏幕的物理高度,宽度为17.0*12.75,那么水平的分辨率1600/17.0=94DPI,垂直的分辨率1200/12.75 = 94DPI,如果我们设置系统分辨率等于物理分辨率,即设置系统分辨率为94DPI,就可以实现“分辨率无关”。
实现:
定义Ruler类,继承FrameworkElement
定义依赖属性:长度double, 是否自动缩放(bool),缩放因子(double),鼠标当前位置的刻度线(double),起始刻度(double),刻度的位置,是在上面还是下面(Enum),单位,厘米还是英寸(Enum)
在OnRender中,
根据当前选择的单位,将标尺的长度与宽度从cm转化为DPI,
然后使用DrawingContext绘制矩形,显示出整个标尺
然后使用DrawLine绘制鼠标当前位置,初始时为负,不显示
然后使用DrawLine依次绘制刻度线,中线,主刻度线
最后使用DrawText绘制数值
protected override void OnRender(DrawingContext drawingContext){base.OnRender(drawingContext);double xDest = (Unit == Unit.Cm ? DipHelper.CmToDip(Length) : DipHelper.InchToDip(Length)) * this.Zoom;drawingContext.DrawRectangle(null, BorderPen, new Rect(new Point(0.0, 0.0), new Point(xDest, Height)));double chip = Unit == Unit.Cm ? DipHelper.CmToDip(Chip) : DipHelper.InchToDip(Chip);drawingContext.DrawLine(RedPen, new Point(chip, 0), new Point(chip, Height));for (double dUnit = 0; dUnit <= Length; dUnit++){double d;if (Unit == Unit.Cm){d = DipHelper.CmToDip(dUnit) * this.Zoom;if (dUnit < Length){#region 绘制刻度线for (int i = 1; i <= 9; i++){if (i != 5)//不绘制中线{double dMm = DipHelper.CmToDip(dUnit + 0.1 * i) * this.Zoom;if (Marks == MarksLocation.Up)drawingContext.DrawLine(ThinPen, new Point(dMm, 0), new Point(dMm, SegmentHeight / 3.0));elsedrawingContext.DrawLine(ThinPen, new Point(dMm, Height), new Point(dMm, Height - SegmentHeight / 3.0));}} #endregion#region 绘制中线double dMiddle = DipHelper.CmToDip(dUnit + 0.5) * this.Zoom;if (Marks == MarksLocation.Up)drawingContext.DrawLine(p, new Point(dMiddle, 0), new Point(dMiddle, SegmentHeight * 2.0 / 3.0));elsedrawingContext.DrawLine(p, new Point(dMiddle, Height), new Point(dMiddle, Height - SegmentHeight * 2.0 / 3.0)); #endregion}}else{d = DipHelper.InchToDip(dUnit) * this.Zoom;if (dUnit < Length){if (Marks == MarksLocation.Up){double dQuarter = DipHelper.InchToDip(dUnit + 0.25) * this.Zoom;drawingContext.DrawLine(ThinPen, new Point(dQuarter, 0),new Point(dQuarter, SegmentHeight / 3.0));double dMiddle = DipHelper.InchToDip(dUnit + 0.5) * this.Zoom;drawingContext.DrawLine(p, new Point(dMiddle, 0),new Point(dMiddle, SegmentHeight * 2D / 3D));double d3Quarter = DipHelper.InchToDip(dUnit + 0.75) * this.Zoom;drawingContext.DrawLine(ThinPen, new Point(d3Quarter, 0),new Point(d3Quarter, SegmentHeight / 3.0));}else{double dQuarter = DipHelper.InchToDip(dUnit + 0.25) * this.Zoom;drawingContext.DrawLine(ThinPen, new Point(dQuarter, Height),new Point(dQuarter, Height - SegmentHeight / 3.0));double dMiddle = DipHelper.InchToDip(dUnit + 0.5) * this.Zoom;drawingContext.DrawLine(p, new Point(dMiddle, Height),new Point(dMiddle, Height - SegmentHeight * 2D / 3D));double d3Quarter = DipHelper.InchToDip(dUnit + 0.75) * this.Zoom;drawingContext.DrawLine(ThinPen, new Point(d3Quarter, Height),new Point(d3Quarter, Height - SegmentHeight / 3.0));}}}#region 绘制主刻度线if (Marks == MarksLocation.Up)drawingContext.DrawLine(p, new Point(d, 0), new Point(d, SegmentHeight));elsedrawingContext.DrawLine(p, new Point(d, Height), new Point(d, Height - SegmentHeight));#endregion#region 绘制数值if ((dUnit != 0.0) && (dUnit < Length)){FormattedText ft = new FormattedText((dUnit + CountShift).ToString(CultureInfo.CurrentCulture),CultureInfo.CurrentCulture,FlowDirection.LeftToRight,new Typeface("Arial"),DipHelper.PtToDip(6),Brushes.DimGray);ft.SetFontWeight(FontWeights.Regular);ft.TextAlignment = TextAlignment.Center;if (Marks == MarksLocation.Up)drawingContext.DrawText(ft, new Point(d, Height - ft.Height));elsedrawingContext.DrawText(ft, new Point(d, Height - SegmentHeight - ft.Height));} #endregion}}
效果图
源码:
https://download.csdn.net/download/sinat_31608641/11850893
这篇关于wpf实现类似word文档的标尺功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!