Guest User

Untitled

a guest
Oct 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. require "dl/import"
  4. require "dxruby"
  5.  
  6. module WinAPI
  7. module User32
  8. SWP_NOSIZE = 1
  9. SM_CXFULLSCREEN = 0
  10. SM_CYFULLSCREEN = 1
  11. PM_NOREMOVE = 0
  12. PM_REMOVE = 1
  13.  
  14. # BOOL PeekMessage(
  15. # LPMSG lpMsg, // メッセージ情報
  16. # HWND hWnd, // ウィンドウのハンドル
  17. # UINT wMsgFilterMin, // 最初のメッセージ
  18. # UINT wMsgFilterMax, // 最後のメッセージ
  19. # UINT wRemoveMsg // 削除オプション
  20. # );
  21.  
  22. # typedef struct tagMSG { // msg
  23. # HWND hwnd;
  24. # UINT message;
  25. # WPARAM wParam;
  26. # LPARAM lParam;
  27. # DWORD time;
  28. # POINT pt;
  29. # } MSG;
  30.  
  31. # HHOOK SetWindowsHookEx(
  32. # int idHook, // フックタイプ
  33. # HOOKPROC lpfn, // フックプロシージャ
  34. # HINSTANCE hMod, // アプリケーションインスタンスのハンドル
  35. # DWORD dwThreadId // スレッドの識別子
  36. # );
  37.  
  38. # DWORD GetWindowThreadProcessId(
  39. # HWND hWnd, // ウィンドウのハンドル
  40. # LPDWORD lpdwProcessId // プロセス ID
  41. # );
  42.  
  43. # LRESULT CallNextHookEx(
  44. # HHOOK hhk, // 現在のフックのハンドル
  45. # int nCode, // フックプロシージャに渡すフックコード
  46. # WPARAM wParam, // フックプロシージャに渡す値
  47. # LPARAM lParam // フックプロシージャに渡す値
  48. # );
  49.  
  50. extend DL::Importer
  51. dlload "user32"
  52.  
  53. extern "int GetSystemMetrics(int)"
  54. extern "int PeekMessage(void*, unsigned int, unsigned int, unsigned int, unsigned int)"
  55. extern "int GetMessage(void*, unsigned int, unsigned int, unsigned int)"
  56. extern "int SetWindowsHookEx(int, void*, int, unsigned int)"
  57. extern "unsigned int GetWindowThreadProcessId(unsigned int, void*)"
  58. extern "long CallNextHookEx(unsigned int, int, unsigned int, unsigned long)"
  59.  
  60. # LRESULT CALLBACK CKeyboardHook::KeyboardProc(int p_nCode, WPARAM p_wParam, LPARAM p_lParam)
  61. MessageCallback = bind("long message_callback(int, unsigned int, long)") { |n_code, wparam, lparam|
  62. if n_code < 0 || n_code == 3 # HC_NOREMOVE = 3
  63. return WinAPI::User32.CallNextHookEx(get_hhook_id, n_code, wparam, lparam)
  64. end
  65. puts "%d %d %d" % [n_code, wparam, lparam]
  66. WinAPI::User32.CallNextHookEx(get_hhook_id, n_code, wparam, lparam)
  67. return 1
  68. }
  69.  
  70. POINT = struct(["long x", "long y"])
  71. MSG = struct(["unsigned long hwnd", "unsigned int message", "unsigned int wParam", "unsigned long lParam",
  72. "unsigned int time", "void* pt"])
  73. end
  74. end
  75.  
  76. def window_align_center
  77. hwnd = Window.hWnd
  78. desktop_width = WinAPI::User32.GetSystemMetrics(WinAPI::User32::SM_CXFULLSCREEN)
  79. desktop_height = WinAPI::User32.GetSystemMetrics(WinAPI::User32::SM_CYFULLSCREEN)
  80. Window.x = (desktop_width - Window.width) / 2
  81. Window.y = (desktop_height - Window.height) / 2
  82. end
  83.  
  84. window_align_center
  85.  
  86. msg = WinAPI::User32::MSG.malloc
  87. hwnd = Window.hWnd
  88.  
  89. #Window.fps = 10
  90.  
  91. WH_GETMESSAGE = 3
  92.  
  93. thread_id = WinAPI::User32.GetWindowThreadProcessId(hwnd, 0)
  94. HHOOK_ID = WinAPI::User32.SetWindowsHookEx(2, WinAPI::User32::MessageCallback, 0, thread_id)
  95.  
  96. def get_hhook_id
  97. return HHOOK_ID
  98. end
  99.  
  100. if HHOOK_ID == 0
  101. raise "cannot hook"
  102. end
  103.  
  104. Window.loop do
  105. #result = WinAPI::User32.PeekMessage(msg, hwnd, 0, 0, WinAPI::User32::PM_NOREMOVE)
  106. #result = WinAPI::User32.PeekMessage(msg, hwnd, 0, 0, 0x40)
  107. #result = WinAPI::User32.GetMessage(msg, 0, 0, 0)
  108.  
  109. break if Input.key_push?(K_ESCAPE)
  110. end
Add Comment
Please, Sign In to add comment