C#软件开发实例.私人订制自己的屏幕截图工具(八)添加键盘操作截图的功能

本实例全部文章目录

虽然添加了放大镜的功能,但是在进行像素级的定位时,还是不容易精确定位,在用鼠标操作时要改变一两个像素的位置还是有些困难的。

处理键盘按下事件

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /// <summary>  
  2. /// 处理键盘按下事件  
  3. /// 用于实现以下功能:  
  4. /// 当用户按下Esc键时,退出截图过程;  
  5. /// Shift + Enter 开始截图的功能;  
  6. /// 使用键盘的上下左右键调整截图位置的功能;  
  7. /// Shift + 上下左右键调整截图区域大小的功能;  
  8. /// </summary>  
  9. /// <param name="sender"></param>  
  10. /// <param name="e"></param>  
  11. private void Form1_KeyDown(object sender, KeyEventArgs e)  
  12. {  
  13.     if (e.KeyCode == Keys.Escape)  
  14.     {  
  15.         ExitCutImage(true);  
  16.         // 如果不加这一句,热键只能在窗口隐藏后使用一次,之后就不起作用了。  
  17.         //RegisterHotKey(Handle, 100, 2 | 1, Keys.A);  
  18.     }  
  19.     if (e.Shift && e.KeyCode == Keys.Enter)  
  20.     {  
  21.         if (!this.lbl_CutImage.Visible)  
  22.         {  
  23.             this.isCuting = true;  
  24.             this.beginPoint = MousePosition;  
  25.             this.endPoint = MousePosition;  
  26.             SaveCutImageSize(MousePosition, MousePosition);  
  27.             UpdateCutInfoLabel(UpdateUIMode.ShowInfoBox | UpdateUIMode.ShowCutImage);  
  28.         }  
  29.     }  
  30.     if (e.KeyCode == Keys.Left)  
  31.     {  
  32.         if (this.lbl_CutImage.Visible)  
  33.         {  
  34.             if (e.Shift)  
  35.             {  
  36.                 if (this.cutImageRect.Width > 1)  
  37.                 {  
  38.                     this.cutImageRect.Width -= 1;  
  39.                     Cursor.Position = new Point(Cursor.Position.X - 1, Cursor.Position.Y);  
  40.                     UpdateCutInfoLabel(UpdateUIMode.None);  
  41.                 }  
  42.             }  
  43.             else  
  44.             {  
  45.                 if (this.cutImageRect.Left > -1)  
  46.                 {  
  47.                     this.cutImageRect.X -= 1;  
  48.                     UpdateCutInfoLabel(UpdateUIMode.None);  
  49.                 }  
  50.             }  
  51.         }  
  52.         else  
  53.         {  
  54.             if (Cursor.Position.X > -1)  
  55.             {  
  56.                 Cursor.Position = new Point(Cursor.Position.X - 1, Cursor.Position.Y);  
  57.             }  
  58.         }  
  59.     }  
  60.     if (e.KeyCode == Keys.Right)  
  61.     {  
  62.         if (this.lbl_CutImage.Visible)  
  63.         {  
  64.             if (e.Shift)  
  65.             {  
  66.                 if (this.cutImageRect.Right < this.Width + 1)  
  67.                 {  
  68.                     this.cutImageRect.Width += 1;  
  69.                     Cursor.Position = new Point(Cursor.Position.X + 1, Cursor.Position.Y);  
  70.                     UpdateCutInfoLabel(UpdateUIMode.None);  
  71.                 }  
  72.             }  
  73.             else  
  74.             {  
  75.                 if (this.cutImageRect.Right < this.Width + 1)  
  76.                 {  
  77.                     this.cutImageRect.X += 1;  
  78.                     UpdateCutInfoLabel(UpdateUIMode.None);  
  79.                 }  
  80.             }  
  81.         }  
  82.         else  
  83.         {  
  84.             if (Cursor.Position.X < this.Width + 1)  
  85.             {  
  86.                 Cursor.Position = new Point(Cursor.Position.X + 1, Cursor.Position.Y);  
  87.             }  
  88.         }  
  89.     }  
  90.   
  91.     if (e.KeyCode == Keys.Up)  
  92.     {  
  93.         if (this.lbl_CutImage.Visible)  
  94.         {  
  95.             if (e.Shift)  
  96.             {  
  97.                 if (this.cutImageRect.Height > 1)  
  98.                 {  
  99.                     this.cutImageRect.Height -= 1;  
  100.                     Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y - 1);  
  101.                     UpdateCutInfoLabel(UpdateUIMode.None);  
  102.                 }  
  103.             }  
  104.             else  
  105.             {  
  106.                 if (this.cutImageRect.Top > -1)  
  107.                 {  
  108.                     this.cutImageRect.Y -= 1;  
  109.                     UpdateCutInfoLabel(UpdateUIMode.None);  
  110.                 }  
  111.             }  
  112.         }  
  113.         else  
  114.         {  
  115.             if (Cursor.Position.Y > -1)  
  116.             {  
  117.                 Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y - 1);  
  118.             }  
  119.         }  
  120.     }  
  121.     if (e.KeyCode == Keys.Down)  
  122.     {  
  123.         if (this.lbl_CutImage.Visible)  
  124.         {  
  125.             if (e.Shift)  
  126.             {  
  127.                 if (this.cutImageRect.Bottom < this.Height + 1)  
  128.                 {  
  129.                     this.cutImageRect.Height += 1;  
  130.                     Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y + 1);  
  131.                     UpdateCutInfoLabel(UpdateUIMode.None);  
  132.                 }  
  133.             }  
  134.             else  
  135.             {  
  136.                 if (this.cutImageRect.Bottom < this.Height + 1)  
  137.                 {  
  138.                     this.cutImageRect.Y += 1;  
  139.                     UpdateCutInfoLabel(UpdateUIMode.None);  
  140.                 }  
  141.             }  
  142.         }  
  143.         else  
  144.         {  
  145.             if (Cursor.Position.Y < this.Height + 1)  
  146.             {  
  147.                 Cursor.Position = new Point(Cursor.Position.X, Cursor.Position.Y + 1);  
  148.             }  
  149.         }  
  150.     }  
  151. }  

处理键盘抬起事件

[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /// <summary>  
  2. /// 处理键盘抬起事件  
  3. /// Shift + Enter 开始截图,当松开Shitf键后,  
  4. /// 停止截图区域大小的设置,不然的话鼠标移动还会改变截取区域的大小;  
  5. /// </summary>  
  6. /// <param name="sender"></param>  
  7. /// <param name="e"></param>  
  8. private void Form1_KeyUp(object sender, KeyEventArgs e)  
  9. {  
  10.     if (e.KeyCode == Keys.ShiftKey)  
  11.     {  
  12.         if (this.isCuting)  
  13.         {  
  14.             this.isCuting = false;  
  15.             this.pictureBox_zoom.Hide();  
  16.   
  17.             this.lastMouseMoveTime = 0;  
  18.             UpdateCutInfoLabel(UpdateUIMode.None);  
  19.         }  
  20.     }  
  21. }  

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

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

这时你可以通过上下左右键来改变截图区域的位置,按下Shift键不要松开,再按上下左右键可以改变截图区域的大小。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值