C#软件开发实例.私人订制自己的屏幕截图工具(十一)编辑工具栏的实现

时间:2021-08-27 15:24:28

本实例全部文章目录

(一)功能概览

(二)创建项目、注册热键、显示截图主窗口

(三)托盘图标及菜单的实现

(四)基本截图功能实现

(五)针对拖拽时闪烁卡顿现象的优化

(六)添加配置管理功能

(七)添加放大镜的功能

(八)添加键盘操作截图的功能

(九)使用自定义光标,QQ截图时的光标

(十)在截图中包含鼠标指针形状

概述

截图之后,有时我们需要对其进行标注、修改等功能,这里就讲一下编辑工具栏的实现。

效果

C#软件开发实例.私人订制自己的屏幕截图工具(十一)编辑工具栏的实现

资源

C#软件开发实例.私人订制自己的屏幕截图工具(十一)编辑工具栏的实现
C#软件开发实例.私人订制自己的屏幕截图工具(十一)编辑工具栏的实现
C#软件开发实例.私人订制自己的屏幕截图工具(十一)编辑工具栏的实现

大概思路

工具栏的图片包含正常的状态和选中的状态,绘制的时候根据状态取对应位置的图片进行绘制;

事件方法

需要响应几个事件:选取截图区域结束,显示工具栏;鼠标在工具栏上移动时,设置按钮的焦点状态;鼠标在工具栏上按下时,设置按钮的焦点状态;

界面元素

C#软件开发实例.私人订制自己的屏幕截图工具(十一)编辑工具栏的实现
我这里使用Label控件显示工具栏;使用Panel控件显示工具的设置项及调色板;

相关代码

初始化工具栏
        /// <summary>        /// 窗口加载事件处理程序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
