代码改变世界

转类似qq截图功能

2012-01-05 16:24  枫桥夜泊日  阅读(215)  评论(0编辑  收藏  举报

1      /// <summary>
        /// 截图功能按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnCutImage_Click(object sender, EventArgs e)
        {
            isDownCutBTN = true;
            Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
            Graphics g = Graphics.FromImage(img);
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
            ScreenBody body = new ScreenBody(this);
            body.BackgroundImage = img;
            body.ShowDialog();
        }

 

2    /// <summary>
      /// ScreenBody类  

     /// </summary> 

     public  partial class ScreenBody : Form
     {
        private Graphics MainPainter;  //主画笔
        private Pen pen;               //就是笔咯
        private bool isDowned;         //判断鼠标是否按下
        private bool RectReady;         //矩形是否绘制完成
        private Image baseImage;       //基本图形(原来的画面)
        public static Rectangle Rect;        //就是要保存的矩形
        private Point downPoint;        //鼠标按下的点
        int tmpx;
        int tmpy;
        private bool IsFormSaveOpen=false;    //保存窗口是否打开
        private frmQQ _frmQQ;  
        //保存窗口的宽度
        private const int FormSaveWidth=405;
        //保存窗口与截图中间的宽度
        private const int FormSaveheight = 4;
        public ScreenBody(frmQQ frmQQ)
        {
            InitializeComponent();
            this._frmQQ = frmQQ;
        }

        private void ScreenBody_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            MainPainter = this.CreateGraphics();
            pen = new Pen(Brushes.Blue);
            isDowned = false;
            baseImage = this.BackgroundImage;
            Rect = new Rectangle();
            RectReady = false;
        }

        /// <summary>
        /// 鼠标双击保存截图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScreenBody_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (((MouseEventArgs)e).Button == MouseButtons.Left && Rect.Contains(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y))
            {
                SaveFileDialog savafil = new SaveFileDialog();
                savafil.FileName = DateTime.Now.ToString("yyyyMMddhhmmss");//用时间创建图片名称
                savafil.Filter = "Image Files(*.JPG;*.GIF)|*.JPG;*.GIF|All files(*.*)|*.*";
                if (savafil.ShowDialog() == DialogResult.OK)
                {
                    Image memory = new Bitmap(Rect.Width, Rect.Height);
                    Graphics g = Graphics.FromImage(memory);
                    g.CopyFromScreen(Rect.X + 1, Rect.Y + 1, 0, 0, Rect.Size);
                    Clipboard.SetImage(memory);
                    memory.Save(savafil.FileName);
                    this.Close();
                }
            }
        }

        /// <summary>
        /// 按下鼠标产生的特效
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScreenBody_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDowned = true;

                if (RectReady == false)
                {
                    Rect.X = e.X;
                    Rect.Y = e.Y;
                    downPoint = new Point(e.X, e.Y);
                }
                if (RectReady == true)
                {
                    tmpx = e.X;
                    tmpy = e.Y;
                }
            }
            if (e.Button == MouseButtons.Right)
            {
                if (RectReady != true)
                {
                    this.Close();
                    return;
                }
                MainPainter.DrawImage(baseImage, 0, 0);
                RectReady = false;
            }

        }

        /// <summary>
        /// 当鼠标松开后!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScreenBody_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDowned = false;
                RectReady = true;

                if (!IsFormSaveOpen && _frmQQ.isDownCutBTN)
                {
                    Point point = new Point(e.X-FormSaveWidth, e.Y+FormSaveheight);
                    FormSave formSave = new FormSave(this,point,this._frmQQ);
                    formSave.Location = point;
                    formSave.Show();
                    IsFormSaveOpen = true;
                }
            }
        }

        /// <summary>
        /// 当鼠标移动时候,记录坐标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ScreenBody_MouseMove(object sender, MouseEventArgs e)
        {
            if (RectReady == false)
            {
                if (isDowned == true)
                {
                    Image New = DrawScreen((Image)baseImage.Clone(), e.X, e.Y);
                    MainPainter.DrawImage(New, 0, 0);
                    New.Dispose();
                }
            }
            if (RectReady == true)
            {
                if (Rect.Contains(e.X, e.Y))
                {
                    //this.Cursor = Cursors.Hand;
                    if (isDowned == true)
                    {
                        //和上一次的位置比较获取偏移量
                        Rect.X = Rect.X + e.X - tmpx;
                        Rect.Y = Rect.Y + e.Y - tmpy;
                        //记录现在的位置
                        tmpx = e.X;
                        tmpy = e.Y;
                        //2011.12.31 修改  不能移动窗口
                       // MoveRect((Image)baseImage.Clone(), Rect);
                    }
                }
            }
        }

        /// <summary>
        /// 根据坐标来记录截屏
        /// </summary>
        /// <param name="Painter"></param>
        /// <param name="Mouse_x"></param>
        /// <param name="Mouse_y"></param>
        private void DrawRect(Graphics Painter, int Mouse_x, int Mouse_y)
        {
            int width = 0;
            int heigth = 0;
            if (Mouse_y < Rect.Y)
            {
                Rect.Y = Mouse_y;
                heigth = downPoint.Y - Mouse_y;
            }
            else
            {
                heigth = Mouse_y - downPoint.Y;
            }
            if (Mouse_x < Rect.X)
            {
                Rect.X = Mouse_x;
                width = downPoint.X - Mouse_x;
            }
            else
            {
                width = Mouse_x - downPoint.X;
            }
            Rect.Size = new Size(width, heigth);
            Painter.DrawRectangle(pen, Rect);
        }

        private Image DrawScreen(Image back, int Mouse_x, int Mouse_y)
        {
            Graphics Painter = Graphics.FromImage(back);
            DrawRect(Painter, Mouse_x, Mouse_y);
            return back;
        }

        private void MoveRect(Image image, Rectangle Rect)
        {
            Graphics Painter = Graphics.FromImage(image);
            Painter.DrawRectangle(pen, Rect.X, Rect.Y, Rect.Width, Rect.Height);
            MainPainter.DrawImage(image, 0, 0);
            image.Dispose();
        }
    }

