程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 模擬PrintScreen 和 Alt+PrintScreen截取屏幕圖片

C# 模擬PrintScreen 和 Alt+PrintScreen截取屏幕圖片

編輯:C#入門知識

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// 與鍵盤動作關聯的輔加信息  
        );

        /// 
        /// 模擬Print Screen鍵盤消息,截取全屏圖片。
        /// 
        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(); 
        }

        /// 
        /// 模擬Alt Print Screen鍵盤消息,截取當前窗口圖片。
        /// 
        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)
        {

        }

        /// 
        /// 從剪貼板獲取圖片
        /// 
        /// 
        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時第一次總是不能得到正確的圖片,不知道是為什麼!希望高手路過指點一二,小弟不勝感激!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved