截图功能实现(二)如何框选区域

用过QQ截图的人都知道在我们按下快捷键截图时,我们可以用鼠标随意框选我们需要的区域,而且选中的区域变成高亮,今天就和大家说说这事如何实现的。
1.橡皮筋类的实现
 
    网上有一个橡皮筋类能够实现这种效果,如果急着用又不想自己写可以网上下过来试试,但是今天说说它的原理,其实框选区域就是在你鼠标左键按下到你左键抬起这个过程中鼠标移动的的这块矩形区域
关于截图功能实现分享(二)如何框选截图区域

      如图起点为左上角,终点为右下角,其实我们再框选时,先记录起点,终点为鼠标移动时就是onmousemove时的点这样看起来就好像矩形区域跟随鼠标在调整大小,到鼠标抬起时记录下点。这里我们把起点记为m_ptBegin,终点记为m_ptEnd。图中还标明其他几个点这几个点是可拖拽区域,就像QQ一样鼠标放上去会改变鼠标样子
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 C# 实现框选截图功能的代码示例: ```csharp using System; using System.Drawing; using System.Windows.Forms; public class ScreenCapture : Form { private Point start; private Rectangle rect; public ScreenCapture() { this.DoubleBuffered = true; this.FormBorderStyle = FormBorderStyle.None; this.WindowState = FormWindowState.Maximized; this.Cursor = Cursors.Cross; this.MouseDown += ScreenCapture_MouseDown; this.MouseMove += ScreenCapture_MouseMove; this.MouseUp += ScreenCapture_MouseUp; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); using (Pen pen = new Pen(Color.Red, 2)) { e.Graphics.DrawRectangle(pen, rect); } } private void ScreenCapture_MouseDown(object sender, MouseEventArgs e) { start = new Point(e.X, e.Y); rect = new Rectangle(start, Size.Empty); } private void ScreenCapture_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { Point current = new Point(e.X, e.Y); rect.Location = new Point( Math.Min(start.X, current.X), Math.Min(start.Y, current.Y)); rect.Size = new Size( Math.Abs(start.X - current.X), Math.Abs(start.Y - current.Y)); Invalidate(); } } private void ScreenCapture_MouseUp(object sender, MouseEventArgs e) { if (rect.Width > 0 && rect.Height > 0) { using (Bitmap bitmap = new Bitmap(rect.Width, rect.Height)) { using (Graphics graphics = Graphics.FromImage(bitmap)) { graphics.CopyFromScreen(rect.Location, Point.Empty, rect.Size); } Clipboard.SetImage(bitmap); MessageBox.Show("截图已复制到剪贴板。"); } } Close(); } public static void Main() { Application.Run(new ScreenCapture()); } } ``` 这个程序创建了一个全屏的窗体,并在窗体上绘制了一个红色的矩形,用于框选截图。当用户按下鼠标左键并拖动时,矩形会跟随鼠标移动。当用户释放鼠标左键时,程序会将框选区域的屏幕截图复制到剪贴板,并关闭窗体。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值