Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- require "dl/import"
- require "dxruby"
- module WinAPI
- module User32
- SWP_NOSIZE = 1
- SM_CXFULLSCREEN = 0
- SM_CYFULLSCREEN = 1
- PM_NOREMOVE = 0
- PM_REMOVE = 1
- # BOOL PeekMessage(
- # LPMSG lpMsg, // メッセージ情報
- # HWND hWnd, // ウィンドウのハンドル
- # UINT wMsgFilterMin, // 最初のメッセージ
- # UINT wMsgFilterMax, // 最後のメッセージ
- # UINT wRemoveMsg // 削除オプション
- # );
- # typedef struct tagMSG { // msg
- # HWND hwnd;
- # UINT message;
- # WPARAM wParam;
- # LPARAM lParam;
- # DWORD time;
- # POINT pt;
- # } MSG;
- # HHOOK SetWindowsHookEx(
- # int idHook, // フックタイプ
- # HOOKPROC lpfn, // フックプロシージャ
- # HINSTANCE hMod, // アプリケーションインスタンスのハンドル
- # DWORD dwThreadId // スレッドの識別子
- # );
- # DWORD GetWindowThreadProcessId(
- # HWND hWnd, // ウィンドウのハンドル
- # LPDWORD lpdwProcessId // プロセス ID
- # );
- # LRESULT CallNextHookEx(
- # HHOOK hhk, // 現在のフックのハンドル
- # int nCode, // フックプロシージャに渡すフックコード
- # WPARAM wParam, // フックプロシージャに渡す値
- # LPARAM lParam // フックプロシージャに渡す値
- # );
- extend DL::Importer
- dlload "user32"
- extern "int GetSystemMetrics(int)"
- extern "int PeekMessage(void*, unsigned int, unsigned int, unsigned int, unsigned int)"
- extern "int GetMessage(void*, unsigned int, unsigned int, unsigned int)"
- extern "int SetWindowsHookEx(int, void*, int, unsigned int)"
- extern "unsigned int GetWindowThreadProcessId(unsigned int, void*)"
- extern "long CallNextHookEx(unsigned int, int, unsigned int, unsigned long)"
- # LRESULT CALLBACK CKeyboardHook::KeyboardProc(int p_nCode, WPARAM p_wParam, LPARAM p_lParam)
- MessageCallback = bind("long message_callback(int, unsigned int, long)") { |n_code, wparam, lparam|
- if n_code < 0 || n_code == 3 # HC_NOREMOVE = 3
- return WinAPI::User32.CallNextHookEx(get_hhook_id, n_code, wparam, lparam)
- end
- puts "%d %d %d" % [n_code, wparam, lparam]
- WinAPI::User32.CallNextHookEx(get_hhook_id, n_code, wparam, lparam)
- return 1
- }
- POINT = struct(["long x", "long y"])
- MSG = struct(["unsigned long hwnd", "unsigned int message", "unsigned int wParam", "unsigned long lParam",
- "unsigned int time", "void* pt"])
- end
- end
- def window_align_center
- hwnd = Window.hWnd
- desktop_width = WinAPI::User32.GetSystemMetrics(WinAPI::User32::SM_CXFULLSCREEN)
- desktop_height = WinAPI::User32.GetSystemMetrics(WinAPI::User32::SM_CYFULLSCREEN)
- Window.x = (desktop_width - Window.width) / 2
- Window.y = (desktop_height - Window.height) / 2
- end
- window_align_center
- msg = WinAPI::User32::MSG.malloc
- hwnd = Window.hWnd
- #Window.fps = 10
- WH_GETMESSAGE = 3
- thread_id = WinAPI::User32.GetWindowThreadProcessId(hwnd, 0)
- HHOOK_ID = WinAPI::User32.SetWindowsHookEx(2, WinAPI::User32::MessageCallback, 0, thread_id)
- def get_hhook_id
- return HHOOK_ID
- end
- if HHOOK_ID == 0
- raise "cannot hook"
- end
- Window.loop do
- #result = WinAPI::User32.PeekMessage(msg, hwnd, 0, 0, WinAPI::User32::PM_NOREMOVE)
- #result = WinAPI::User32.PeekMessage(msg, hwnd, 0, 0, 0x40)
- #result = WinAPI::User32.GetMessage(msg, 0, 0, 0)
- break if Input.key_push?(K_ESCAPE)
- end
Add Comment
Please, Sign In to add comment