Class: Win32::Screenshot::BitmapMaker

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/win32/screenshot/bitmap_maker.rb

Overview

internal methods

Defined Under Namespace

Classes: WindowStruct

Constant Summary collapse

EnumWindowCallback =
FFI::Function.new(:bool, [ :long, :pointer ], { :convention => :stdcall }) do |hwnd, param|
  searched_window = WindowStruct.new param
  if(searched_window[:search_class] != 0)
    title = Util.window_class(hwnd)
  else
    title = Util.window_title(hwnd)
  end
  if title =~ Regexp.new(searched_window[:title].read_string) && window_visible(hwnd)
    searched_window[:hwnd] = hwnd
    false
  else
    if(searched_window[:search_class] != 0)
      # if they're searching for a classname, enumerate children, too
      enum_child_windows(hwnd, EnumWindowCallback, param)
      if searched_window[:hwnd] != 0
        # return early if already discovered
        false 
      else
       true
      end
    else
      true
    end
  end
end
SW_RESTORE =
9
SRCCOPY =
0x00CC0020
DIB_RGB_COLORS =
0

Class Method Summary collapse

Class Method Details

.capture_all(hwnd, &proc) ⇒ Object



153
154
155
156
# File 'lib/win32/screenshot/bitmap_maker.rb', line 153

def capture_all(hwnd, &proc)
  width, height = Util.dimensions_for(hwnd)
  capture_area(hwnd, 0, 0, width, height, &proc)
end

.capture_area(hwnd, x1, y1, x2, y2) ⇒ Object

block



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/win32/screenshot/bitmap_maker.rb', line 161

def capture_area(hwnd, x1, y1, x2, y2) # block
  hScreenDC = dc(hwnd)
  w = x2-x1
  h = y2-y1

  hmemDC = create_compatible_dc(hScreenDC)
  hmemBM = create_compatible_bitmap(hScreenDC, w, h)
  select_object(hmemDC, hmemBM)
  bit_blt(hmemDC, 0, 0, w, h, hScreenDC, x1, y1, SRCCOPY)
  bitmap_size = w * h * 3 + w % 4 * h
  lpvpxldata = FFI::MemoryPointer.new(bitmap_size)

  # Bitmap header
  # http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html
  bmInfo = [40, w, h, 1, 24, 0, 0, 0, 0, 0, 0, 0].pack('L3S2L6')
  di_bits(hmemDC, hmemBM, 0, h, lpvpxldata, bmInfo, DIB_RGB_COLORS)

  bmFileHeader = [
          19778,
          bitmap_size + 40 + 14,
          0,
          0,
          54
  ].pack('SLSSL')

  bmp_data = bmFileHeader + bmInfo + lpvpxldata.read_string(bitmap_size)
  yield(w, h, bmp_data)
ensure
  lpvpxldata.free
  delete_object(hmemBM)
  delete_dc(hmemDC)
  release_dc(0, hScreenDC)
end

.get_process_id_from_hwnd(hwnd) ⇒ Object



147
148
149
150
151
# File 'lib/win32/screenshot/bitmap_maker.rb', line 147

def get_process_id_from_hwnd hwnd
  out = FFI::MemoryPointer.new(:uint)
  GetWindowThreadProcessId(hwnd, out)
  out.get_uint32(0) # read_uint
end

.hwnd(window_title, search_class = false) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/win32/screenshot/bitmap_maker.rb', line 105

def hwnd(window_title, search_class = false)
  window = WindowStruct.new
  unless window_title.is_a?(Regexp)
    window_title = Regexp.escape(window_title.to_s)
  else
    window_title = window_title.to_s
  end
  window_title = FFI::MemoryPointer.from_string(window_title)
  window[:title] = window_title
  window[:search_class] = search_class ? 1 : 0
  enum_windows(EnumWindowCallback, window.to_ptr)
  window[:hwnd] == 0 ? nil : window[:hwnd]
end

.prepare_window(hwnd, pause) ⇒ Object



119
120
121
122
123
# File 'lib/win32/screenshot/bitmap_maker.rb', line 119

def prepare_window(hwnd, pause)
  restore(hwnd) if minimized(hwnd)
  set_foreground(hwnd)
  sleep pause
end

.restore(hwnd) ⇒ Object



127
128
129
# File 'lib/win32/screenshot/bitmap_maker.rb', line 127

def restore(hwnd)
  show_window(hwnd, SW_RESTORE)
end

.set_foreground(hwnd) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/win32/screenshot/bitmap_maker.rb', line 131

def set_foreground(hwnd)
  if foreground_window != hwnd
    set_foreground_window(hwnd)
    set_active_window(hwnd)
    bring_window_to_top(hwnd)
    # and just in case...
    foreground_thread = window_thread_process_id(foreground_window, nil)
    other_thread = window_thread_process_id(hwnd, nil)
    attach_thread_input(foreground_thread, other_thread, true) unless other_thread == foreground_thread
    set_foreground_window(hwnd)
    set_active_window(hwnd)
    bring_window_to_top(hwnd)
    attach_thread_input(foreground_thread, other_thread, false) unless other_thread == foreground_thread
  end
end