C# 实现完整功能的截图控件(1)-实现绘图工具栏控件


C# 实现完整功能的截图控件(1)-实现绘图工具栏控件


    之前写了一篇关于截图的文章(查看),只实现了简单的截图,接下的文章将介绍怎样一步步的实现一个完整的截图控件。这篇文章将介绍怎样实现绘图工具栏控件DrawToolsControl,先来了解一下这个工具栏控件包含些什么内容。因为只对截图实现添加一些简单的图形和文字绘制,所以只实现了添加矩形、椭圆、箭头、文字和线条,所以工具栏需要包含绘制矩形、椭圆、箭头、文字和线条按钮。因为还要实现撤销、保存截图等,所以工具栏还要添加撤销、保存、退出和保存当前图形的按钮。需要的按钮就这么多了,我们可以用ToolStrip来添加这些按钮,但是为了控件看起来更漂亮,需要对ToolStrip进行重绘,下面就具体怎样实现DrawToolsControl控件。


第一步:继承UserControl控件,在上面添加一个ToolStrip,然后添加前面所说的所有按钮,调整好UserControl控件的大小,让它刚好可以显示完整的ToolStrip。

第二步:添加相应按钮的点击事件,具体就不一一列出了。


第三步:对UserControl进行重绘。先改变UserControl的Region,让它为圆角的样式,然后重绘背景和边框。



第四步:对ToolStrip进行重绘。这是比较重要的,绘制好的ToolStrip还可以在其他地方使用。对ToolStrip实现重绘,需要继承ToolStripRenderer类实现一个新的ToolStripRendererEx类,根据需要,对OnRenderToolStripBackground、OnRenderButtonBackground和OnRenderSeparator方法进行重写,然后把ToolStrip的Renderer属性设置为新的ToolStripRendererEx就行了。


来看看实现的关键代码:


1、 UserControl设置Region和绘制代码:
protected override void OnPaint(PaintEventArgs e)
{
   base.OnPaint(e);
   
   Graphics g = e.Graphics;
   g.SmoothingMode = SmoothingMode.AntiAlias;
   
   using (GraphicsPath path = GraphicsPathHelper.CreatePath(
   ClientRectangle, 8, RoundStyle.All, false))
   {
      using (SolidBrush brush = new SolidBrush(ColorTable.BackColorNormal))
      {
         g.FillPath(brush, path);
      }
      using (Pen pen = new Pen(ColorTable.BorderColor))
      {
         g.DrawPath(pen, path);
         
         using (GraphicsPath innerPath = GraphicsPathHelper.CreatePath(
         ClientRectangle, 8, RoundStyle.All, true))
         {
            g.DrawPath(pen, innerPath);
         }
      }
   }
}

private void SetRegion()
{
   using (GraphicsPath path = GraphicsPathHelper.CreatePath(
   ClientRectangle, 8, RoundStyle.All, false))
   {
      if (base.Region != null)
      {
         base.Region.Dispose();
      }
      base.Region = new Region(path);
   }
}

2、 ToolStripRendererEx重绘代码:
protected override void OnRenderToolStripBackground(
ToolStripRenderEventArgs e)
{
   Graphics g = e.Graphics;
   g.SmoothingMode = SmoothingMode.AntiAlias;
   
   LinearGradientMode mode =
   e.ToolStrip.Orientation == Orientation.Horizontal ?
   LinearGradientMode.Vertical : LinearGradientMode.Horizontal;
   RenderBackgroundInternal(
   g,
   e.AffectedBounds,
   ColorTable.BackColorHover,
   ColorTable.BorderColor,
   ColorTable.BackColorNormal,
   RoundStyle.All,
   false,
   true,
   mode);
}

protected override void OnRenderButtonBackground(
ToolStripItemRenderEventArgs e)
{
   ToolStripButton item = e.Item as ToolStripButton;
   if (item != null)
   {
      LinearGradientMode mode =
      e.ToolStrip.Orientation == Orientation.Horizontal ?
      LinearGradientMode.Vertical : LinearGradientMode.Horizontal;
      Graphics g = e.Graphics;
      g.SmoothingMode = SmoothingMode.AntiAlias;
      Rectangle bounds = new Rectangle(Point.Empty, item.Size);
      
      if (item.BackgroundImage != null)
      {
         Rectangle clipRect = item.Selected ? item.ContentRectangle : bounds;
         ControlPaintEx.DrawBackgroundImage(
         g,
         item.BackgroundImage,
         ColorTable.BackColorNormal,
         item.BackgroundImageLayout,
         bounds,
         clipRect);
      }
      
      if (item.CheckState == CheckState.Unchecked)
      {
         if (item.Selected)
         {
            Color color = ColorTable.BackColorHover;
            if (item.Pressed)
            {
               color = ColorTable.BackColorPressed;
            }
            RenderBackgroundInternal(
            g,
            bounds,
            color,
            ColorTable.BorderColor,
            ColorTable.BackColorNormal,
            RoundStyle.All,
            true,
            true,
            mode);
            return;
         }
         else
         {
            if (e.ToolStrip is ToolStripOverflow)
            {
               using (Brush brush = new SolidBrush(ColorTable.BackColorNormal))
               {
                  g.FillRectangle(brush, bounds);
               }
               return;
            }
         }
      }
      else
      {
         Color color = ControlPaint.Light(ColorTable.BackColorHover);
         if (item.Selected)
         {
            color = ColorTable.BackColorHover;
         }
         if (item.Pressed)
         {
            color = ColorTable.BackColorPressed;
         }
         RenderBackgroundInternal(
         e.Graphics,
         bounds,
         color,
         ColorTable.BorderColor,
         ColorTable.BackColorNormal,
         RoundStyle.All,
         true,
         true,
         mode);
         return;
      }
   }
   
   base.OnRenderButtonBackground(e);
}

