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截取屏幕图片的示例代码分享(图)的细致内容,更多请关注ki4网别的相干文章!