使用WinForms c#文档查看器创建一个自定义注释

LEADTOOLS包含30多个内置注释对象,可以扩展这些对象以创建自定义注释对象。类派生的自定义圆注释AnnEllipseObject使用文档查看器在c# WinForms应用程序中创建类。

概述
总结 本教程涵盖了使用文档查看器在c# WinForms应用程序中自动和自定义注释功能。
完成时间 45分钟
Visual Studio项目 下载教程项目(14kb)
平台 WinForms c#应用程序
IDE Visual Studio 2019, 2022
开发许可 下载LEADTOOLS

所需的知识

方法之前,通过复习下面的教程,熟悉创建项目、初始化文档查看器和向文档查看器添加注释的基本步骤使用WinForms c#文档查看器创建一个自定义注释教程。

创建项目并添加LEADTOOLS引用

中创建的项目的副本开始在文档上绘制和编辑注释教程。如果项目不可用,请按照该教程中的步骤创建它。

所需要的参考资料取决于项目的目的。引用可以通过以下两种方法中的一种添加(但不能同时添加)。对于这个项目,需要以下参考资料。

如果使用NuGet引用,本教程需要以下NuGet包及其依赖项:

如果使用本地DLL引用,则需要以下DLL。dll位于< INSTALL_DIR > \ LEADTOOLS22 \ Bin \ Dotnet4 \ x64

有关应用程序需要哪些DLL文件的完整列表,请参阅在你的申请中包含的文件

设置License文件

许可证解锁项目所需的特性。它必须在调用任何工具箱函数之前设置。有关详细信息,包括针对不同平台的教程,请参阅设置运行时许可证教程

有两种类型的运行时许可证:

将自定义注释添加到注释工具栏面板

添加引用、设置许可并初始化Document Viewer之后,就可以开始编码了。

Form1.cs解决方案资源管理器.右键单击设计窗口,选择“查看代码”或按F7来显示表单后面的代码。

将下列语句添加到使用顶部的块:

c#
使用系统;使用先;使用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文件夹,突出添加,并单击类……

将类添加到文件夹

添加三个新类:

请注意

一定要更改每个类的名称空间,以匹配Form1.cs中的名称空间。

将下面的代码添加到AnnCircleDrawDesigner类:

c#
使用系统;使用Leadtools;使用Leadtools.Annotations.Engine;使用Leadtools.Annotations.Designers;名称空间Create_a_Custom_AnnotationAnnCircleDrawDesigner: 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类:

c#
使用Leadtools.Annotations.Engine;名称空间Create_a_Custom_AnnotationAnnCircleObject: AnnEllipseObject//设置id为UserObjectID公共常量intCircleObjectId = UserObjectId;///<摘要>///对象的构造函数。///将对象的id设置为圆形对象id///> < /总结公共AnnCircleObject ()基地()SetId (CircleObjectId);标签=受保护的覆盖AnnObject Create ()返回AnnCircleObject ();

将下面的代码添加到AnnCircleObjectRenderer类:

c#
使用System.Collections.Generic;使用Leadtools.Annotations.Rendering;使用Leadtools.Annotations.Automation;使用Leadtools.Annotations.Engine;使用Leadtools;名称空间Create_a_Custom_AnnotationAnnCircleObjectRenderer: 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操作)List newThumbs =列表< 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);如下:

c#
varautomationManagerHelper =AutomationManagerHelper (automationManager);CreateCustomObject (automationManager);

类中添加一个新方法Form1类命名CreateCustomObject (AnnAutomationManager annManager).方法中调用此方法InitAnnotations ()方法,如上所示。将以下代码添加到新方法中:

c#
私人无效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.CircleObjectIdAnnCircleObjectRenderer (annManager));

运行项目

按下运行项目F5,或选择Debug ->开始调试。

如果正确地遵循了这些步骤,应用程序将运行,并且可以选择工具栏上的任何注释在加载的文档上绘制。选择黑色圆圈图标,单击并拖动文档以添加自定义圆圈注释。下图显示了文档查看器,查看器右侧有注释工具栏。

DocumentViewer显示在文档上绘制的一些注释

总结

方法的使用AutomationManagerAutomationManagerHelperAnnAutomation,AnnAutomationObject类的主题是控件绘制和编辑自动和自定义注释。

另请参阅

iOS
188金宝搏的网址客服|支持|联系我们|知识产权公告
©1991 - 2022领德科技有限公司版权所有。