对于模拟键盘,除了利用keybd_event,更简单的是使用sendkeys,而且keybd_event已经被sendinput取代。

对于模拟键盘,除了利用keybd_event,更简单的是使用sendkeys,而且keybd_event已经被sendinput取代。         具体代码参考:     请问,用C#如何实现模拟键盘输入     http://expert.csdn.net/Expert/topic/1055/1055110.xml?temp=.1404993         对于模拟鼠标,只好用SendInput,     http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/KeyboardInput/KeyboardInputReference/KeyboardInputFunctions/SendInput.asp         具体代码参考:     http://groups.google.com/groups?hl=zh-CN&lr=&ie=UTF-8&oe=UTF-8&threadm=665201c200e8%24e3a1f550%2435ef2ecf%40TKMSFTNGXA11&rnum=3&prev=/groups%3Fq%3Dsendinput%2Bmouse%2Bc%2523%26hl%3Dzh-CN%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D665201c200e8%2524e3a1f550%252435ef2ecf%2540TKMSFTNGXA11%26rnum%3D3

 

 

Screen Print Capture in C# using SendInput()

 

Recently I needed a way to have an app take a screen shot of itself and save it to a directory. I only wanted the currently focused screen to be captured. I looked around on the web, and found lots of crazy interop scripts to handle doing a screen shots.

But it just didn't seem right. *I* can screen capture by just hitting Alt->Print Screen, why can't my app? I knew there would be some interop involved, but certainly it doesn't have to be that hard, does it?

Turns out, it really doesn't. By sending some Keyboard events using the Win32 SendInput() via User32.dll, and working with the clipboard data in the normal fashion, it's fairly easy to get it all working. First, you need the capture and save code:

private void CaptureAndSave()
{
 uint intReturn = 0;
 NativeWIN32.INPUT structInput;
 structInput = new NativeWIN32.INPUT();
 structInput.type = (uint)1;
 structInput.ki.wScan = 0;
 structInput.ki.time = 0;
 structInput.ki.dwFlags = 0;
 structInput.ki.dwExtraInfo = 0;

 //Press Alt Key
 structInput.ki.wVk = (ushort)NativeWIN32.VK.MENU;
 intReturn = NativeWIN32.SendInput((uint)1, ref structInput, Marshal.SizeOf(structInput));

 // Key down the actual key-code
 structInput.ki.wVk = (ushort)NativeWIN32.VK.SNAPSHOT;//vk;
 intReturn = NativeWIN32.SendInput((uint)1, ref structInput, Marshal.SizeOf(structInput));

 // Key up the actual key-code
 structInput.ki.dwFlags = NativeWIN32.KEYEVENTF_KEYUP;
 structInput.ki.wVk = (ushort)NativeWIN32.VK.SNAPSHOT;//vk;
 intReturn = NativeWIN32.SendInput((uint)1, ref structInput, Marshal.SizeOf(structInput));

 //Keyup Alt
 structInput.ki.dwFlags = NativeWIN32.KEYEVENTF_KEYUP;
 structInput.ki.wVk = (ushort)NativeWIN32.VK.MENU;
 intReturn = NativeWIN32.SendInput((uint)1, ref structInput, Marshal.SizeOf(structInput));

 IDataObject data = Clipboard.GetDataObject();

 if (data.GetDataPresent(DataFormats.Bitmap))
 {
  Image image = (Image)data.GetData(DataFormats.Bitmap,true);

  image.Save("image" +  ".bmp",System.Drawing.Imaging.ImageFormat.Bmp);
  image.Save("image" +  ".jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
  image.Save("image" +  ".gif",System.Drawing.Imaging.ImageFormat.Gif);
 }
 else
 {
  Console.WriteLine("The Data In Clipboard is not in an image format");
 }
}

As you can see, all the heavy lifting appears to be in a class called NativeWIN32. This class is nothing more than an internal wrapper class around the User32.dll functions. I've included the full suite of VK enums since I already had it in my code:

public class NativeWIN32
{
 public const ushort KEYEVENTF_KEYUP = 0x0002;

 public enum VK : ushort
 {
  SHIFT                = 0x10,
  CONTROL              = 0x11,
  MENU                 = 0x12,
  ESCAPE               = 0x1B,
  BACK                 = 0x08,
  TAB                  = 0x09,
  RETURN               = 0x0D,
  PRIOR                = 0x21,
  NEXT                 = 0x22,
  END                  = 0x23,
  HOME                 = 0x24,
  LEFT                 = 0x25,
  UP                   = 0x26,
  RIGHT                = 0x27,
  DOWN                 = 0x28,
  SELECT               = 0x29,
  PRINT                = 0x2A,
  EXECUTE              = 0x2B,
  SNAPSHOT             = 0x2C,
  INSERT               = 0x2D,
  DELETE               = 0x2E,
  HELP                 = 0x2F,
  NUMPAD0              = 0x60,
  NUMPAD1              = 0x61,
  NUMPAD2              = 0x62,
  NUMPAD3              = 0x63,
  NUMPAD4              = 0x64,
  NUMPAD5              = 0x65,
  NUMPAD6              = 0x66,
  NUMPAD7              = 0x67,
  NUMPAD8              = 0x68,
  NUMPAD9              = 0x69,
  MULTIPLY             = 0x6A,
  ADD                  = 0x6B,
  SEPARATOR            = 0x6C,
  SUBTRACT             = 0x6D,
  DECIMAL              = 0x6E,
  DIVIDE               = 0x6F,
  F1                   = 0x70,
  F2                   = 0x71,
  F3                   = 0x72,
  F4                   = 0x73,
  F5                   = 0x74,
  F6                   = 0x75,
  F7                   = 0x76,
  F8                   = 0x77,
  F9                   = 0x78,
  F10                  = 0x79,
  F11                  = 0x7A,
  F12                  = 0x7B,
  OEM_1                = 0xBA,   // ',:' for US
  OEM_PLUS             = 0xBB,   // '+' any country
  OEM_COMMA            = 0xBC,   // ',' any country
  OEM_MINUS            = 0xBD,   // '-' any country
  OEM_PERIOD           = 0xBE,   // '.' any country
  OEM_2                = 0xBF,   // '/?' for US
  OEM_3                = 0xC0,   // '`~' for US
  MEDIA_NEXT_TRACK         = 0xB0,
  MEDIA_PREV_TRACK         = 0xB1,
  MEDIA_STOP               = 0xB2,
  MEDIA_PLAY_PAUSE         = 0xB3,
  LWIN           =0x5B,
  RWIN           =0x5C
 }


 public struct KEYBDINPUT
 {
  public ushort wVk;
  public ushort wScan;
  public uint dwFlags;
  public long time;
  public uint dwExtraInfo;
 };

 [StructLayout(LayoutKind.Explicit,Size=28)]
  public struct INPUT
 {
  [FieldOffset(0)] public uint type;
  [FieldOffset(4)] public KEYBDINPUT ki;
 };


 [DllImport("user32.dll")]
 public static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);

}

