系统相关
首页 > 系统相关> > Ruby windows API

Ruby windows API

作者:互联网

Get cursor position with Win32API

result = "0"*8   # Eight bytes (enough for two longs)
getCursorXY = Win32API.new("user32","GetCursorPos",["P"],"V")
getCursorXY.call(result)
x, y = result.unpack("LL")  # Two longs

Use Win32API to get a pointer a function

require "Win32API"

def system(cmd)
  sys = Win32API.new("crtdll", "system", ['P'], 'L')
  sys.Call(cmd)
end

system("dir")  # cmd /c not needed!

Read from keyboard

require 'Win32API'

def getch
  @getch ||= Win32API.new('crtdll', '_getch', [], 'L')
  @getch.call
end

while (c = getch) != ?\e
  puts "You typed #{c.chr.inspect}"
end

Working with Microsoft Windows: open a dialog box:

require 'Win32API'

title = "My Application"
text = "Hello, world!"

Win32API.new('user32', 'MessageBox', %w{L P P L}, 'I').call(0, text, title, 0)

 

Open a dialog and check the result

require 'Win32API'

title = "My Application"
text = "Hello, world!"

dialog = Win32API.new('user32', 'MessageBox', 'LPPL', 'I')
result = dialog.call(0, text, title, 1)

case result
  when 1:
    puts "Clicked OK"
  when 2:
    puts "Clicked Cancel"
  else
    puts "Clicked something else!"
end

 

标签:getch,end,windows,text,API,result,new,Ruby,Win32API
来源: https://www.cnblogs.com/unicornsir/p/15191116.html