LEADTOOLS包含30多个内置注释对象,可以扩展这些对象以创建自定义注释对象。类派生的自定义圆注释AnnEllipseObject使用文档查看器在c# WinForms应用程序中创建类。
| 概述 | |
|---|---|
| 总结 | 本教程涵盖了使用文档查看器在c# WinForms应用程序中自动和自定义注释功能。 |
| 完成时间 | 45分钟 |
| Visual Studio项目 | 下载教程项目(14kb) |
| 平台 | WinForms c#应用程序 |
| IDE | Visual Studio 2019, 2022 |
| 开发许可 | 下载LEADTOOLS |
方法之前,通过复习下面的教程,熟悉创建项目、初始化文档查看器和向文档查看器添加注释的基本步骤使用WinForms c#文档查看器创建一个自定义注释教程。
中创建的项目的副本开始在文档上绘制和编辑注释教程。如果项目不可用,请按照该教程中的步骤创建它。
所需要的参考资料取决于项目的目的。引用可以通过以下两种方法中的一种添加(但不能同时添加)。对于这个项目,需要以下参考资料。
如果使用NuGet引用,本教程需要以下NuGet包及其依赖项:
Leadtools.Annotations.WinFormsLeadtools.Document.SdkLeadtools.Document.Viewer.WinForms如果使用本地DLL引用,则需要以下DLL。dll位于< INSTALL_DIR > \ LEADTOOLS22 \ Bin \ Dotnet4 \ x64:
Leadtools.dllLeadtools.Annotations.Automation.dllLeadtools.Annotations.Engine.dllLeadtools.Annotations.WinForms.dllLeadtools.Annotations.Rendering.WinForms.dllLeadtools.Caching.dllLeadtools.Codecs.dllLeadtools.Controls.WinForms.dllLeadtools.Document.dllLeadtools.Document.Pdf.dllLeadtools.Document.Viewer.WinForms.dll有关应用程序需要哪些DLL文件的完整列表,请参阅在你的申请中包含的文件.
许可证解锁项目所需的特性。它必须在调用任何工具箱函数之前设置。有关详细信息,包括针对不同平台的教程,请参阅设置运行时许可证教程.
有两种类型的运行时许可证:
添加引用、设置许可并初始化Document Viewer之后,就可以开始编码了。
去Form1.cs在解决方案资源管理器.右键单击设计窗口,选择“查看代码”或按F7来显示表单后面的代码。
将下列语句添加到使用顶部的块:
使用系统;使用先;使用System.Drawing;使用System.Windows.Forms;使用Leadtools;使用Leadtools.Controls;使用Leadtools.Caching;使用Leadtools.Annotations.WinForms;使用Leadtools.Annotations.Designers;使用Leadtools.Annotations.Automation;使用Leadtools.Document;使用Leadtools.Document.Viewer;
首先下载圆形图标PNG图像。提取它并将PNG文件重新命名为Create-a-Custom-Annotation-Circle-Icon.png.
在解决方案资源管理器,往下掉属性选项卡,双击Resources.resx.
点击添加资源然后点击添加现有文件…
浏览到您保存圆圈图标PNG图像的位置,然后单击开放.导入资源之后,项目将创建一个资源文件夹中。
这个图像需要嵌入到项目中。为此,请导航到解决方案资源管理器,往下掉资源文件夹,然后点击Create-a-Custom-Annotation-Circle-Icon.png.在图像的属性中,更改建立行动到嵌入式资源。
在解决方案资源管理器,右键单击项目,突出添加,并单击创建文件夹.将文件夹重命名为AnnCircleObject.
在解决方案资源管理器,右键单击AnnCircleObject文件夹,突出添加,并单击类…….
添加三个新类:
AnnCircleDrawDesigner.csAnnCircleObject.csAnnCircleObjectRenderer.cs请注意
一定要更改每个类的名称空间,以匹配Form1.cs中的名称空间。
将下面的代码添加到AnnCircleDrawDesigner类:
使用系统;使用Leadtools;使用Leadtools.Annotations.Engine;使用Leadtools.Annotations.Designers;名称空间Create_a_Custom_Annotation{类AnnCircleDrawDesigner: AnnRectangleDrawDesigner{//我们需要两个点,起点和终点私人LeadPointD begin = leadpoint . empty;私人LeadPointD end = leadpoint . empty;///<摘要>///AnnCircleDrawDesigner的构造函数///> < /总结公共AnnCircleDrawDesigner(IAnnAutomationControl automationControl, AnnContainer container, AnnCircleObject annObject):基地(automationControl, container, annObject) {}///<摘要>///重写指针向下事件///将开始点和结束点设置为第一次单击的位置///> < /总结公共覆盖保龄球OnPointerDown(AnnContainer sender, AnnPointerEventArgs e){begin = e.位置;结束=开始;返回基地.OnPointerDown(发送方,e);}///<摘要>///重写指针移动事件///将新的鼠标点设置为结束变量///做一些计算来创建一个圆对象(宽度/高度保持相等)///> < /总结公共覆盖保龄球OnPointerMove(AnnContainer sender, AnnPointerEventArgs e){end = e.位置;AnnCircleObject circle = (AnnCircleObject)目标对象;双X = (end。X - start .X);双y = Math.Abs(结束。Y - start .Y);双scaleX = 1;双scaleY = 1;如果(x < y)scaleX = y / x;其他的scaleY = x / y;圆。Rect = LeadRectD.Create(开始。X,开始。Y, Math.Abs(结束。X - start .X) * Math.Abs(scaleX), Math.Abs(结束。Y - start .Y) * Math.Abs(scaleY));无效(LeadRectD.Empty);返回真正的;}}}
将下面的代码添加到AnnCircleObject类:
使用Leadtools.Annotations.Engine;名称空间Create_a_Custom_Annotation{类AnnCircleObject: AnnEllipseObject{//设置id为UserObjectID公共常量intCircleObjectId = UserObjectId;///<摘要>///对象的构造函数。///将对象的id设置为圆形对象id///> < /总结公共AnnCircleObject ():基地(){SetId (CircleObjectId);标签=零;}受保护的覆盖AnnObject Create (){返回新AnnCircleObject ();}}}
将下面的代码添加到AnnCircleObjectRenderer类:
使用System.Collections.Generic;使用Leadtools.Annotations.Rendering;使用Leadtools.Annotations.Automation;使用Leadtools.Annotations.Engine;使用Leadtools;名称空间Create_a_Custom_Annotation{类AnnCircleObjectRenderer: AnnEllipseObjectRenderer{///<摘要>///呈现器的构造函数///获取椭圆对象渲染器,并对圆使用相同的样式///> < /总结公共AnnCircleObjectRenderer (AnnAutomationManager经理):基地(){IAnnObjectRenderer annEllipseObjRenderer = manager.RenderingEngine.Renderers[AnnObject.EllipseObjectId];LabelRenderer = annEllipseObjRenderer.LabelRenderer;LocationsThumbStyle = annEllipseObjRenderer.LocationsThumbStyle;RotateCenterThumbStyle = annEllipseObjRenderer.RotateCenterThumbStyle;RotateGripperThumbStyle = annEllipseObjRenderer.RotateGripperThumbStyle;//下面的代码片段改变注释的缩略图大小//铅条大小newThumbSize =铅条大小。创建(300、300);//缩略图的新大小,根据需要进行更改/ / LocationsThumbStyle。大小= newThumbSize;/ / RotateCenterThumbStyle。大小= newThumbSize;/ / RotateGripperThumbStyle。大小= newThumbSize;}///<摘要>///重写RenderThumbs方法///穿过拇指,去掉顶部,底部,左边和右边的拇指,这样就不能从一个圆圈改变///> < /总结公共覆盖无效RenderThumbs(AnnContainerMapper mapper, LeadPointD[] thumbLocations, AnnFixedStateOperations操作){ListnewThumbs =新列表< LeadPointD > (); 为(intI = 0;i < thumbLocations.Length;I += 2)newThumbs.Add (thumbLocations[我]);基地.RenderThumbs(mapper, newThumbs.ToArray(), operations);}}}
右键单击Form1.cs在解决方案资源管理器并选择视图代码来显示表单后面的代码。
在InitAnnotations ()方法CreateCustomObject (automationManager);下面的代码,var automationManagerHelper = new automationManagerHelper (automationManager);如下:
varautomationManagerHelper =新AutomationManagerHelper (automationManager);CreateCustomObject (automationManager);
类中添加一个新方法Form1类命名CreateCustomObject (AnnAutomationManager annManager).方法中调用此方法InitAnnotations ()方法,如上所示。将以下代码添加到新方法中:
私人无效CreateCustomObject (AnnAutomationManager annManager){//创建circle对象,分配设计器,设置工具栏图像,然后将其添加到管理器和渲染引擎AnnAutomationObject circleAutomationObject =新AnnAutomationObject ();circleAutomationObject。Id = AnnCircleObject.CircleObjectId;circleAutomationObject。Name =“圆”;circleAutomationObject。ToolBarToolTipText = circleAutomationObject.Name;circleAutomationObject。DrawDesignerType =typeof(AnnCircleDrawDesigner);circleAutomationObject。EditDesignerType =typeof(AnnRectangleEditDesigner);circleAutomationObject。RunDesignerType =typeof(AnnRunDesigner);circleAutomationObject。ObjectTemplate =新AnnCircleObject ();circleAutomationObject。ToolBarImage =新位图(typeof(Form1),“Resources.Create-a-Custom-Annotation-Circle-Icon.png”);circleAutomationObject。快捷菜单=新Leadtools.Annotations.WinForms.ObjectContextMenu ();annManager.Objects.Add (circleAutomationObject);annManager.RenderingEngine.Renderers.Add (AnnCircleObject.CircleObjectId新AnnCircleObjectRenderer (annManager));}
按下运行项目F5,或选择Debug ->开始调试。
如果正确地遵循了这些步骤,应用程序将运行,并且可以选择工具栏上的任何注释在加载的文档上绘制。选择黑色圆圈图标,单击并拖动文档以添加自定义圆圈注释。下图显示了文档查看器,查看器右侧有注释工具栏。
方法的使用AutomationManager,AutomationManagerHelper,AnnAutomation,AnnAutomationObject类的主题是控件绘制和编辑自动和自定义注释。