##########################

 private void button5_Click(object sender, EventArgs e)

        {

            INPUT[] input = new INPUT[2];

            input[0].type = INPUT_MOUSE;

            input[0].mi.dx = 0;

            input[0].mi.dy = 0;

            input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;



            input[1].type = INPUT_MOUSE;

            input[1].mi.dx = 0;

            input[1].mi.dy = 0;

            input[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;



            SetCursorPos(500, 120);

            SendInput(1, ref input[0], System.Runtime.InteropServices.Marshal.SizeOf(input[0].GetType()));

            //rightClick(); 

            //this.Text= SendInput(2,ref input[0], System.Runtime.InteropServices.Marshal.SizeOf(input[0])).ToString();

            //this.Text = SendInput(2, ref input[1], System.Runtime.InteropServices.Marshal.SizeOf(input[1])).ToString(); 

        }

        const int MOUSEEVENTF_MOVE = 0x1;//mouse   move  

        const int MOUSEEVENTF_LEFTDOWN = 0x2;//left   button   down  

        const int MOUSEEVENTF_LEFTUP = 0x4;//left   button   up  

        const int MOUSEEVENTF_RIGHTDOWN = 0x8;//right   button   down  

        const int MOUSEEVENTF_RIGHTUP = 0x10;//right   button   up  

        const int MOUSEEVENTF_MIDDLEDOWN = 0x20;//middle   button   down  

        const int MOUSEEVENTF_MIDDLEUP = 0x40;//middle   button   up  

        const int MOUSEEVENTF_ABSOLUTE = 0x8000;//absolute   move  

        const int INPUT_MOUSE = 0;

        const int INPUT_KEYBOARD = 1;

        const int INPUT_HARDWARE = 2;



        public struct MOUSEINPUT

        {

            public int dx;

            public int dy;

            public long mouseData;

            public long dwFlags;

            public long time;

            public long dwExtraInfo;

        }



        private static void rightClick()

        {

            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);

            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

            //SendInput(2, ref input[0], System.Runtime.InteropServices.Marshal.SizeOf(input[0]));

            //SendInput(2, ref input[1], System.Runtime.InteropServices.Marshal.SizeOf(input[1]))

        }



        public struct INPUT

        {

            public long type;

            public MOUSEINPUT mi;

        }

        [DllImport("user32.dll")]

        private static extern int SendInput(int a,ref INPUT s,int lParam);



        [DllImport("user32.dll")]

        private static extern int SetCursorPos(int a, int y);



        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "mouse_event")]



        public static extern void mouse_event(

                  int dwFlags,

                  int dx,

                  int dy,

                  int cButtons,

                  int dwExtraInfo

                  );   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值