| 网站首页 | 资讯 | Hack | 漏洞 | 网管 | 编程 | 培训 | 品黑页 | 软件 | 论坛 | 动画 | 视频 | 经典 | 教学站 | 黑客点睛 | 
服务导航 我要发布 主力频道 空间域名 精华收集 服务器出租 黑客培训 光盘刻录 特色服务 解决方案 我要投诉
您现在的位置: 华夏黑客同盟 >> 编程 >> C语言 >> 正文 用户登录 新用户注册
C#模仿QQ截图功能         ★★★ 【字体:
C#模仿QQ截图功能
作者:不详 文章来源:南域剑盟 点击数: 更新时间:2007-1-7

前阵子改了段C#截图功能的代码,现贴上来希望对大家有用

主文件

CaptureScreenForm.cs

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

namespace CaptureScreen
{
 /// <summary>
 /// CaptureScreenForm 的摘要说明。
 /// </summary>
 public class CaptureScreenForm : System.Windows.Forms.Form
 {
  private System.Windows.Forms.PictureBox picbox;
  private System.Windows.Forms.Button exitBtn;
  private System.Windows.Forms.Button aboutBtn;
  private System.Windows.Forms.Button ClearBtn;
  private System.Windows.Forms.Button saveBtn;
  private System.Windows.Forms.Button fullScreenBtn;
  private System.Windows.Forms.Button mouseCaptureBtn;

