Module: Win::Gui::Window

Extended by:
Library
Included in:
Win::Gui, Dialog
Defined in:
lib/win/gui/window.rb

Overview

Contains constants and Win32API functions related to window manipulation

Constant Summary collapse

SW_HIDE =

Hides the window and activates another window.

0
SW_NORMAL =

Same as SW_SHOWNORMAL

1
SW_SHOWNORMAL =

Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.

1
SW_SHOWMINIMIZED =

Activates the window and displays it as a minimized window.

2
SW_SHOWMAXIMIZED =

Activates the window and displays it as a maximized window.

3
SW_MAXIMIZE =

Activates the window and displays it as a maximized window.

3
SW_SHOWNOACTIVATE =

Displays a window in its most recent size and position. Similar to SW_SHOWNORMAL, but the window is not activated.

4
SW_SHOW =

Activates the window and displays it in its current size and position.

5
SW_MINIMIZE =

Minimizes the specified window, activates the next top-level window in the Z order.

6
SW_SHOWMINNOACTIVE =

Displays the window as a minimized window. Similar to SW_SHOWMINIMIZED, except the window is not activated.

7
SW_SHOWNA =

Displays the window in its current size and position. Similar to SW_SHOW, except the window is not activated.

8
SW_RESTORE =

Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.

9
SW_SHOWDEFAULT =

Sets the show state based on the SW_ value specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application.

10
SW_FORCEMINIMIZE =

Windows 2000/XP: Minimizes a window, even if the thread that owns the window is not responding. Only use this flag when minimizing windows from a different thread.

11
GW_HWNDFIRST =

GetWindow constants:

0
GW_HWNDLAST =
1
GW_HWNDNEXT =
2
GW_HWNDPREV =
3
GW_OWNER =
4
GW_CHILD =
5
GW_ENABLEDPOPUP =
6
GA_PARENT =

GetAncestor constants:

1
GA_ROOT =
2
GA_ROOTOWNER =
3

Constants included from Library

Library::TYPES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Library

callback, define_api, define_snake_method, enforce_count, extended, function, generate_names, generate_signature, generate_snake_method_body, try_function

Class Method Details

.return_enumObject

Def_block that calls API function expecting EnumWindowsProc callback (EnumWindows, EnumChildWindows, …). Default callback pushes all passed handles into Array that is returned if Enum function call was successful. If runtime block is given it is called after the end of default callback (handle Array is still being collected and returned by the method). If Enum… function call fails, method returns nil, otherwise an Array of all window handles passed into callback.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/win/gui/window.rb', line 67

def return_enum #:nodoc:
  lambda do |api, *args, &block|
    args.push 0 if args.size == api.prototype.size - 2 # If value is missing, it defaults to 0
    handles = []

    # Insert callback proc into appropriate place of args Array
    args[api.prototype.find_index(:EnumWindowsProc), 0] =
            proc do |handle, message|
              handles << handle
              block ? block[handle, message] : true
            end
    handles if api.call *args
  end
end

.return_string(encode = nil) ⇒ Object

Helper method that creates def_block returning (possibly encoded) string as a result of api function call or nil if zero characters was returned by api call



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/win/gui/window.rb', line 85

def return_string( encode = nil )  #:nodoc:
  lambda do |api, *args|
    namespace.enforce_count( args, api.prototype, -2)
    buffer = FFI::MemoryPointer.new :char, 1024
    args += [buffer, buffer.size]
    num_chars = api.call(*args)
    return nil if num_chars == 0
    if encode
      string = buffer.get_bytes(0, num_chars*2)
      string = string.force_encoding('utf-16LE').encode(encode)
    else
      string = buffer.get_bytes(0, num_chars)
    end
    string.rstrip
  end
end

Instance Method Details

#foreground?(win_handle) ⇒ Boolean

Tests if given window handle points to foreground (topmost) window

Returns:

  • (Boolean)


828
829
830
# File 'lib/win/gui/window.rb', line 828

def foreground?( win_handle )
  win_handle == foreground_window
end

#hide_window(win_handle) ⇒ Object

Hides the window and activates another window



821
822
823
# File 'lib/win/gui/window.rb', line 821

def hide_window( win_handle )
  show_window(win_handle, SW_HIDE)
end

#shut_window(win_handle) ⇒ Object

Shuts down the window created by different thread by posting WM_SYSCOMMAND, SC_CLOSE message to it. This closely emulates user clicking on X button of the target window. As it would be expected, this actually gives the target window chance to close gracefully (it may ask user to save data and stuff). I have not found so far how to REALLY destroy window in different thread without it asking user anything.



838
839
840
# File 'lib/win/gui/window.rb', line 838

def shut_window( win_handle )
  post_message(win_handle, Win::Gui::Message::WM_SYSCOMMAND, Win::Gui::Message::SC_CLOSE, nil)
end

#text(win_handle, buffer_size = 1024) ⇒ Object

Returns text associated with window by sending WM_GETTEXT message to it.


Remarks: It is different from GetWindowText that returns only window title



847
848
849
850
851
# File 'lib/win/gui/window.rb', line 847

def text( win_handle, buffer_size=1024 )
  buffer = FFI::MemoryPointer.new :char, buffer_size
  num_chars = send_message win_handle, Win::Gui::Message::WM_GETTEXT, buffer.size, buffer
  num_chars == 0 ?  nil : buffer.get_bytes(0, num_chars)
end