编写一个类似qq的屏幕捕捉程序

昨天晚上搞了很久,其实是很无聊

随便做了个程序,实现了像qq一样的抓屏功能

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace gowk.forms
{
 /// <summary>
 /// CaptureForm 的摘要说明。
 /// </summary>
 public class CaptureForm : System.Windows.Forms.Form
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  Point op;
  Rectangle area=Rectangle.Empty;
  private Image img;
  public CaptureForm()
  {
   this.SetStyle(ControlStyles.DoubleBuffer|ControlStyles.AllPaintingInWmPaint|ControlStyles.UserPaint,true);
   InitializeComponent();
   this.Bounds=System.Windows.Forms.Screen.PrimaryScreen.Bounds;

   this.BackgroundImage=this.GetDestopImage();
  }
  public Image Image
  {
   get{return this.img;}
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  private Image GetDestopImage()
  {
   Rectangle area=System.Windows.Forms.Screen.PrimaryScreen.Bounds;
   Bitmap bm=new Bitmap(area.Width,area.Height);
   Graphics g=Graphics.FromImage(bm);

   System.IntPtr p=g.GetHdc();
   IntPtr c=GetDesktopWindow();
   //gowk.common.API.SendMessage(c,gowk.common.API.WM_PRINT,p,new IntPtr(gowk.common.API.PRF_CLIENT|gowk.common.API.PRF_NONCLIENT|gowk.common.API.PRF_CHILDREN|gowk.common.API.PRF_ERASEBKGND));
   System.IntPtr ddc=GetDC(c);
   gowk.common.API.BitBlt(p,0,0,this.Width,this.Height,ddc,0,0,gowk.common.API.SRCCOPY);
   gowk.common.API.ReleaseDC(c,ddc);
   g.ReleaseHdc(p);
   return bm;
  }
  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   //
   // CaptureForm
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(292, 273);
   this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
   this.Name = "CaptureForm";
   this.Text = "CaptureForm";
   this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
   this.Click += new System.EventHandler(this.CaptureForm_Click);
   this.DoubleClick += new System.EventHandler(this.CaptureForm_DoubleClick);

  }
  #endregion
  
  [DllImport("user32.dll",EntryPoint="GetDC")]
  public static extern IntPtr GetDC(IntPtr ptr);
  [DllImport("user32.dll", CharSet=CharSet.Auto)]
  public static extern IntPtr GetDesktopWindow();

  private void CaptureForm_DoubleClick(object sender, System.EventArgs e)
  {
   Bitmap bm=new Bitmap(this.BackgroundImage);
   this.img=bm.Clone(this.area,System.Drawing.Imaging.PixelFormat.Format16bppArgb1555);
   this.DialogResult=DialogResult.OK;
  }

  int index=-1;
  protected override void OnMouseDown(MouseEventArgs e)
  {
   base.OnMouseDown (e);
   if(this.area==Rectangle.Empty && e.Button==MouseButtons.Left)
   {
    this.area.Location=new Point(e.X,e.Y);
   }
   this.op=new Point(e.X,e.Y);
   this.index=this.GetSelectedHandle(new Point(e.X,e.Y));
   this.SetCursor();
  }

  private int GetSelectedHandle(Point p)
  {
   int index=-1;
   for(int i=1;i<9;i++)
   {
    if(GetHandleRect(i).Contains(p))
    {
     index=i;
     break;
    }
   }
   if(this.area.Contains(p))index=0;
   System.Diagnostics.Trace.WriteLine(area.ToString());
   System.Diagnostics.Trace.WriteLine(p.ToString());
   System.Diagnostics.Trace.WriteLine(index.ToString());
   return index;
  }
  protected override void OnMouseMove(MouseEventArgs e)
  {
   base.OnMouseMove (e);
   if(this.Capture)
   {
    this.MoveHandleTo(new Point(e.X,e.Y));
    this.Invalidate();
   }
   else
   {
    this.index=this.GetSelectedHandle(new Point(e.X,e.Y));
    this.SetCursor();
   }
  }
  private  void MoveHandleTo(Point point)
  {
   int left = area.Left;
   int top = area.Top;
   int right = area.Right;
   int bottom = area.Bottom;

   switch ( index )
   {
    case 0:
     area.X+=point.X-op.X;
     area.Y+=point.Y-op.Y;
     this.op=point;
     return;
    case 1:
     left = point.X;
     top = point.Y;
     break;
    case 2:
     top = point.Y;
     break;
    case 3:
     right = point.X;
     top = point.Y;
     break;
    case 4:
     right = point.X;
     break;
    case 5:
     right = point.X;
     bottom = point.Y;
     break;
    case 6:
     bottom = point.Y;
     break;
    case 7:
     left = point.X;
     bottom = point.Y;
     break;
    case 8:
     left = point.X;
     break;
   }
   this.op=point;
   area.X=left;
   area.Y=top;
   area.Width=right-left;
   area.Height=bottom-top;
  }
  private void SetCursor()
  {
   Cursor cr=Cursors.Default;
   if(index==1 ||index==5)
   {
    cr=Cursors.SizeNWSE;
   }
   else if(index==2 ||index==6)
   {
    cr=Cursors.SizeNS;
   }
   else if(index==3 ||index==7)
   {
    cr=Cursors.SizeNESW;
   }
   else if(index==4 ||index==8)
   {
    cr=Cursors.SizeWE;
   }
   else if(index==0)
   {
    cr=Cursors.SizeAll;
   }
   Cursor.Current=cr;
  }

  protected override void OnMouseUp(MouseEventArgs e)
  {
   base.OnMouseUp (e);
   int left = area.Left;
   int top = area.Top;
   int right = area.Right;
   int bottom = area.Bottom;
   area.X=Math.Min(left,right);
   area.Y=Math.Min(top,bottom);
   area.Width=Math.Abs(left-right);
   area.Height=Math.Abs(top-bottom);
   if(e.Button==MouseButtons.Right)
   {
    if(this.area==Rectangle.Empty)
    {
     this.DialogResult=DialogResult.Cancel;
    }
    else
    {
     this.area=Rectangle.Empty;
     this.Invalidate();
    }
   }
   this.index=this.GetSelectedHandle(new Point(e.X,e.Y));
   this.SetCursor();
  }

  protected override void OnPaint(PaintEventArgs e)
  {
   //  base.OnPaint (e);
   base.OnPaint(e);
   e.Graphics.DrawRectangle(new Pen(this.ForeColor),this.area);
   
   for(int i=1;i<9;i++)
   {
    e.Graphics.FillRectangle(new SolidBrush(Color.Red),this.GetHandleRect(i));
   }
  }
  private Rectangle GetHandleRect(int index)
  {
   Point point = GetHandle(index);
   return new Rectangle(point.X - 3, point.Y - 3, 7, 7);
  }
  private  Point GetHandle(int index)
  {
   int x, y, xCenter, yCenter;

   xCenter = area.X + area.Width/2;
   yCenter = area.Y + area.Height/2;
   x = area.X;
   y = area.Y;

   switch ( index )
   {
    case 1:
     x = area.X;
     y = area.Y;
     break;
    case 2:
     x = xCenter;
     y = area.Y;
     break;
    case 3:
     x = area.Right;
     y = area.Y;
     break;
    case 4:
     x = area.Right;
     y = yCenter;
     break;
    case 5:
     x = area.Right;
     y = area.Bottom;
     break;
    case 6:
     x = xCenter;
     y = area.Bottom;
     break;
    case 7:
     x = area.X;
     y = area.Bottom;
     break;
    case 8:
     x = area.X;
     y = yCenter;
     break;
   }

   return new Point(x, y);

  }
  private void CaptureForm_Click(object sender, System.EventArgs e)
  {
  }

 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值