Ruby windows API

2021/8/27 7:06:09

本文主要是介绍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

 



这篇关于Ruby windows API的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程