this.lbl_ToolBox.Size = new Size(Properties.Resources.ToolsBox.Size.Width, 26);//设置大小
this.lbl_ToolBox.Visible = false;//默认不显示
}
显示工具栏
        /// <summary>        /// 截图窗口鼠标抬起事件处理程序        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void Form1_MouseUp(object sender, MouseEventArgs e)        {            if (e.Button == MouseButtons.Left)            {                if (this.isCuting)                {                    this.isCuting = false;                    //如果自动发现窗口的区域不为空,则说明用户选择的是自动发现窗口的区域                    if (!rect_WindowFromPoint.IsEmpty)                    {                        SaveCutImageSize(rect_WindowFromPoint.Location, new Point(rect_WindowFromPoint.Right - 1, rect_WindowFromPoint.Bottom -1));                    }                    if (this.ToolBoxVisible)                    {                        this.lbl_ToolBox.Show();                        //向工具栏发送鼠标按下和抬起事件,设置默认进入矩形编辑状态                        MouseEventArgs arg = new MouseEventArgs(MouseButtons.Left, 1, 15, 15, 0);                        this.lbl_ToolBox_MouseDown(this.lbl_ToolBox, arg);                        this.lbl_ToolBox_MouseUp(this.lbl_ToolBox, arg);                    }                    this.pictureBox_zoom.Hide();                    this.lastMouseMoveTime = 0;                    UpdateCutInfoLabel(UpdateUIMode.None);                }            }        }
        /// <summary>        /// 工具栏按钮的宽度        /// </summary>        private int[] toolBoxsWidth = new int[] { 22, 22, 22, 22, 22, 22, 22, 22, 35, 22, 54 };        /// <summary>        /// 工具样式按钮的左侧边距        /// </summary>        private int[] toolBoxsLeft = new int[] { 7, 33, 59, 85, 111, 137, 171, 197, 223, 268, 294 };        /// <summary>        /// 工具栏按钮提示信息        /// </summary>        private string[] toolBoxsTip = new string[] { "矩形工具", "椭圆工具", "箭头工具", "画刷工具", "文字工具", "提示工具", "撤消编辑", "保存截图", "上传截图", "退出截图", "完成截图" };        /// <summary>        /// 工具栏绘制事件处理程序        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void lbl_ToolBox_Paint(object sender, PaintEventArgs e)        {            Bitmap bmp_lbl = new Bitmap(e.ClipRectangle.Width, e.ClipRectangle.Height);            Graphics g = Graphics.FromImage(bmp_lbl);            g.DrawImage(Properties.Resources.ToolsBox, e.ClipRectangle, e.ClipRectangle, GraphicsUnit.Pixel);            bool tipFlag = false;            for (int i = 0; i < toolBoxsLeft.Length; i++)            {                Rectangle fcs_Rect = new Rectangle(toolBoxsLeft[i], 3, toolBoxsWidth[i], toolHeight);                Rectangle sld_Rect = new Rectangle(toolBoxsLeft[i], 29, toolBoxsWidth[i], toolHeight);                if (fcs_Rect.Contains(this.mouseDownToolBoxLocation))                {                    g.DrawImage(Properties.Resources.ToolsBox, fcs_Rect, sld_Rect, GraphicsUnit.Pixel);                }                else                {                    if (fcs_Rect.Contains(this.mouseInToolBoxLocation))                    {                        g.DrawImage(Properties.Resources.ToolsBox, fcs_Rect, sld_Rect, GraphicsUnit.Pixel);                    }                }                //如果选中的是矩形工具,则根据子类型绘制按钮的图标                if (i == 0)                {                    Rectangle src_Rect = new Rectangle(0, 0, 22, 20);                    //绘制工具按钮的背景                    if (fcs_Rect.Contains(this.mouseDownToolBoxLocation))                    {                        g.DrawImage(Properties.Resources.ToolBox_Selected, fcs_Rect, src_Rect, GraphicsUnit.Pixel);                    }                    else                    {                        if (fcs_Rect.Contains(this.mouseInToolBoxLocation))                        {                            g.DrawImage(Properties.Resources.ToolBox_Selected, fcs_Rect, src_Rect, GraphicsUnit.Pixel);                        }                        else                        {                            g.DrawImage(Properties.Resources.ToolBox_Normal, fcs_Rect, src_Rect, GraphicsUnit.Pixel);                        }                    }                    Rectangle dst_Rect = new Rectangle(fcs_Rect.Left + 3, fcs_Rect.Top + 2, 16, 16);                    src_Rect = new Rectangle(0, 0, 16, 16);                    //根据子类型绘制按钮的图标                    switch (this.imageSubEditMode)                    {                        case ImageSubEditMode.Rectangle:                            g.DrawImage(Properties.Resources.Icon_Rectangle, dst_Rect, src_Rect, GraphicsUnit.Pixel);                            break;                        case ImageSubEditMode.CircularRectangle:                            g.DrawImage(Properties.Resources.Icon_Circular, dst_Rect, src_Rect, GraphicsUnit.Pixel);                            break; ;                        case ImageSubEditMode.Ellipse:                            g.DrawImage(Properties.Resources.Icon_Ellipse, dst_Rect, src_Rect, GraphicsUnit.Pixel);                            break;                        case ImageSubEditMode.Arrowhead:                            g.DrawImage(Properties.Resources.Icon_Arrowhead, dst_Rect, src_Rect, GraphicsUnit.Pixel);                            break;                        default: break;                    }                    //绘制三角块                    Rectangle dstRect_SanJiaoKuai = new Rectangle(fcs_Rect.Left + 15, fcs_Rect.Top + 15, 5, 3);                    Rectangle srcRect_SanJiaoKuai = new Rectangle(0, 0, 5, 3);                    g.DrawImage(Properties.Resources.SanJiaoKuai, dstRect_SanJiaoKuai, srcRect_SanJiaoKuai, GraphicsUnit.Pixel);                }                //==============================================================================================================                //如果选中的是提示工具,则根据子类型绘制按钮的图标                if (i == 5)                {                    Rectangle src_Rect = new Rectangle(0, 0, 22, 20);                    //绘制工具按钮的背景                    if (fcs_Rect.Contains(this.mouseDownToolBoxLocation))                    {                        g.DrawImage(Properties.Resources.ToolBox_Selected, fcs_Rect, src_Rect, GraphicsUnit.Pixel);                    }                    else                    {                        if (fcs_Rect.Contains(this.mouseInToolBoxLocation))                        {                            g.DrawImage(Properties.Resources.ToolBox_Selected, fcs_Rect, src_Rect, GraphicsUnit.Pixel);                        }                        else                        {                            g.DrawImage(Properties.Resources.ToolBox_Normal, fcs_Rect, src_Rect, GraphicsUnit.Pixel);                        }                    }                    Rectangle dst_Rect = new Rectangle(fcs_Rect.Left + 3, fcs_Rect.Top + 2, 16, 16);                    src_Rect = new Rectangle(0, 0, 16, 16);                    //根据子类型绘制按钮的图标                    switch (this.imageSubEditMode)                    {                        case ImageSubEditMode.Rectangle:                            g.DrawImage(Properties.Resources.Icon_ToolTips, dst_Rect, src_Rect, GraphicsUnit.Pixel);                            break;                        case ImageSubEditMode.CircularRectangle:                            g.DrawImage(Properties.Resources.Icon_ToolTips, dst_Rect, src_Rect, GraphicsUnit.Pixel);                            break; ;                        case ImageSubEditMode.Ellipse:                            g.DrawImage(Properties.Resources.Icon_ToolTips, dst_Rect, src_Rect, GraphicsUnit.Pixel);                            break;                        case ImageSubEditMode.Arrowhead:                            g.DrawImage(Properties.Resources.Icon_ToolTips, dst_Rect, src_Rect, GraphicsUnit.Pixel);                            break;                        default: break;                    }                    //绘制三角块                    Rectangle dstRect_SanJiaoKuai = new Rectangle(fcs_Rect.Left + 15, fcs_Rect.Top + 15, 5, 3);                    Rectangle srcRect_SanJiaoKuai = new Rectangle(0, 0, 5, 3);                    g.DrawImage(Properties.Resources.SanJiaoKuai, dstRect_SanJiaoKuai, srcRect_SanJiaoKuai, GraphicsUnit.Pixel);                }                //==============================================================================================================                if (fcs_Rect.Contains(this.mouseInToolBoxLocation))                {                    this.toolTip1.SetToolTip(this.lbl_ToolBox, toolBoxsTip[i]);                    tipFlag = true;                }            }            if (!tipFlag)            {                this.toolTip1.SetToolTip(this.lbl_ToolBox, "");            }            e.Graphics.DrawImage(bmp_lbl, 0, 0);            g.Dispose();            bmp_lbl.Dispose();        }

小结

主要用到的技术:区域绘制,按钮多态,编辑状态;