  public static Image image = null ;
  private System.Windows.Forms.Panel panel1;

  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public CaptureScreenForm()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

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

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.picbox = new System.Windows.Forms.PictureBox();
   this.fullScreenBtn = new System.Windows.Forms.Button();
   this.mouseCaptureBtn = new System.Windows.Forms.Button();
   this.ClearBtn = new System.Windows.Forms.Button();
   this.saveBtn = new System.Windows.Forms.Button();
   this.aboutBtn = new System.Windows.Forms.Button();
   this.exitBtn = new System.Windows.Forms.Button();
   this.panel1 = new System.Windows.Forms.Panel();
   this.panel1.SuspendLayout();
   this.SuspendLayout();
   //
   // picbox
   //
   this.picbox.Location = new System.Drawing.Point(0, 40);
   this.picbox.Name = "picbox";
   this.picbox.Size = new System.Drawing.Size(680, 312);
   this.picbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
   this.picbox.TabIndex = 0;
   this.picbox.TabStop = false;
   //
   // fullScreenBtn
   //
   this.fullScreenBtn.Location = new System.Drawing.Point(0, 0);
   this.fullScreenBtn.Name = "fullScreenBtn";
   this.fullScreenBtn.Size = new System.Drawing.Size(96, 32);
   this.fullScreenBtn.TabIndex = 1;
   this.fullScreenBtn.Text = "全屏截取";
   this.fullScreenBtn.Click += new System.EventHandler(this.fullScreenBtn_Click);
   //
   // mouseCaptureBtn
   //
   this.mouseCaptureBtn.Location = new System.Drawing.Point(120, 0);
   this.mouseCaptureBtn.Name = "mouseCaptureBtn";
   this.mouseCaptureBtn.Size = new System.Drawing.Size(96, 32);
   this.mouseCaptureBtn.TabIndex = 2;
   this.mouseCaptureBtn.Text = "鼠标截取";
   this.mouseCaptureBtn.Click += new System.EventHandler(this.mouseCaptureBtn_Click);
   //
   // ClearBtn
   //
   this.ClearBtn.Location = new System.Drawing.Point(240, 0);
   this.ClearBtn.Name = "ClearBtn";
   this.ClearBtn.Size = new System.Drawing.Size(88, 32);
   this.ClearBtn.TabIndex = 3;
   this.ClearBtn.Text = "清除截取图片";
   this.ClearBtn.Click += new System.EventHandler(this.ClearBtn_Click);
   //
   // saveBtn
   //
   this.saveBtn.Location = new System.Drawing.Point(352, 0);
   this.saveBtn.Name = "saveBtn";
   this.saveBtn.Size = new System.Drawing.Size(96, 32);
   this.saveBtn.TabIndex = 4;
   this.saveBtn.Text = "保存截取图片";
   this.saveBtn.Click += new System.EventHandler(this.saveBtn_Click);
   //
   // aboutBtn
   //
   this.aboutBtn.Location = new System.Drawing.Point(472, 0);
   this.aboutBtn.Name = "aboutBtn";
   this.aboutBtn.Size = new System.Drawing.Size(96, 32);
   this.aboutBtn.TabIndex = 5;
   this.aboutBtn.Text = "关  于";
   this.aboutBtn.Click += new System.EventHandler(this.aboutBtn_Click);
   //
   // exitBtn
   //
   this.exitBtn.Location = new System.Drawing.Point(592, 0);
   this.exitBtn.Name = "exitBtn";
   this.exitBtn.Size = new System.Drawing.Size(88, 32);
   this.exitBtn.TabIndex = 6;
   this.exitBtn.Text = "退  出";
   this.exitBtn.Click += new System.EventHandler(this.exitBtn_Click);
   //
   // panel1
   //
   this.panel1.Controls.Add(this.fullScreenBtn);
   this.panel1.Controls.Add(this.mouseCaptureBtn);
   this.panel1.Controls.Add(this.ClearBtn);
   this.panel1.Controls.Add(this.saveBtn);
   this.panel1.Controls.Add(this.aboutBtn);
   this.panel1.Controls.Add(this.exitBtn);
   this.panel1.Location = new System.Drawing.Point(0, 0);
   this.panel1.Name = "panel1";
   this.panel1.Size = new System.Drawing.Size(680, 32);
   this.panel1.TabIndex = 7;
   //
   // CaptureScreenForm
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.AutoScroll = true;
   this.ClientSize = new System.Drawing.Size(696, 358);
   this.Controls.Add(this.panel1);
   this.Controls.Add(this.picbox);
   this.Name = "CaptureScreenForm";
   this.Text = "截取屏幕";
   this.panel1.ResumeLayout(false);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new CaptureScreenForm());
  }

  protected override void OnPaint(PaintEventArgs e)
  {
   base.OnPaint (e);
   if(image != null)
   {
    this.picbox.Image = image;
   }
   else
   {
    this.picbox.Image = null;
    this.picbox.Refresh();
   }
  }


  // 引用API

  [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
  public static extern bool BitBlt(
   IntPtr hdcDest, //目标设备的句柄
   int nXDest, // 目标对象的左上角的X坐标
   int nYDest, // 目标对象的左上角的X坐标
   int nWidth, // 目标对象的矩形的宽度
   int nHeight, // 目标对象的矩形的长度
   IntPtr hdcSrc, // 源设备的句柄
   int nXSrc, // 源对象的左上角的X坐标
   int nYSrc, // 源对象的左上角的X坐标
   System.Int32 dwRop // 光栅的操作值
   );


  private void exitBtn_Click(object sender, System.EventArgs e)
  {
   Application.Exit();
  }

  private void aboutBtn_Click(object sender, System.EventArgs e)
  {
   MessageBox.Show("Code by Star in 2006-3-17,E_mail: ygh_01@163.com ,thank you to use this.");
  }

  private void ClearBtn_Click(object sender, System.EventArgs e)
  {
   CaptureScreenForm.image = null;
   this.Invalidate();
  }

  private void saveBtn_Click(object sender, System.EventArgs e)
  {
   bool isSave = true;
   SaveFileDialog saveImageDialog = new SaveFileDialog();
   saveImageDialog.Title = "Capture screen image save dialog";
   saveImageDialog.Filter= @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif";

   if(saveImageDialog.ShowDialog() == DialogResult.OK)
   {
     string fileName = saveImageDialog.FileName.ToString();
    
    if(fileName != "" && fileName != null)
    {
     string fileExtName = fileName.Substring(fileName.LastIndexOf(".")+1).ToString();

     System.Drawing.Imaging.ImageFormat imgformat = null;     
 
     if(fileExtName!="")
     {
      switch(fileExtName)
      {
       case "jpg":
        imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
        break;
       case "bmp":
        imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
        break;
       case "gif":
        imgformat = System.Drawing.Imaging.ImageFormat.Gif;
        break;
       default:
        MessageBox.Show("只能存取为: jpg,bmp,gif 格式");
        isSave = false;
        break;
      }

     }

     //默认保存为JPG格式
     if(imgformat == null)
     {
      imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
     }
                   
     if(isSave)
     {
      try
      {
       CaptureScreenForm.image.Save(fileName,imgformat);
       MessageBox.Show("图片已经成功保存!~~");
      }
      catch
      {
       MessageBox.Show("保存失败,你还没有截取过图片或已经清空图片!");
      }
     }

    }

   }
  }

  private void fullScreenBtn_Click(object sender, System.EventArgs e)
  {
      CaptureScreenForm.image = windowFullScreen();
   picbox.Image = CaptureScreenForm.image;
  }

  public static Bitmap windowFullScreen()
  {
   //建立屏幕Graphics
   Graphics grpScreen = Graphics.FromHwnd(IntPtr.Zero);
   //根据屏幕大小建立位图
   Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,grpScreen);
   //建立位图相关Graphics
   Graphics grpBitmap = Graphics.FromImage(bitmap);
            //建立屏幕上下文
   IntPtr hdcScreen = grpScreen.GetHdc();
   //建立位图上下文
   IntPtr hdcBitmap = grpBitmap.GetHdc();
   //将屏幕捕获保存在图位中
   BitBlt(hdcBitmap,0,0,bitmap.Width,bitmap.Height,hdcScreen,0,0,0x00CC0020);
   //关闭位图句柄
   grpBitmap.ReleaseHdc(hdcBitmap);
   //关闭屏幕句柄
   grpScreen.ReleaseHdc(hdcScreen);
   //释放位图对像
   grpBitmap.Dispose();
   //释放屏幕对像
   grpScreen.Dispose();

   //返回捕获位图
   return bitmap;
  }

  private void mouseCaptureBtn_Click(object sender, System.EventArgs e)
  {
   MouseCaptureForm mouseCapture = new MouseCaptureForm();
   mouseCapture.Show();
  }
 }
}