3    /// <summary>
      /// 另存为,取消,完成s按钮

      /// </summary>

    public partial class FormSave :Form
    {
        #region 属性
        private ScreenBody _screenBody;
        private Rectangle _rectangle;
        private Image _image;
        private frmQQ _frmQQ;
        public static bool IsDownCutBTN = false;
        #endregion

        public FormSave(ScreenBody screenBody, Point point, frmQQ frmQQ)
        {
            InitializeComponent();
            this._screenBody = screenBody;
            this._frmQQ = frmQQ;
            this.Location = point;
        }

        /// <summary>
        /// 另存为按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BTNSaveAs_Click(object sender, EventArgs e)
        {
            IsDownCutBTN = true;
            _rectangle = ScreenBody.Rect;
            if (_rectangle != null)
            {
                SaveFileDialog savafil = new SaveFileDialog();
                savafil.FileName = DateTime.Now.ToString("yyyyMMddhhmmss");//用时间创建图片名称
                savafil.Filter = "Image Files(*.JPG;*.GIF)|*.JPG;*.GIF|All files(*.*)|*.*";
                if (savafil.ShowDialog() == DialogResult.OK)
                {
                    _image = new Bitmap(_rectangle.Width, _rectangle.Height);
                    Graphics g = Graphics.FromImage(_image);
                    g.CopyFromScreen(_rectangle.X + 1, _rectangle.Y + 1, 0, 0, _rectangle.Size);
                    Clipboard.SetImage(_image);
                    _image.Save(savafil.FileName);
                    this.Close();
                }
            }
            _frmQQ.isDownCutBTN = false;
        }

        /// <summary>
        /// 取消按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BTNCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 完成按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BTNConfirm_Click(object sender, EventArgs e)
        {
            _rectangle = ScreenBody.Rect;
            _image = new Bitmap(_rectangle.Width, _rectangle.Height);
            Graphics g = Graphics.FromImage(_image);
            g.CopyFromScreen(_rectangle.X + 1, _rectangle.Y + 1, 0, 0, _rectangle.Size);
            Clipboard.SetImage(_image);
            Clipboard.SetDataObject(_image, true);
            this._frmQQ.txtInput.Paste();
            this.Close();
        }

        private void FormSave_FormClosed(object sender, FormClosedEventArgs e)
        {
            _screenBody.Close();
        }
    }