/*
这是升级版的,花纯春截屏器了哈,比昨天哪个多了,在系统托盘图标上的右键弹出菜单操作,就不会将程序本身给显示在截出来的图片上了。而且修正了昨天那个显示图片不全毛病。
可以从这儿下载http://metababy.chinay.com/sp/metababy/08503052753.rar
后几天,逐步完善其他功能。
*/
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace 截屏
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.LinkLabel linkLabel1;
private System.Windows.Forms.LinkLabel linkLabel2;
private System.Windows.Forms.LinkLabel linkLabel3;
private System.Windows.Forms.LinkLabel linkLabel4;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.ComponentModel.IContainer components;
private ContextMenu notifyiconMnu;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
private static extern bool BitBlt (
IntPtr hdcDest , // 目标 DC的句柄
int nXDest ,
int nYDest ,
int nWidth ,
int nHeight ,
IntPtr hdcSrc , // 源DC的句柄
int nXSrc ,
int nYSrc ,
System.Int32 dwRop // 光栅的处理数值
) ;//这儿,我也没搞清楚,知其然不知所以然,这些参数的具体意义,我还得找找资料,有朋友知道的,请回复在评论里,谢谢
//今天,我也在网上找了,没有找到解释。只有死记吧,搜出来结果,都是只是在用,代码也是一样的,没有解释原因的,算了,读书要“不求甚解”嘛,不然,就学不起走了,今后,时机到了,总会在“不经意间”知道为什么要这样用的。
[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]//这是原来没有的一段,就是这个造成了截图不全
private static extern IntPtr CreateDC (
string lpszDriver , // 驱动名称
string lpszDevice , // 设备名称
string lpszOutput , // 无用,可以设定位"NULL"
IntPtr lpInitData // 任意的打印机数据
) ;
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.linkLabel2 = new System.Windows.Forms.LinkLabel();
this.linkLabel3 = new System.Windows.Forms.LinkLabel();
this.linkLabel4 = new System.Windows.Forms.LinkLabel();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 120);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "截屏";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 64);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(184, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "c://花纯春.jpg";
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(168, 23);
this.label1.TabIndex = 2;
this.label1.Text = "请输入图片存放位置及名称";
//
// linkLabel1
//
this.linkLabel1.Location = new System.Drawing.Point(24, 176);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.TabIndex = 3;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "我的小店";
this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
//
// linkLabel2
//
this.linkLabel2.Location = new System.Drawing.Point(136, 176);
this.linkLabel2.Name = "linkLabel2";
this.linkLabel2.TabIndex = 4;
this.linkLabel2.TabStop = true;
this.linkLabel2.Text = "我的blog主力站";
this.linkLabel2.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel2_LinkClicked);
//
// linkLabel3
//
this.linkLabel3.Location = new System.Drawing.Point(24, 224);
this.linkLabel3.Name = "linkLabel3";
this.linkLabel3.TabIndex = 5;
this.linkLabel3.TabStop = true;
this.linkLabel3.Text = "我的blog备用站";
this.linkLabel3.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel3_LinkClicked);
//
// linkLabel4
//
this.linkLabel4.Location = new System.Drawing.Point(136, 224);
this.linkLabel4.Name = "linkLabel4";
this.linkLabel4.TabIndex = 6;
this.linkLabel4.TabStop = true;
this.linkLabel4.Text = "我的MSN空间";
this.linkLabel4.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel4_LinkClicked);
//
// notifyIcon1,系统托盘图标,新加的
//
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "花纯春截屏器";
this.notifyIcon1.Visible = true;
//定义一个MenuItem数组,并把此数组同时赋值给ContextMenu对象
MenuItem[] mnuItms = new MenuItem [3];
mnuItms [ 0 ] = new MenuItem ( ) ;
mnuItms [ 0 ] .Text = "捕获当前屏幕!" ;
mnuItms [ 0 ] .Click += new System.EventHandler ( this.button1_Click ) ;
mnuItms [ 1 ] = new MenuItem ( "-" ) ;
mnuItms [ 2 ] = new MenuItem ( ) ;
mnuItms [ 2 ] .Text = "退出系统" ;
mnuItms [ 2 ] .Click += new System.EventHandler ( this.ExitSelect ) ;
mnuItms [ 2 ] .DefaultItem = true ;
notifyiconMnu = new ContextMenu ( mnuItms ) ;
notifyIcon1.ContextMenu = notifyiconMnu ;//新加的右键菜单
//为托盘程序加入设定好的ContextMenu对象
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.linkLabel4);
this.Controls.Add(this.linkLabel3);
this.Controls.Add(this.linkLabel2);
this.Controls.Add(this.linkLabel1);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "Form1";
this.Text = "花纯春写的截屏练习程序";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
this.Visible = false ;//这是新加的
try
{
IntPtr dc1 = CreateDC ( "DISPLAY" , null , null , ( IntPtr ) null ) ;
//创建显示器的DC,这也是后来才加的,原来没有此句
//获得当前屏幕的大小
Rectangle rect = new Rectangle ( ) ;
rect = Screen.GetWorkingArea ( this ) ;
//创建一个以当前屏幕为模板的图象
Graphics g1 = this.CreateGraphics ( ) ;
//创建以屏幕大小为标准的位图
Image MyImage = new Bitmap ( rect.Width , rect.Height , g1 ) ;
Graphics g2 = Graphics.FromImage ( MyImage ) ;
//得到屏幕的DC
IntPtr dc3 = g1.GetHdc ( ) ;
//得到Bitmap的DC
IntPtr dc2 = g2.GetHdc ( ) ;
//调用此API函数,实现屏幕捕获
BitBlt ( dc2 , 0 , 0 , rect.Width , rect.Height , dc1 , 0 , 0 , 13369376 ) ;
//释放掉屏幕的DC
g1.ReleaseHdc ( dc3 ) ;//这是后来才改的,以来的为g1.ReleaseHdc(dc1);
//释放掉Bitmap的DC
g2.ReleaseHdc ( dc2 ) ;
//以JPG文件格式来保存
MyImage.Save ( textBox1.Text ,ImageFormat.Jpeg);
string a="当前屏幕已经保存到"+textBox1.Text;
MessageBox.Show ( a ) ;
this.Visible = true ;//这是新加的
}
catch{MessageBox.Show("出错!");}
}
public void ExitSelect ( object sender , System.EventArgs e )
{
//隐藏托盘程序中的图标
notifyIcon1.Visible = false ;
//关闭系统
this.Close ( ) ;
}
private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://shop1471977.taobao.com");
}
private void linkLabel2_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://riji.163.com/weblog/page/metababy");
}
private void linkLabel3_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://blog.youkuaiyun.com/metababy");
}
private void linkLabel4_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://spaces.msn.com/members/metababy");
}
}
}