模仿QQ截图片
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 using System.Data.OleDb; 9 using System.Xml; 10 namespace Test 11 { 12 public partial class Form1 : Form 13 { 14 private bool MouseIsDown = false; 15 private int _x, _y; 16 private Rectangle MouseRect = Rectangle.Empty; 17 public Form1() 18 { 19 InitializeComponent(); 20 } 21 private void ResizeToRectangle(Point p) 22 { 23 DrawRectangle(); 24 MouseRect.Width = p.X - MouseRect.Left; 25 MouseRect.Height = p.Y - MouseRect.Top; 26 DrawRectangle(); 27 } 28 private void DrawRectangle() 29 { 30 Rectangle rect = this.RectangleToScreen(MouseRect); 31 ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed); 32 33 } 34 private void DrawStart(Point StartPoint) 35 { 36 barkPictureBox.Capture = true; 37 Cursor.Clip = this.RectangleToScreen(new Rectangle(0, 0, ClientSize.Width, ClientSize.Height)); 38 MouseRect = new Rectangle(StartPoint.X, StartPoint.Y, 0, 0); 39 } 40 41 42 private void button1_Click(object sender, EventArgs e) 43 { 44 45 } 46 47 48 void barkPictureBox_MouseUp(object sender, MouseEventArgs e) 49 { 50 barkPictureBox.Capture = false; 51 Cursor.Clip = Rectangle.Empty; 52 MouseIsDown = false; 53 DrawRectangle(); 54 55 if (MouseRect.X == 0 || MouseRect.Y == 0 || MouseRect.Width == 0 || MouseRect.Height == 0) //如果区域有一个是0就返回 56 { 57 } 58 else 59 { 60 ///代码 61 } 62 63 MouseRect = Rectangle.Empty; 64 int sx = _x < e.X ? _x : e.X; 65 int sy = _y < e.Y ? _y : e.Y; 66 int w = Math.Abs(_x - e.X); 67 int h = Math.Abs(_y - e.Y); 68 Graphics g = Graphics.FromHwnd(pictureBox2.Handle); 69 g.Clear(pictureBox2.BackColor); 70 g.DrawImage(barkPictureBox.Image, new Rectangle(0, 0, w, h), sx, sy, w, h, GraphicsUnit.Pixel); 71 } 72 73 void barkPictureBox_MouseMove(object sender, MouseEventArgs e) 74 { 75 if (MouseIsDown) ResizeToRectangle(new Point(e.X + barkPictureBox.Location.X, e.Y + barkPictureBox.Location.Y)); 76 77 } 78 79 void barkPictureBox_MouseDown(object sender, MouseEventArgs e) 80 { 81 _x = e.X; 82 _y = e.Y; 83 84 MouseIsDown = true; 85 DrawStart(new Point(e.X + barkPictureBox.Location.X, e.Y + barkPictureBox.Location.Y)); 86 } 87 88 89 90 91 92 93 private void Form1_Load(object sender, EventArgs e) 94 { 95 barkPictureBox.MouseDown += new MouseEventHandler(barkPictureBox_MouseDown); 96 barkPictureBox.MouseMove += new MouseEventHandler(barkPictureBox_MouseMove); 97 barkPictureBox.MouseUp += new MouseEventHandler(barkPictureBox_MouseUp); 98 99 } 100 } 101 }