有了之前的基础知识了解,如今開始实现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 方法。

最后发图不发种:

C#模拟PrtScn实现截屏的更多相关文章

  1. iOS中正确的截屏姿势

    昨天写了个用到截屏功能的插件,结果问题不断,今天终于解决好了,把debug过程中所有尝试过的截屏方法都贴出来吧- 第一种 这是iOS 3时代开始就被使用的方法,它被废止于iOS 7.iOS的私有方法, ...

  2. c# wpf 利用截屏键实现截屏功能

    原文:c# wpf 利用截屏键实现截屏功能     最近做一个wpf程序需要截图功能,查找资料费了一些曲折,跟大家分享一下.     先是找到了这样一份代码:     static class Scr ...

  3. Stick hero "攻略", android 代码编写与分析(后台截屏, 后台模拟点击)

    论文写完,感觉头脑好久没被灵感刺激了,前些天室友介绍了个小游戏,我突然来了灵感可以写的简单的android 程序实现自动运行.主要的过会为三步: 1,Android 屏幕的获取.因为安全的原因,过程比 ...

  4. VB用API模拟截屏键PrintScreen

    很多人用 SendKeys "{PRTSC}" 模拟截屏键 PrintScreen 的时候提示<错误:'70' 拒绝的权限>,于是经常遇到人问...干脆写下来 '声明 ...

  5. iOS检测用户截屏并获取所截图片

    iOS检测用户截屏并获取所截图片 微信可以检测到用户截屏行为(Home + Power),并在稍后点击附加功能按钮时询问用户是否要发送刚才截屏的图片,这个用户体验非常好.在iOS7之前, 如果用户截屏 ...

  6. nodejs+phantomjs+七牛 实现截屏操作并上传七牛存储

    近来研究了下phantomjs,只是初涉,还谈不上深入研究,首先介绍下什么是phantomjs. 官网上的介绍是:”PhantomJS is a headless WebKit scriptable ...

  7. iOS截屏并修改截图然后分享的功能实现

    一. 实现的效果类似微博的截图分享 不仅截图分享的时候还进行图片的修改,增加自己的二维码 二.实现方式 苹果在ios7之后提供了一个新的通知类型:UIApplicationUserDidTakeScr ...

  8. Android 长截屏原理

    https://android-notes.github.io/2016/12/03/android%E9%95%BF%E6%88%AA%E5%B1%8F%E5%8E%9F%E7%90%86/   a ...

  9. Android8.1 MTK平台 截屏功能分析

    前言 涉及到的源码有 frameworks\base\services\core\java\com\android\server\policy\PhoneWindowManager.java vend ...

随机推荐

  1. maven编译报错 -source 1.7 中不支持 lambda 表达式

    Maven项目编译失败: [ERROR] COMPILATION ERROR : [INFO] ---------------------------------------------------- ...

  2. DateNavigator

    <Border BorderThickness="1,1,1,1" BorderBrush="Black" Grid.Column="1&quo ...

  3. 九度OJ 1006 ZOJ问题 (这题測试数据有问题)

    题目1006:ZOJ问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:15725 解决:2647 题目描写叙述: 对给定的字符串(仅仅包括'z','o','j'三种字符),推断他能否AC ...

  4. Tomcat日志格式自定义

    设置日志显示信息格式,默认情况下,Tomcat的访问日志是不记录的.需要在serve.xml中修改配置,去掉注释.     <!--         <Valve className=&q ...

  5. 除掉inline-block 间距

    1.现象: <!doctype html> <html lang="en"> <head> <meta charset="UTF ...

  6. 如何固定OpenERP顶的主菜单,方便滚动至第二屏以及多屏时,快速切换主菜单

    如何固定OpenERP顶的主菜单,方便滚动至第二屏以及多屏时,快速切换主菜单 作者:广州-步科,来自OpenERP应用群() 将“addons\web\static\src\css”目录下的“base ...

  7. 【面试笔试】Java常见面试笔试总结

    Java 基础 1.有哪些数据类型 Java定义了8种简单类型:byte.short.int.long.char.float.double和boolean. 2.面向对象的语言特征 封装.继承.多态 ...

  8. JSTL fmt:formatNumber 数字、货币格式化

    <fmt:formatNumber value="12.34" pattern="#0.00" /> 12.34 保留小数点后两位数 <fmt ...

  9. DX:神奇的LayoutControl.BestFit()

    自动生成LayoutControl后,界面总是不对:在LayoutControl中找到一个BestFit()方法,调用后神奇的结果出现了,这不正是我想要的吗?测试代码: public partial ...

  10. SSO之CAS + LDAP

    本来主要详细是介绍CAS和LDAP整合实现单点登录的步骤. 1. 依<SSO之安装CAS Server>所述安装好CAS Server.2. 安装ApacheDS.安装好ApacheDS后 ...