只有Form2和Form3,1被删掉了...
Form2的属性设置:
FormBorderStyle为None;ShowInTaskbar为False;WindowState为Maximized;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
namespace PrintScreen
{
public partial class Form2 : Form //截图什么的很粗糙,很多人性化的待改进。
{
private Bitmap MyImage = null;
//private Bitmap lastMyImage = null;
// private Bitmap newMyImage = null;
private int SRCCOPY = 0xcc0020;
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private 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 // 光栅的操作值
);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr CreateDC(
string lpszDriver, // 驱动名称
string lpszDevice, // 设备名称
string lpszOutput, // 无用,可以设定位"NULL"
IntPtr lpInitData // 任意的打印机数据
);
[System.Runtime.InteropServices.DllImportAttribute("msimg32.dll")]
private static extern Int32 AlphaBlend(
IntPtr hdcDest, //目标设备的句柄
int nXOriginDest, // 目标对象的左上角的X坐标
int nYOriginDest, // 目标对象的左上角的X坐标
int nWidthDest, // 目标对象的矩形的宽度
int nHeightDest, // 目标对象的矩形的长度
IntPtr hdcSrc, // 源设备的句柄
int nXOriginSrc, // 源对象的左上角的X坐标
int nYOriginSrc, // 源对象的左上角的X坐标
int nWidthSrc,
int nHeightSrc,
BLENDFUNCTION blend
);
private struct BLENDFUNCTION
{
public byte BlendOp;//源混合操作
public byte BlendFlags;//必须是0
public byte SourceConstantAlpha;//0到255间的值,这里0表示完全透明,255表示完全不透明。
public byte AlphaFormat;//有两个值 0表示常量alpha值,AC_SRC_ALPHA表示每个像素有各自的alpha通道
}
private enum BlendOperation : byte
{
AC_SRC_OVER = 0x00//表示根据源alpha值把源图像叠加到目标图像上
}
private enum BlendFlags : byte
{
Zero = 0x00
}
private enum SourceConstantAlpha : byte
{
Transparent = 0x00,
Opaque = 0xFF
}
private enum AlphaFormat : byte
{
AC_SRC_ALPHA = 0x01
}
private int bimageX=0;//begin
private int bimageY=0;
private int fimageX = 0;//final
private int fimageY = 0;
private int nimageX = 0;//new
private int nimageY = 0;
private int width = 0;
private int height = 0;
private Point doublep;
bool begin = false;
private Graphics g=null;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
this.UpdateStyles();//据说防抖动
g = this.CreateGraphics();
}
private void Form2_MouseDown(object sender, MouseEventArgs e)
{
if (!begin)
{
begin = true;
bimageX = e.X;
bimageY = e.Y;
}
if (e.Button == MouseButtons.Right)
{
this.Close();
Application.Exit();
}
}
private void Form2_MouseUp(object sender, MouseEventArgs e)
{
if (begin)
{
begin = false;
}
}
private void Form2_MouseMove(object sender, MouseEventArgs e)
{
if (begin)
{
this.Refresh();
fimageX = e.X;
fimageY = e.Y;
width = Math.Abs(fimageX - bimageX);
height = Math.Abs(fimageY - bimageY);
if (fimageX < bimageX)
{
nimageX = fimageX;
}
else
{
nimageX = bimageX;
}
if (fimageY < bimageY)
{
nimageY = fimageY;
}
else
{
nimageY = bimageY;
}
g.DrawRectangle(new Pen(Color.Red), new Rectangle(nimageX, nimageY, width, height));
}
}
private void Form2_MouseDoubleClick(object sender, MouseEventArgs e)
{
/* BLENDFUNCTION blend;
blend.BlendOp = (byte)BlendOperation.AC_SRC_OVER;
blend.BlendFlags = (byte)BlendFlags.Zero;
blend.SourceConstantAlpha = 120;
blend.AlphaFormat = (byte)0;*/
Point lastp = new Point(nimageX, nimageY);
doublep = new Point(e.X, e.Y);
Rectangle CatchRect = new Rectangle(lastp, new Size(width, height));
if (CatchRect.Contains(doublep))
{
if ((width <= 1) | (height <= 1))
{
this.Close();
}
else
{
ppp.lastMyImage = new Bitmap(width - 1, height - 1, g);
//根据屏幕大小创建一个与之相同大小的Bitmap对象
Graphics g2 = Graphics.FromImage(ppp.lastMyImage);
//获得屏幕的句柄
IntPtr dc3 = g.GetHdc();
//获得位图的句柄
IntPtr dc2 = g2.GetHdc();
//把当前屏幕捕获到位图对象中
BitBlt(dc2, 0, 0, width - 1, height - 1, dc3, nimageX + 1, nimageY + 1, SRCCOPY);
// AlphaBlend(dc2, 0, 0, width - 1, height - 1, dc3, nimageX + 1, nimageY + 1, width - 1, height - 1, blend);
//把当前屏幕拷贝到位图中
g.ReleaseHdc(dc3);
//释放屏幕句柄
g2.ReleaseHdc(dc2);
//释放位图句柄
this.Hide();
Form3 f3 = new Form3();
f3.ShowDialog();
f3.Dispose();
this.Close();
}
}
}
public override void Refresh()
{
g.DrawImage(this.BackgroundImage, 0, 0);
}
private void Form2_Activated(object sender, EventArgs e)
{
BLENDFUNCTION blend;
blend.BlendOp = (byte)BlendOperation.AC_SRC_OVER;
blend.BlendFlags = (byte)BlendFlags.Zero;
blend.SourceConstantAlpha = 220;
blend.AlphaFormat = (byte)0;
IntPtr dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);
//创建显示器的DC
Graphics g1 = Graphics.FromHdc(dc1);
//由一个指定设备的句柄创建一个新的Graphics对象
MyImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g1);
//根据屏幕大小创建一个与之相同大小的Bitmap对象
Graphics g2 = Graphics.FromImage(MyImage);
//获得屏幕的句柄
IntPtr dc3 = g1.GetHdc();
//获得位图的句柄
IntPtr dc2 = g2.GetHdc();
//把当前屏幕捕获到位图对象中
// BitBlt(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc3, 0, 0, SRCCOPY);
//把当前屏幕拷贝到位图中
AlphaBlend(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc3, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, blend);
g1.ReleaseHdc(dc3);
//释放屏幕句柄
g2.ReleaseHdc(dc2);
// AlphaBlend(dc2,0,0,Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,dc3,0,0,Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height,blend);
this.BackgroundImage = MyImage;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
namespace PrintScreen
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Clipboard.SetImage(ppp.lastMyImage);
if (MessageBox.Show("已经复制导剪切板") == DialogResult.OK)
{
Application.Exit();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "BMP图形文件 (*.bmp)|*.bmp";
saveFileDialog.FilterIndex = 2;
saveFileDialog.RestoreDirectory = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string localFilePath = saveFileDialog.FileName.ToString();
ppp.lastMyImage.Save(localFilePath, ImageFormat.Bmp);
//MessageBox.Show("保存完毕");
}
this.Close();
Application.Exit();
}
private void button3_Click(object sender, EventArgs e)
{
//远程发送还没做。
}
private void button4_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}