Class: WinUser32Ruby::Window
- Inherits:
-
Object
- Object
- WinUser32Ruby::Window
- Defined in:
- lib/win-user32-ruby.rb
Constant Summary collapse
- TIMEOUT =
10
Instance Attribute Summary collapse
-
#handle ⇒ Object
readonly
Returns the value of attribute handle.
Class Method Summary collapse
-
.find_window(title, class_name = nil) ⇒ Object
Wrapper for
FindWindow. -
.get_foreground_window ⇒ Object
Wrapper for
GetForegroundWindow.
Instance Method Summary collapse
-
#bring_window_to_top ⇒ Object
Wrapper for
BringWindowToTop. - #click ⇒ Object
-
#cursor_center ⇒ Object
- Centers cursor on this window Returns
-
non-zero on success, zero otherwise.
-
#exit! ⇒ Object
- Helper to Post Message to close window Returns
-
non-zero on success, zero otherwise.
-
#find_window_ex(title, child = 0, class_name = nil) ⇒ Object
Wrapper for
FindWindowEx. -
#get_center ⇒ Object
- Gets the coordinates to the center of this window Returns
-
[x, y].
-
#get_client_rect ⇒ Object
Wrapper for
GetClientRect. -
#get_next_child ⇒ Object
Wrapper for
GetWindow. -
#get_top_window ⇒ Object
Wrapper for
GetTopWindow. -
#get_window_rect ⇒ Object
Wrapper for
GetWindowRect. -
#get_window_text ⇒ Object
Wrapper for
WM_GETTEXT. -
#initialize(hwnd) ⇒ Window
constructor
A new instance of Window.
-
#set_focus ⇒ Object
Wrapper for
SetFocus. -
#set_foreground_window ⇒ Object
Wrapper for
SetForegroundWindow. -
#show_window ⇒ Object
Wrapper for
ShowWindow.
Constructor Details
#initialize(hwnd) ⇒ Window
Returns a new instance of Window.
15 16 17 |
# File 'lib/win-user32-ruby.rb', line 15 def initialize(hwnd) @handle = hwnd end |
Instance Attribute Details
#handle ⇒ Object (readonly)
Returns the value of attribute handle.
13 14 15 |
# File 'lib/win-user32-ruby.rb', line 13 def handle @handle end |
Class Method Details
.find_window(title, class_name = nil) ⇒ Object
Wrapper for FindWindow.
title-
is the title of the window we are trying to find
class_name: is the predefined registered control-class name, generally not used
- Return
-
window handle
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/win-user32-ruby.rb', line 25 def self.find_window(title, class_name = nil) fw= user32 'FindWindow', 'PP', 'L' begin wnd = timeout(TIMEOUT) {sleep 0.2 while (h = fw.call class_name, title) <= 0; h} rescue TimeoutError return nil end Window.new wnd end |
.get_foreground_window ⇒ Object
Wrapper for GetForegroundWindow.
- Returns
-
WinUser32Ruby::Window
39 40 41 42 43 44 45 46 |
# File 'lib/win-user32-ruby.rb', line 39 def self.get_foreground_window b = user32 'GetForegroundWindow', 'V', 'L' r = b.call sleep 0.5 r = Window.new(r) end |
Instance Method Details
#bring_window_to_top ⇒ Object
Wrapper for BringWindowToTop.
- Returns
-
non-zero on success, zero otherwise
151 152 153 154 155 |
# File 'lib/win-user32-ruby.rb', line 151 def bring_window_to_top hwnd = @handle b = user32 'BringWindowToTop', 'L', 'I' b.call hwnd end |
#click ⇒ Object
144 145 146 147 |
# File 'lib/win-user32-ruby.rb', line 144 def click cursor_center mouse_click end |
#cursor_center ⇒ Object
Centers cursor on this window
- Returns
-
non-zero on success, zero otherwise
140 141 142 |
# File 'lib/win-user32-ruby.rb', line 140 def cursor_center set_cursor_pos(*get_center) end |
#exit! ⇒ Object
Helper to Post Message to close window
- Returns
-
non-zero on success, zero otherwise
187 188 189 190 |
# File 'lib/win-user32-ruby.rb', line 187 def exit! hwnd = @handle hwnd, WM_CLOSE, 0, 0 end |
#find_window_ex(title, child = 0, class_name = nil) ⇒ Object
Wrapper for FindWindowEx.
title-
is the title of the window we are trying to find
child-
handle to child from which to start the search
class_name: is the predefined registered control-class name, generally not used
- Return
-
window handle
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/win-user32-ruby.rb', line 55 def find_window_ex(title, child = 0, class_name = nil) parent = @handle fw= user32 'FindWindowEx', 'LLPP', 'L' begin wnd = timeout(TIMEOUT) {sleep 0.2 while (h = fw.call parent, child, class_name, title) <= 0; h} rescue TimeoutError return nil end Window.new wnd end |
#get_center ⇒ Object
Gets the coordinates to the center of this window
- Returns
-
[x, y]
133 134 135 136 |
# File 'lib/win-user32-ruby.rb', line 133 def get_center r = get_window_rect [(r[0] + r[2]) / 2, (r[1] + r[3]) / 2] end |
#get_client_rect ⇒ Object
Wrapper for GetClientRect.
- Return
-
[left, top, right, bottom], coordinates are in screen coordinates and not relative to the client window as in the equivalent user32.dll
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/win-user32-ruby.rb', line 118 def get_client_rect hwnd = @handle crect = [0, 0, 0, 0].pack("L*") gr = user32 'GetClientRect', 'LP', 'I' gr.call hwnd, crect left, top, right, bottom = crect.unpack("L*") crect = [left, top, right, bottom].map { |e| [e].pack('L').unpack('l').first } pout = client_to_screen(hwnd, crect[0], crect[1]) [pout[0], pout[1], pout[0] + crect[2], pout[1] + crect[3]] end |
#get_next_child ⇒ Object
Wrapper for GetWindow.
- Return
-
handle to window according to
cmd
82 83 84 85 86 87 |
# File 'lib/win-user32-ruby.rb', line 82 def get_next_child hwnd, cmd = @handle, GW_CHILD fw = user32 'GetWindow', 'LI', 'L' Window.new fw.call(hwnd, cmd) end |
#get_top_window ⇒ Object
Wrapper for GetTopWindow.
- Return
-
window handle
71 72 73 74 75 76 77 78 |
# File 'lib/win-user32-ruby.rb', line 71 def get_top_window parent = @handle fw= user32 'GetTopWindow', 'L', 'L' wnd = timeout(TIMEOUT) {sleep 0.2 while (h = fw.call parent) <= 0; h} Window.new wnd end |
#get_window_rect ⇒ Object
Wrapper for GetWindowRect.
- Return
- left, top, right, bottom
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/win-user32-ruby.rb', line 103 def get_window_rect hwnd = @handle rect = [0, 0, 0, 0].pack("L*") gr = user32 'GetWindowRect', 'LP', 'I' gr.call hwnd, rect left, top, right, bottom = rect.unpack("L*") [left, top, right, bottom].map{|e| [e].pack('L').unpack('l').first} end |
#get_window_text ⇒ Object
Wrapper for WM_GETTEXT.
- Return
-
window text
91 92 93 94 95 96 97 98 |
# File 'lib/win-user32-ruby.rb', line 91 def get_window_text hwnd = @handle buffer = ' ' * 2048 length = hwnd, WM_GETTEXT, buffer.length, buffer length == 0 ? '' : buffer[0..length - 1] end |
#set_focus ⇒ Object
Wrapper for SetFocus.
- Returns
-
non-zero on success, zero otherwise
159 160 161 162 163 |
# File 'lib/win-user32-ruby.rb', line 159 def set_focus hwnd = @handle b = user32 'SetFocus', 'L', 'L' b.call hwnd end |
#set_foreground_window ⇒ Object
Wrapper for SetForegroundWindow.
- Returns
-
non-zero on success, zero otherwise
167 168 169 170 171 172 173 174 175 |
# File 'lib/win-user32-ruby.rb', line 167 def set_foreground_window hwnd = @handle b = user32 'SetForegroundWindow', 'L', 'I' r = b.call hwnd sleep 0.5 r end |
#show_window ⇒ Object
Wrapper for ShowWindow.
- Returns
-
non-zero on success, zero otherwise
179 180 181 182 183 |
# File 'lib/win-user32-ruby.rb', line 179 def show_window hwnd = @handle b = user32 'ShowWindow', 'LI', 'I' b.call hwnd, SW_RESTORE end |