protected override void OnRenderSeparator(
ToolStripSeparatorRenderEventArgs e)
{
   Rectangle rect = e.Item.ContentRectangle;
   if (e.ToolStrip is ToolStripDropDown)
   {
      if (e.Item.RightToLeft == RightToLeft.Yes)
      {
         //rect.X -= OffsetMargin + 4;
      }
      else
      {
         rect.X += OffsetMargin + 4;
      }
      rect.Width -= OffsetMargin + 8;
   }
   RenderSeparatorLine(
   e.Graphics,
   rect,
   ColorTable.BackColorPressed,
   ColorTable.BackColorNormal,
   SystemColors.ControlLightLight,
   e.Vertical);
}




本文来源:
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
C/S框架网
上一篇:C#.Net对象内存模型及堆/栈数据结构详解 (四)
下一篇:最全C#OOP设计思想之汽车模型源代码
评论列表

发表评论

评论内容
昵称:
关联文章

C# 实现完整功能(1)-实现绘图工具栏
C# 实现软件功能
C# VS自带Chart图表实现实时折线图,波形
C#实现DevExpress换肤功能
详解DevExpress.LookUpEdit实现自动搜索定位功能
C# GridView列头添加CheckBox实现全选功能
使用Scheduler实现生产进度监控程序
(C#)RichTextBox查找文本演示(功能全)
C# ImageListView下载(源代码)
C/S架构软件快速开发平台-旗舰版V5.1软件
C# Barcode条码使用方法
WinFramework轻量级开发框架 - 软件
C#.Net自定义 - GridPopupContainerEdit
LookUpEdit带给用户良好操作体验
DevExpress Winform 利用GridControl开发会计凭证(C#源码)
隐藏主窗体工具栏功能按钮(方法二)
C#.Net用户自定义制作教程
C# 实现条码图片自动生成功能
C#用ListView实现XP分组样式()
CSFramework.WebApi后端框架软件

热门标签
.NET5 .NET6 .NET7 APP Auth-软件授权注册系统 Axios B/S B/S开发框架 Bug Bug记录 C#加密解密 C#源码 C/S CHATGPT CMS系统 CodeGenerator CSFramework.DB CSFramework.EF CSFrameworkV1学习版 CSFrameworkV2标准版 CSFrameworkV3高级版 CSFrameworkV4企业版 CSFrameworkV5旗舰版 CSFrameworkV6.0 DAL数据访问层 Database datalock DbFramework Demo教学 Demo下载 DevExpress教程 DOM EF框架 Element-UI EntityFramework ERP ES6 Excel FastReport GIT HR IDatabase IIS JavaScript LINQ MES MiniFramework MIS NavBarControl Node.JS NPM OMS ORM PaaS POS Promise API Redis SAP SEO SQL SQLConnector TMS系统 Token令牌 VS2022 VSCode VUE WCF WebApi WebApi NETCore WebApi框架 WEB开发框架 Windows服务 Winform 开发框架 Winform 开发平台 WinFramework Workflow工作流 Workflow流程引擎 版本区别 报表 踩坑日记 操作手册 代码生成器 迭代开发记录 基础资料窗体 架构设计 角色权限 开发sce 开发技巧 开发教程 开发框架 开发平台 开发指南 客户案例 快速搭站系统 快速开发平台 秘钥 密钥 权限设计 软件报价 软件测试报告 软件简介 软件开发框架 软件开发平台 软件开发文档 软件体系架构 软件下载 软著证书 三层架构 设计模式 生成代码 实用小技巧 收钱音箱 数据锁 数据同步 微信小程序 未解决问题 文档下载 喜鹊ERP 喜鹊软件 系统对接 详细设计说明书 行政区域数据库 需求分析 疑难杂症 蝇量级框架 蝇量框架 用户管理 用户开发手册 用户控件 在线支付 纸箱ERP 智能语音收款机 自定义窗体 自定义组件 自动升级程序