C#模拟PrintScreen和Alt+PrintScreen截取屏幕图片的示例代码分享(图)
C# 模拟PrintScreen 和 Alt+PrintScreen截取屏幕图片
keybd_event API
函数功能:该函数合成一次击键事件。系统可使用这种合成的击键事件来产生WM_KEYUP或WM_KEYDOWN消息,键盘驱动程序的中断处理程序调用keybd_event函数。在Windows NT中该函数己被使用SendInput来替代它。
函数原型;VOID keybd_event(BYTE bVk,BYTE bScan,DWORD dwFlags,DWORD dwExtralnfo);
参数:
bVk:定义一个虚拟键码。键码值必须在1~254之间。
bScan:定义该键的硬件扫描码。
dwFlags:定义函数操作的各个方面的一个标志位集。应用程序可使用如下一些预定义常数的组合设置标志位。
KEYEVENTF_EXTENDEDKEY:若指定该值,则扫描码前一个值为OXEO(224)的前缀字节。
KEYEVENTF_KEYUP:若指定该值,该键将被释放;若未指定该值,该键将被按下。
dwExtralnfo:定义与击键相关的附加的32位值。
返回值:该函数无返回值。
完整代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace PrintScreen { public partial class Form1 : Form { [DllImport("user32.dll")] static extern void keybd_event ( byte bVk,// 虚拟键值 byte bScan,// 硬件扫描码 uint dwFlags,// 动作标识 IntPtr dwExtraInfo// 与键盘动作关联的辅加信息 ); /// <summary> /// 模拟Print Screen键盘消息,截取全屏图片。 /// </summary> public void PrintScreen() { keybd_event((byte)0x2c, 0, 0x0, IntPtr.Zero);//down Application.DoEvents(); keybd_event((byte)0x2c, 0, 0x2, IntPtr.Zero);//up Application.DoEvents(); } /// <summary> /// 模拟Alt Print Screen键盘消息,截取当前窗口图片。 /// </summary> public void AltPrintScreen() { keybd_event((byte)Keys.Menu, 0, 0x0, IntPtr.Zero); keybd_event((byte)0x2c, 0, 0x0, IntPtr.Zero);//down Application.DoEvents(); Application.DoEvents(); keybd_event((byte)0x2c, 0, 0x2, IntPtr.Zero);//up keybd_event((byte)Keys.Menu, 0, 0x2, IntPtr.Zero); Application.DoEvents(); Application.DoEvents(); } public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } /// <summary> /// 从剪贴板获取图片 /// </summary> /// <returns></returns> private Bitmap GetScreenImage() { IDataObject newobject = null; Bitmap NewBitmap = null; try { Application.DoEvents(); newobject = Clipboard.GetDataObject(); if (Clipboard.ContainsImage()) { NewBitmap = (Bitmap)(Clipboard.GetImage().Clone()); } return NewBitmap; } catch(Exception ex) { Console.WriteLine(ex.Message); return null; } } private void button1_Click(object sender, EventArgs e) { button1.Enabled = false; pictureBox1.Image = null; PrintScreen(); pictureBox1.Image = GetScreenImage(); button1.Enabled = true; Application.DoEvents(); } private void button2_Click(object sender, EventArgs e) { button2.Enabled = false; pictureBox1.Image = null; AltPrintScreen(); pictureBox1.Image = GetScreenImage(); button2.Enabled = true; Application.DoEvents(); } } }
运行效果:
遗留问题:
PrintScreen没有任务问题,但使用AltPrintScreen时第一次总是不能得到正确的图片,不知道是为什么!希望高手路过指点一二,小弟不胜感激!
以上是C#模拟PrintScreen和Alt+PrintScreen截取屏幕图片的示例代码分享(图)的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题











使用 C# 的 Active Directory 指南。在这里,我们讨论 Active Directory 在 C# 中的介绍和工作原理以及语法和示例。

多线程和异步的区别在于,多线程同时执行多个线程,而异步在不阻塞当前线程的情况下执行操作。多线程用于计算密集型任务,而异步用于用户交互操作。多线程的优势是提高计算性能,异步的优势是不阻塞 UI 线程。选择多线程还是异步取决于任务性质:计算密集型任务使用多线程,与外部资源交互且需要保持 UI 响应的任务使用异步。
