C#开发截图工具,使用到的重要对象 System.Windows.Form.NotifyIcon using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Imaging; namespace Com.DongPad { public partial class Form_Screenshots : Form { #region 构造函数 public Form_Screenshots() { GetScreenBmp(); InitializeComponent(); CustomInitialize(); } #endregion #region 私有变量 Bitmap bmp = null; /// <summary> /// 鼠标是否按下 /// </summary> bool isMouseDown = false; /// <summary> /// 得到的结果 /// </summary> string website = "http://blog.youkuaiyun.com/david1030"; string line = "------------------------------------"; string email = "david1030@126.com"; Point startPoint = Point.Empty;//鼠标起始点 Point midPoint = Point.Empty;//中间变量 Point endPoint = Point.Empty;//鼠标终止点 Rectangle selectRegion = Rectangle.Empty;//选中的区域 Rectangle mouseRec = Rectangle.Empty; Rectangle[] smallRecions = new Rectangle[8]; Color selectRegionBC = Color.FromArgb(10, 100, 130);//border color bool isMoveRegion = false; #endregion #region 本地初始化 private void GetScreenBmp() { Screen pS = Screen.PrimaryScreen; if (bmp == null) bmp = new Bitmap(pS.Bounds.Width, pS.Bounds.Height); Graphics g = Graphics.FromImage(bmp); g.CopyFromScreen(Point.Empty, Point.Empty, pS.Bounds.Size); g.DrawImage(bmp, Point.Empty); this.BackgroundImage = bmp; } private void CustomInitialize() { SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲 this.WindowState = FormWindowState.Maximized; this.FormBorderStyle = FormBorderStyle.None; this.TopMost = true; this.Location = Point.Empty; this.StartPosition = FormStartPosition.Manual; this.ShowInTaskbar = false; this.ControlBox = false; this.Cursor = Cursors.Cross; this.MaximizeBox = false; this.MinimizeBox = false; this.Size = bmp.Size; //注册mouse this.MouseMove += new MouseEventHandler(Form_Color_taking_MouseMove); this.MouseDown += new MouseEventHandler(Form_Color_taking_MouseDown); this.MouseUp += new MouseEventHandler(Form_Color_taking_MouseUp); this.DoubleClick += new EventHandler(Form_Screenshots_DoubleClick); this.Paint += new PaintEventHandler(Form_Screenshots_Paint); // this.notifyIcon_task.DoubleClick += new EventHandler(notifyIcon_task_DoubleClick); } void notifyIcon_task_DoubleClick(object sender, EventArgs e) { notifyIcon_task.ShowBalloonTip(2000, "Screenshots", string.Format("如果有什么疑问,请到{0}联系我吧!/r/n也可以給我发mail:{1}!/r/n恢复功能请点击右键!", website, email), ToolTipIcon.Info); } #endregion #region 截屏画板 void Form_Screenshots_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; if (isMouseDown || selectRegion!= Rectangle.Empty) { using (Pen sPen = new Pen(selectRegionBC)) { g.DrawRectangle(sPen, selectRegion); //画出选中区域 } if (isMouseDown) g.FillRectangle(new SolidBrush(Color.FromArgb(160, 244, 244, 255)), selectRegion); using (SolidBrush sBrush = new SolidBrush(selectRegionBC)) { //左上 中上 右上 //左中 右中 //左下 中下 右下 //在如上位置画出点阵区域,用于拉伸选中区域 smallRecions[0] = new Rectangle(selectRegion.X - 2, selectRegion.Y - 2, 8, 8);//左上 smallRecions[1] = new Rectangle(selectRegion.X + selectRegion.Width / 2 - 2, selectRegion.Y - 2, 5, 5);//中上 smallRecions[2] = new Rectangle(selectRegion.X + selectRegion.Width - 2, selectRegion.Y - 2, 5, 5);//右上 smallRecions[3] = new Rectangle(selectRegion.X - 2, selectRegion.Y + selectRegion.Height / 2 - 2, 5, 5);//左中 smallRecions[4] = new Rectangle(selectRegion.X + selectRegion.Width - 2, selectRegion.Y + selectRegion.Height / 2 - 2, 5, 5);//右中 smallRecions[5] = new Rectangle(selectRegion.X - 2, selectRegion.Y + selectRegion.Height - 2, 5, 5);//左下 smallRecions[6] = new Rectangle(selectRegion.X + selectRegion.Width / 2 - 2, selectRegion.Y + selectRegion.Height - 2, 5, 5);//中下 smallRecions[7] = new Rectangle(selectRegion.X + selectRegion.Width - 2, selectRegion.Y + selectRegion.Height - 2, 5, 5);//右下 for (int i = 0; i < smallRecions.Length; i++) g.FillRectangle(sBrush, smallRecions[i]); } } } #endregion #region 双击保存事件 void Form_Screenshots_DoubleClick(object sender, EventArgs e) { if (selectRegion != Rectangle.Empty && selectRegion.IntersectsWith(mouseRec)) { try { Bitmap partImg = new Bitmap(selectRegion.Width, selectRegion.Height); Graphics graphics = Graphics.FromImage(partImg); Rectangle destRect = new Rectangle(new Point(0,0), new Size(selectRegion.Width, selectRegion.Height));//目标位置 Rectangle origRect = new Rectangle(new Point(selectRegion.X, selectRegion.Y), new Size(selectRegion.Width, selectRegion.Height));//原图位置(默认从原图中截取的图片大小等于目标图片的大小) graphics.DrawImage(bmp, destRect, origRect, GraphicsUnit.Pixel); partImg.Save( string.Format("part_{0}.jpg", DateTime.Now.ToString("yyyyMMddHHmmss")), ImageFormat.Jpeg); Clipboard.SetDataObject(partImg, true); } catch { } selectRegion = Rectangle.Empty; this.Cursor = Cursors.Cross; this.Visible = false; } } #endregion #region 鼠标选取事件 void Form_Color_taking_MouseUp(object sender, MouseEventArgs e) { if (isMouseDown) isMouseDown = false; if (isMoveRegion) isMoveRegion = false; this.Invalidate(); } void Form_Color_taking_MouseDown(object sender, MouseEventArgs e) { if (!isMouseDown && selectRegion == Rectangle.Empty) { this.Cursor = Cursors.Cross; startPoint = this.PointToClient(MousePosition); isMouseDown = true; } else if (selectRegion != Rectangle.Empty) { //已经选择了待截取区域 startPoint = this.PointToClient(MousePosition); isMoveRegion = true; } } void Form_Color_taking_MouseMove(object sender, MouseEventArgs e) { if (isMouseDown )//鼠标按下用于选择待截取区域 { midPoint = this.PointToClient(MousePosition); if (midPoint != endPoint) { endPoint = midPoint; selectRegion.Location = startPoint; selectRegion.Width = Math.Abs (endPoint.X - startPoint.X); selectRegion.Height = Math.Abs (endPoint.Y - startPoint.Y); this.Invalidate(); } } else if (selectRegion != Rectangle.Empty)//已经选好了待截取的区域, { midPoint= this.PointToClient(MousePosition); if (midPoint != endPoint) { mouseRec = new Rectangle(midPoint.X, midPoint.Y, 1, 1); if (selectRegion.IntersectsWith(mouseRec)) { this.Cursor = Cursors.SizeAll; startPoint = endPoint; endPoint = midPoint; if (isMoveRegion)//拖动截取的区域 { selectRegion.Offset(-startPoint.X + endPoint.X, -startPoint.Y + endPoint.Y); this.Invalidate(); } } else//没有交集 { this.Cursor = Cursors.Cross; } } } } #endregion #region 预处理命令键(Esc) /// <summary> /// 预处理命令键, /// </summary> /// <param name="msg"></param> /// <param name="keyData"></param> /// <returns></returns> protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == Keys.Escape) { this.Visible = false; return true; } else { MessageBox.Show("鼠标双击截取,点击Esc退出。"); } return base.ProcessCmdKey(ref msg, keyData); } #endregion #region 任务栏菜单选择 private void toolStripMenuItem_quit_Click(object sender, EventArgs e) { //程序退出后,任务栏图标并没有立即消失 //所以在这里强制设置其不可见 this.notifyIcon_task.Visible = false; System.Environment.Exit(0); } private void toolStripMenuItem_show_Click(object sender, EventArgs e) { selectRegion = Rectangle.Empty; this.Visible = true; } #endregion } }