首页 > QQ技巧 > 正文

自制完全仿QQ截图工具核心源代码

时间:2013-03-31 09:20 作者:QQ地带 我要评论

下面分享下核心代码:

左键按下开始绘图代码
 
 1         /// <summary>
 2         /// 按下左键开始绘制
 3         /// </summary>
 4         private void imageProcessBox1_MouseDown(object sender, MouseEventArgs e) {
 5             if (imageProcessBox1.Cursor != Cursors.SizeAll &&
 6                 imageProcessBox1.Cursor != Cursors.Default)
 7                 panel1.Visible = false;         //表示改变选取大小 隐藏工具条
 8             //若果在选取类点击 并且有选择工具
 9             if (e.Button == MouseButtons.Left && imageProcessBox1.IsDrawed && HaveSelectedToolButton()) {
10                 if (imageProcessBox1.SelectedRectangle.Contains(e.Location)) {
11                     m_ptOriginal = e.Location;   //沈阳骨科 www.wjkfk.com
12                     if (tBtn_Text.IsSelected) {         //如果选择的是绘制文本 弹出文本框
13                         textBox1.Location = e.Location;
14                         textBox1.Visible = true;
15                         textBox1.Focus();
16                         return;
17                     }
18                     m_isStartDraw = true;
19                     Cursor.Clip = imageProcessBox1.SelectedRectangle;
20                 }
21             }
22         }

 

移动鼠标开始截图代码
 
 1         /// <summary>
 2         /// 移动鼠标获取截图区域
 3         /// </summary>
 4         private void imageProcessBox1_MouseMove(object sender, MouseEventArgs e) {
 5             m_ptCurrent = e.Location;
 6             //根据是否选择有工具决定 鼠标指针样式
 7             if (imageProcessBox1.SelectedRectangle.Contains(e.Location) && HaveSelectedToolButton() && imageProcessBox1.IsDrawed)
 8                 this.Cursor = Cursors.Cross;
 9             else if (!imageProcessBox1.SelectedRectangle.Contains(e.Location))
10                 this.Cursor = Cursors.Default;
11
12             if (imageProcessBox1.IsStartDraw && panel1.Visible)   //在重置选取的时候 重置工具条位置(成立于移动选取的时候)
13                 this.SetToolBarLocation();
14
15             if (m_isStartDraw) {        //如果在区域内点下那么绘制相应图形
16                 using (Graphics g = Graphics.FromImage(m_bmpLayerShow)) {
17                     int tempWidth = 1;
18                     if (toolButton2.IsSelected) tempWidth = 3;
19                     if (toolButton3.IsSelected) tempWidth = 5;
20                     Pen p = new Pen(colorBox1.SelectedColor, tempWidth);
21
22                     #region   绘制矩形
23
24                     if (tBtn_Rect.IsSelected) {
25                         int tempX = e.X - m_ptOriginal.X > 0 ? m_ptOriginal.X : e.X;
26                         int tempY = e.Y - m_ptOriginal.Y > 0 ? m_ptOriginal.Y : e.Y;
27                         g.Clear(Color.Transparent);
28                         g.DrawRectangle(p, tempX - imageProcessBox1.SelectedRectangle.Left, tempY - imageProcessBox1.SelectedRectangle.Top, Math.Abs(e.X - m_ptOriginal.X), Math.Abs(e.Y - m_ptOriginal.Y));
29                         imageProcessBox1.Invalidate();
30                     }
31
32                     #endregion
33
34                     #region    绘制圆形
35
36                     if (tBtn_Ellipse.IsSelected) {
37                         g.DrawLine(Pens.Red, 0, 0, 200, 200);
38                         g.Clear(Color.Transparent);
39                         g.DrawEllipse(p, m_ptOriginal.X - imageProcessBox1.SelectedRectangle.Left, m_ptOriginal.Y - imageProcessBox1.SelectedRectangle.Top, e.X - m_ptOriginal.X, e.Y - m_ptOriginal.Y);
40                         imageProcessBox1.Invalidate();
41                     }
42
43                     #endregion
44
45                     #region    绘制箭头
46
47                     if (tBtn_Arrow.IsSelected) {
48                         g.Clear(Color.Transparent); //百分之一www.ksair.com.tw
49                         System.Drawing.Drawing2D.AdjustableArrowCap lineArrow =
50                             new System.Drawing.Drawing2D.AdjustableArrowCap(4, 4, true);
51                         p.CustomEndCap = lineArrow;
52                         g.DrawLine(p, (Point)((Size)m_ptOriginal - (Size)imageProcessBox1.SelectedRectangle.Location), (Point)((Size)m_ptCurrent - (Size)imageProcessBox1.SelectedRectangle.Location));
53                         imageProcessBox1.Invalidate();
54                     }
55
56                     #endregion
57
58                     #region    绘制线条
59
60                     if (tBtn_Brush.IsSelected) {
61                         Point ptTemp = (Point)((Size)m_ptOriginal - (Size)imageProcessBox1.SelectedRectangle.Location);
62                         p.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
63                         g.DrawLine(p, ptTemp, (Point)((Size)e.Location - (Size)imageProcessBox1.SelectedRectangle.Location));
64                         m_ptOriginal = e.Location;
65                         imageProcessBox1.Invalidate();
66                     }
67
68                     #endregion
69
70                     p.Dispose();
71                 }
72             }
73         }

 

保存到粘贴板

1         //将图像保存到剪贴板
2         private void tBtn_Finish_Click(object sender, EventArgs e) {
3             Clipboard.SetImage(m_bmpLayerCurrent);
4             this.Close();
5         }
 

保存图片到硬盘


 1 private void tBtn_Save_Click(object sender, EventArgs e) {
 2             SaveFileDialog saveDlg = new SaveFileDialog();
 3             saveDlg.Filter = "位图(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg";
 4             saveDlg.FilterIndex = 1;
 5             saveDlg.FileName = "CAPTURE_" + GetTimeString();
 6             if (saveDlg.ShowDialog() == DialogResult.OK) {
 7                 switch (saveDlg.FilterIndex) {
 8                     case 1:
 9                         m_bmpLayerCurrent.Clone(new Rectangle(0, 0, m_bmpLayerCurrent.Width, m_bmpLayerCurrent.Height),
10                             System.Drawing.Imaging.PixelFormat.Format24bppRgb).Save(saveDlg.FileName,
11                             System.Drawing.Imaging.ImageFormat.Bmp);
12                         this.Close();
13                         break;
14                     case 2:
15                         m_bmpLayerCurrent.Save(saveDlg.FileName,
16                             System.Drawing.Imaging.ImageFormat.Jpeg);
17                         this.Close();
18                         break;
19                 }
20             }
21         }


标签:QQ截图工具
顶一下
(1)
100%
踩一下
(0)
0%

Google提供的广告