RFID.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. namespace RFID
  6. {
  7. [StructLayout(LayoutKind.Sequential)]
  8. public struct RECT
  9. {
  10. int left;
  11. int top;
  12. int right;
  13. int bottom;
  14. }
  15. public class SendMsg
  16. {
  17. [DllImport("user32.dll")]
  18. public static extern IntPtr GetForegroundWindow();
  19. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  20. public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  21. [DllImport("user32.dll")]
  22. static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
  23. [DllImport("user32.dll")]
  24. static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui);
  25. [StructLayout(LayoutKind.Sequential)]
  26. public struct GUITHREADINFO
  27. {
  28. public int cbSize;
  29. public int flags;
  30. public IntPtr hwndActive;
  31. public IntPtr hwndFocus;
  32. public IntPtr hwndCapture;
  33. public IntPtr hwndMenuOwner;
  34. public IntPtr hwndMoveSize;
  35. public IntPtr hwndCaret;
  36. public RECT rectCaret;
  37. }
  38. [StructLayout(LayoutKind.Sequential)]
  39. public struct RECT
  40. {
  41. int left;
  42. int top;
  43. int right;
  44. int bottom;
  45. }
  46. public GUITHREADINFO? GetGuiThreadInfo(IntPtr hwnd)
  47. {
  48. if (hwnd != IntPtr.Zero)
  49. {
  50. uint threadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero);
  51. GUITHREADINFO guiThreadInfo = new GUITHREADINFO();
  52. guiThreadInfo.cbSize = Marshal.SizeOf(guiThreadInfo);
  53. if (GetGUIThreadInfo(threadId, ref guiThreadInfo) == false)
  54. return null;
  55. return guiThreadInfo;
  56. }
  57. return null;
  58. }
  59. public void SendText(string text)
  60. {
  61. IntPtr hwnd = GetForegroundWindow();
  62. if (String.IsNullOrEmpty(text))
  63. return;
  64. GUITHREADINFO? guiInfo = GetGuiThreadInfo(hwnd);
  65. if (guiInfo != null)
  66. {
  67. for (int i = 0; i < text.Length; i++)
  68. {
  69. SendMessage(guiInfo.Value.hwndFocus, 0x0102, (IntPtr)(int)text[i], IntPtr.Zero);
  70. }
  71. }
  72. }
  73. }
  74. }