另一个截屏文件:

MouseCaptureForm.cs

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

namespace CaptureScreen
{
 /// <summary>
 /// MouseCaptureForm 的摘要说明。
 /// </summary>
 public class MouseCaptureForm : System.Windows.Forms.Form
 {
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  //
  //自定义变量
  //
  private Point pot;
  private Rectangle area = Rectangle.Empty;
  private Image img;
  private int index = -1;
  private string noselectString   =  "\n.请按下鼠标左键不放拖动选取截图区域\n\n .单击鼠标右键或按ESC取消截图";
  private string selectingString  =  "\n.松开鼠标左键确定选取范围\n\n.按ESC重新选择";
  private string selectedString   =  "\n.按鼠标左键调整选择范围\n\n .双击鼠标左键保存截图 \n\n.按鼠标右键重新选择 \n\n.ESC键取消截图";
  private System.Windows.Forms.Label tiptext;
       
  //
  //系统生成定义
  //
  private System.ComponentModel.Container components = null;

  public MouseCaptureForm()
  {
   this.Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
   this.BackgroundImage = CaptureScreenForm.windowFullScreen(); 

   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

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

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   this.tiptext = new System.Windows.Forms.Label();
   this.SuspendLayout();
   //

[1] [2] 下一页

责任编辑:郭瑞  联系方式  Email:郭瑞
电话:51228163
  • 上一篇编程:

  • 下一篇编程:
  • (只显示最新5条。评论内容只代表网友观点,与本站立场无关!)
    姓 名:
    * 游客填写  ·注册用户
    主 页:
    评 分:
    1分 2分 3分 4分 5分
    评论内容:
    验证码: *
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。
  • 最新hack更新
    最新推荐资讯
    相关编程
    给初学编程者的忠告
    粗糙的C#版HTTP代理
    C#版QQ机器人组件源码
    给初学编程者的忠告
    给初学编程者的忠告
    C#中英文混合朗读代码
    C#中get和set访问器的解释
    用C#进行Visio二次开发
    C# 中的关键字base、this
     3.0的五项主要改进
    最新会员软件
    最新推荐视频
    最新推荐动画

    Copyright @ 2005 77169.Net Inc. All rights reserved. 华夏黑客同盟 版权所有
    北京市电信通提供网络带宽

    mailto:webmaster@77169.net
    咨询QQ号:836982 / 59280880
    联系站长 QQ38588913
    热线电话: 86-10-67634029/676229433
    京ICP证041431号