Skip to content

Instantly share code, notes, and snippets.

@mathfur
Created February 11, 2010 03:17
Show Gist options
  • Save mathfur/301167 to your computer and use it in GitHub Desktop.
Save mathfur/301167 to your computer and use it in GitHub Desktop.
# 使用例
# wnd = Window.new("calc") # ハンドラwnd生成
# wnd.post(:wm_command,32781)
class Window
attr_reader :handler
require 'dl/win32'
MESSAGE_TYPE = {
:wm_command => 273,
:wm_keydown => 256,
:wm_keyup => 257,
:wm_lbuttondown => 513,
:wm_lbuttonup => 514,
:wm_char =>0x102,
:wm_copy => 769,
:wm_close => 16,
:wm_copydata => 74,
:wm_clear => 771
}
API = {
:FindWindow => Win32API.new('user32','FindWindow','PP','L'),
:FindWindowEx => Win32API.new('user32','FindWindowEx','LLPP','L'), # @fe=がついてたけど消した
:SendMessage => Win32API.new("user32", "SendMessage",['L']*4,'L'),
:PostMessage => Win32API.new("user32", "PostMessage",['L']*4,'L'),
# TODO: boolが分からないため動かない
:MoveWindow => Win32API.new("user32", "MoveWindow",'LIIIIL','I')
}
def initialize(window_title,subwindow_class=nil,subwindow_caption=nil)
@handler = API[:FindWindow].call(0,window_title)
if subwindow_class && subwindow_caption
@handler=API[:FindWindowEx].call(@handler,0,subwindow_class,subwindow_caption)
end
@handler
end
# FindWindowでウィンドウが取得できたか?
def exist?
@handler!=0
end
# PostMessage
def post(type,w_param=0,l_param=0)
API[:PostMessage].call(@handler,MESSAGE_TYPE[type],w_param,l_param)
end
# SendMessage
def send(type,w_param=0,l_param=0)
API[:SendMessage].call(@handler,MESSAGE_TYPE[type],w_param,l_param)
end
# TODO: boolが分からないため動かない
def move(x,y,x2,y2,flag)
API[:MoveWindow].call(@handler,x1,y1,x2,y2,flag)
end
def ex(class_name,window_title,_) #名前合ってる?
API[:FindWindowEx].call(@handler,class_name,window_title,_)
end
# TODO:
def method_missing(name,*args)
the_api = API[name.to_sym]
raise "存在しないAPIが呼ばれました" unless the_api
#the_api.call(@handler,*args)
raise "未実装です@method_missing"
end
end
class Object
def returned_display
self.display if $VERBOSE
return self
end
def blank?
!self || (self.kind_of?(String) && self=~/^\s*$/) || self==[]
end
end
require "enumerator"
module Enumerable
def map_with_index(&block)
enum_for(:each_with_index)
end
end
class String
def to_rexml
require "rexml/document"
REXML::Document.new(self)
end
# e.g.) "body".wrap("< >") == "<body>"
def wrap(wrapper=nil)
first,last = wrapper.split(" ")
self.sub!(/^/,first)
self.sub!(/$/,last)
end
def chomp(pattern=nil)
super unless pattern
self.sub!(/#{pattern}$/,"")
end
#pattern == nil の時にうまく動かないので保留
#def strip(pattern=nil)
# self.super unless pattern
# self.sub!(/^((#{pattern})?)(.*?)\1$/){ $3 }
#end
end
# グローバル関数
def each_bool(options={},&block)
if options[:type].to_s == "string"
%w{true false}.each(&block)
else
[true,false].each(&block)
end
end
class Visio
def initialize(filename)
require "win32ole"
require "nkf"
@visio = WIN32OLE.new("Visio.Application")
@doc = @visio.Documents.Open(NKF.nkf("-s",filename))
@pages = nil
end
def pages
return @pages if @pages
results = []
@doc.Pages.each do |page|
results << Page.new(page,self)
end
@pages = results
end
end
class Page
attr_reader :name
include Enumerable
def initialize(page,visio)
require "nkf"
@page = page
@visio = visio
@name = NKF.nkf("-w",@page.Name.tr("\n\r",""))
@children = nil
end
def shapes
results = []
@page.shapes.each do |shape|
results << Shape.new(shape)
end
results
end
def parent
@visio
end
def children
return @children if @children
results = []
self.shapes.select{|shape| shape.name=="定義済み処理"}.each do |shape|
results << parent.pages.find{|page| page.name == shape.text}
end
@children = results.compact
end
def method_missing(method_name,*args)
self.call(method_name,*args)
end
end
class Shape
include Enumerable
def initialize(shape)
require "nkf"
@shape = shape
end
def name
NKF.nkf("-w",@shape.Name)
end
def text
NKF.nkf("-w",@shape.Characters.Text)
end
def method_missing(method_name,*args)
self.call(method_name,*args)
end
end
require 'dl/win32'
WM_LBUTTONDOWN = 0x0201
WM_LBUTTONUP = 0x0202
FIND_WINDOW = Win32API.new('user32','FindWindow','PP','L')
FIND_WINDOW_EX = Win32API.new('user32','FindWindowEx','LLPP','L')
SEND_MESSAGE = Win32API.new("user32", "SendMessage",['L']*4,'L')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment