首頁  >  文章  >  後端開發  >  C#模擬PrintScreen和Alt+PrintScreen截取螢幕圖片的範例程式碼分享(圖)

C#模擬PrintScreen和Alt+PrintScreen截取螢幕圖片的範例程式碼分享(圖)

黄舟
黄舟原創
2017-03-13 17:49:052416瀏覽

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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn