您的位置:首页 > 编程语言 > C#

C#模拟PrtScn实现截屏

2015-08-20 15:07 295 查看
有了之前的基础知识了解,现在开始实现PrtScn和Alt+PrtScn。

首先新建一个WPF应用程序,命名为PrintscreenAndAltPrintScreen

导入keybd_event方法

需要为DllImport添加using System.Runtime.InteropServices;

[DllImport("user32.dll")]
static extern void keybd_event
(
byte bVk,// 虚拟键值
byte bScan,// 硬件扫描码
uint dwFlags,// 动作标识
IntPtr dwExtraInfo// 与键盘动作关联的辅加信息
);


编写PrtScn函数:

public void PrintScreen()
{
keybd_event((byte)0x2c, 0, 0x0, IntPtr.Zero);//down
System.Windows.Forms.Application.DoEvents(); //添加引用system.windows.forms
keybd_event((byte)0x2c, 0, 0x2, IntPtr.Zero);//up
System.Windows.Forms.Application.DoEvents();
}


编写Alt+PrtScn函数:

public void AltPrintScreen()
{
keybd_event((byte)Keys.Menu, 0, 0x0, IntPtr.Zero);
keybd_event((byte)0x2c, 0, 0x0, IntPtr.Zero);//down
System.Windows.Forms.Application.DoEvents();
System.Windows.Forms.Application.DoEvents();
keybd_event((byte)0x2c, 0, 0x2, IntPtr.Zero);//up
keybd_event((byte)Keys.Menu, 0, 0x2, IntPtr.Zero);
System.Windows.Forms.Application.DoEvents();
System.Windows.Forms.Application.DoEvents();
}


编写从剪贴板获得图片函数:

需要添加using System.Drawing;添加引用System.Drawing

private Bitmap GetScreenImage()
{
System.Windows.Forms.IDataObject newobject = null;
Bitmap NewBitmap = null;
try
{
System.Windows.Forms.Application.DoEvents();
newobject = System.Windows.Forms.Clipboard.GetDataObject();
if (System.Windows.Forms.Clipboard.ContainsImage())
{
NewBitmap = (Bitmap)(System.Windows.Forms.Clipboard.GetImage().Clone());
}
return NewBitmap;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}


编写xmal程序:

简单添加两个Image控件和两个button按钮

<Window x:Class="PrintscreenAndAltPrintScreen.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="768" Width="1024">
<Grid>
<Image Name="image1" Margin="45,30,562,203">
</Image>
<Button Content="PrtScn" Height="24" HorizontalAlignment="Left" Margin="182,552,0,0" Name="button1" VerticalAlignment="Top" Width="95" Click="button1_Click" />
<Button Content="Alt+PrtScn" Height="24" HorizontalAlignment="Left" Margin="718,552,0,0" Name="button2" VerticalAlignment="Top" Width="95" Click="button2_Click" />
<Image Margin="566,30,41,213" Name="image2" />
</Grid>
</Window>


对两个按钮添加click事件:

这里需要把Bitmap转为BitmapSource,可以参阅博客:WPF(C#)中Bitmap与BitmapImage相互转换

private void button2_Click(object sender, RoutedEventArgs e)
{
image2.Source = null;
AltPrintScreen();
Bitmap bitmap = GetScreenImage();
IntPtr ip = bitmap.GetHbitmap();//从GDI+ Bitmap创建GDI位图对象
//Imaging.CreateBitmapSourceFromHBitmap方法,基于所提供的非托管位图和调色板信息的指针,返回一个托管的BitmapSource
BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
image2.Source = bitmapSource;
}


private void button1_Click(object sender, RoutedEventArgs e)
{
image1.Source = null;
PrintScreen();
Bitmap bitmap = GetScreenImage();
IntPtr ip = bitmap.GetHbitmap();//从GDI+ Bitmap创建GDI位图对象
//Imaging.CreateBitmapSourceFromHBitmap方法//基于所提供的非托管位图和调色板信息的指针,返回一个托管的BitmapSource
BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
image1.Source = bitmapSource;
}


最后对DoEvents 方法的一点解释:

Application.DoEvents 方法

处理当前在消息队列中的所有 Windows 消息。

语法:

public static void DoEvents()


当运行 Windows 窗体时,它将创建新窗体,然后该窗体等待处理事件。该窗体在每次处理事件时,均将处理与该事件关联的所有代码。所有其他事件在队列中等待。当代码处理事件时,应用程序不会响应。例如,如果将甲窗口拖到乙窗口之上,则乙窗口不会重新绘制。

如果在代码中调用 DoEvents,则您的应用程序可以处理其他事件。 例如,如果您有向 ListBox 添加数据的窗体,并将 DoEvents 添加到代码中,那么当将另一窗口拖到您的窗体上时,该窗体将重新绘制。 如果从代码中移除 DoEvents,那么在按钮的单击事件处理程序执行结束以前,您的窗体不会重新绘制。

与 Visual Basic 6.0 不同, DoEvents 方法不调用 Thread.Sleep 方法。

最后发图不发种:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: