Class: Vmopt::NotePad

Inherits:
Object
  • Object
show all
Defined in:
lib/vmopt/notepad.rb

Instance Method Summary collapse

Constructor Details

#initialize(opt) ⇒ NotePad

功能:1.初始化一个notepad程序,如果给定路径,则打开特定路径下的文件,同时激活该窗口2.如果不给定路径,则打开notepad,同时激活该窗口。参数:txt_path: 文件路径open_window: 表示需不需要创建该notepad窗口,默认创建,不设置表示获取该窗口



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vmopt/notepad.rb', line 17

def initialize(opt)
  
  if opt[:txt_path].nil?
    @title = "记事本"
  else  
    basename = File.basename(opt[:txt_path]).to_utf8
    @title = "#{basename} - 记事本"
    @path = opt[:txt_path]
  end

  if opt[:open_window] 
    winexe = Win32API.new('kernel32', 'WinExec', 'PI', 'I')
   opt[:txt_path].nil? ? winexe.call("notepad.exe", 1) : winexe.call("notepad.exe #{opt[:txt_path]}", 1)
    sleep 1
  end

  @window = WinUtils::find_window(/#{@title}/) 
  RAutomation::Window.wait_timeout=40
  
end

Instance Method Details

#closeObject

功能:关闭记事本



82
83
84
# File 'lib/vmopt/notepad.rb', line 82

def close
  @window.WinClose("[CLASS:Notepad]", "")
end

#read_textObject

功能:读取一个已经打开的notepad的里面的内容



48
49
50
# File 'lib/vmopt/notepad.rb', line 48

def read_text
  @window.text_field(:class => "Edit", :id => 15).value
end

#save(filepath = nil) ⇒ Object

功能:点击菜单中的保存按钮来保存文件参数:filepath:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/vmopt/notepad.rb', line 59

def save(filepath=nil)
  if !filepath.nil?
    @window.WinMenuSelectItem("[CLASS:Notepad]", "", "文件", "另存为")

  savewindow = WinUtils::find_window(/另存为/)
  savewindow.text_field(:class => "Edit").set(filepath)       
   #窗口关闭,遍历button会导致卡死,

   # savewindow.buttons.each do|button| 

   #   if button.value == "保存(&S)";

   #    button.click; 

   #   end

   # end

   
  savewindow.send_keys("{ENTER}")    
  sleep 1
  elsif !@path.nil? and filepath.nil?
    @window.WinMenuSelectItem("[CLASS:Notepad]", "", "文件", "保存")      
  elsif @path.nil? and filepath.nil?
    raise WinUtils::NoSavePathError,"Not found the save path ."
  end
end

#set_text(msg) ⇒ Object

功能:1.对一个激活的窗口设置文本内容参数:msg:表示即将填充的信息



42
43
44
# File 'lib/vmopt/notepad.rb', line 42

def set_text(msg)
  @window.text_field(:class => "Edit", :id => 15).set(msg)
end