前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#软件开发实例.个人定制自己的屏幕抓图工具(八)加入了截图功能键盘

C#软件开发实例.个人定制自己的屏幕抓图工具(八)加入了截图功能键盘

作者头像
全栈程序员站长
发布2022-07-06 08:56:17
3710
发布2022-07-06 08:56:17
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君。

章文件夹

(一)功能概览

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

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

(四)基本截图功能实现

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

(六)加入配置管理功能

(七)加入放大镜的功能

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

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

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

尽管加入了放大镜的功能,可是在进行像素级的定位时,还是不easy精确定位,在用鼠标操作时要改变一两个像素的位置还是有些困难的。

处理键盘按下事件

代码语言:javascript
复制
        /// <summary>
        /// 处理键盘按下事件
        /// 用于实现下面功能:
        /// 当用户按下Esc键时,退出截图过程;
        /// Shift + Enter 開始截图的功能;
        /// 使用键盘的上下左右键调整截图位置的功能。
        /// Shift + 上下左右键调整截图区域大小的功能;
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                ExitCutImage(true);
                // 假设不加这一句。热键仅仅能在窗体隐藏后使用一次,之后就不起作用了。
                //RegisterHotKey(Handle, 100, 2 | 1, Keys.A);
            }
            if (e.Shift && e.KeyCode == Keys.Enter)
            {
                if (!this.lbl_CutImage.Visible)
                {
                    this.isCuting = true;
                    this.beginPoint = MousePosition;
                    this.endPoint = MousePosition;
                    SaveCutImageSize(MousePosition, MousePosition);
                    UpdateCutInfoLabel(UpdateUIMode.ShowInfoBox | UpdateUIMode.ShowCutImage);
                }
            }
            if (e.KeyCode == Keys.Left)
            {
                if (this.lbl_CutImage.Visible)
                {
                    if (e.Shift)
                    {
                        if (this.cutImageRect.Width > 1)
                        {
                            this.cutImageRect.Width -= 1;
                            Cursor.Position = new Point(Cursor.Position.X - 1, Cursor.Position.Y);
                            UpdateCutInfoLabel(UpdateUIMode.None);
                        }
                    }
                    else
                    {
                        if (this.cutImageRect.Left > -1)
                        {
                            this.cutImageRect.X -= 1;
                            UpdateCutInfoLabel(UpdateUIMode.None);
                        }
                    }
                }
                else
                {
                    if (Cursor.Position.X > -1)
                    {
                        Cursor.Position = new Point(Cursor.Position.X - 1, Cursor.Position.Y);
                    }
                }
            }
            if (e.KeyCode == Keys.Right)
            {
                if (this.lbl_CutImage.Visible)
                {
                    if (e.Shift)
                    {
                        if (this.cutImageRect.Right < this.Width + 1)
                        {
                            this.cutImageRect.Width += 1;
                            Cursor.Position = new Point(Cursor.Position.X + 1, Cursor.Position.Y);
                            UpdateCutInfoLabel(UpdateUIMode.None);
                        }
                    }
                    else
                    {
                        if (this.cutImageRect.Right < this.Width + 1)
                        {
                            this.cutImageRect.X += 1;
                            UpdateCutInfoLabel(UpdateUIMode.None);
                        }
                    }
                }
                else
                {
                    if (Cursor.Position.X < this.Width + 1)
                    {
                        Cursor.Position = new Point(Cursor.Position.X + 1, Cursor.Position.Y);
                    }
                }
            }

            if (e.KeyCode == Keys.Up)
            {
                if (this.lbl_CutImage.Visible)
                {
                    if (e.Shift)
                    {
                        if (this.cutImageRect.Height > 1)
                        {
                            this.cutImageRect.Height -= 1;
                            Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y - 1);
                            UpdateCutInfoLabel(UpdateUIMode.None);
                        }
                    }
                    else
                    {
                        if (this.cutImageRect.Top > -1)
                        {
                            this.cutImageRect.Y -= 1;
                            UpdateCutInfoLabel(UpdateUIMode.None);
                        }
                    }
                }
                else
                {
                    if (Cursor.Position.Y > -1)
                    {
                        Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y - 1);
                    }
                }
            }
            if (e.KeyCode == Keys.Down)
            {
                if (this.lbl_CutImage.Visible)
                {
                    if (e.Shift)
                    {
                        if (this.cutImageRect.Bottom < this.Height + 1)
                        {
                            this.cutImageRect.Height += 1;
                            Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y + 1);
                            UpdateCutInfoLabel(UpdateUIMode.None);
                        }
                    }
                    else
                    {
                        if (this.cutImageRect.Bottom < this.Height + 1)
                        {
                            this.cutImageRect.Y += 1;
                            UpdateCutInfoLabel(UpdateUIMode.None);
                        }
                    }
                }
                else
                {
                    if (Cursor.Position.Y < this.Height + 1)
                    {
                        Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y + 1);
                    }
                }
            }
        }

处理键盘抬起事件

代码语言:javascript
复制
        /// <summary>
        /// 处理键盘抬起事件
        /// Shift + Enter 開始截图。当松开Shitf键后。
        /// 停止截图区域大小的设置。不然的话鼠标移动还会改变截取区域的大小;
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.ShiftKey)
            {
                if (this.isCuting)
                {
                    this.isCuting = false;
                    this.pictureBox_zoom.Hide();

                    this.lastMouseMoveTime = 0;
                    UpdateCutInfoLabel(UpdateUIMode.None);
                }
            }
        }

用键盘操作截图的功能说明:

按下截图快捷键(一般是:Ctrl + Shift + A)后,能够移动鼠标到大概的位置。然后就能够通过键盘的上下左右键精确移动鼠标的位置,在精确定位截图的位置后,就能够按下Shift 键再按 Enter键。Shift键不要松开,这时能够按上下左右键改变截图区域的大小。松开Shift键完毕截图区域大小设置。

这时你能够通过上下左右键以改变截图区域的位置,按Shift不要松开按键,按箭头键来改变拍摄区域的大小。

版权声明:本文博客原创文章,博客,未经同意,不得转载。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117479.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月5,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 章文件夹
    • (一)功能概览
      • (二)创建项目、注冊热键、显示截图主窗体
        • (三)托盘图标及菜单的实现
          • (四)基本截图功能实现
            • (五)针对拖拽时闪烁卡顿现象的优化
              • (六)加入配置管理功能
                • (七)加入放大镜的功能
                  • (八)加入键盘操作截图的功能
                    • (九)使用自己定义光标,QQ截图时的光标
                      • (十)在截图中包括鼠标指针形状
                        • 处理键盘按下事件
                          • 处理键盘抬起事件
                            • 用键盘操作截图的功能说明:
                            领券
                            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档