我正在尝试在ruby中创建一个小课程,允许我制作系统托盘图标并使用通知气球。但是我在使用Win32API调用的结构时遇到了问题。
http://www.ruby-forum.com/topic/18102
Shell_NotifyIcon Function
NOTIFYICONDATA Struct
这是我现在拥有的代码,只需在系统托盘中添加一个图标:
require 'Win32API'
NIF_MESSAGE = 1
NIF_ICON = 2
NIF_TIP = 4
NIF_STATE = 8
NIF_INFO = 10
NIM_ADD = 0
NIM_MODIFY = 1
NIM_DELETE = 2
NIS_HIDDEN = 1
NIS_SHAREDICON = 2
class NotifyIconData < Struct.new(:cbsize, :hwnd, :uid, :uflags, :ucallbackmessage, :hicon)#, :sztip, :dwstate, :dwstatemask, :szinfo, :utimeout, :uversion, :szinfotitle, :dwinfoflags, :guiditem, :hballoonicon)
def pack
values.pack('LLIIIL')
end
# def self.unpack(s)
# new(*s.unpack('LLIIIL'))
# end
end
#===---
ExtractIcon = Win32API.new('shell32', 'ExtractIcon', 'LPI', 'L')
Shell_NotifyIcon = Win32API.new('shell32', 'Shell_NotifyIconA', 'LP', 'I')
hicoY = ExtractIcon.call(0, 'C:\WINDOWS\SYSTEM32\INETCPL.CPL', 21) # Green tick
hicoN = ExtractIcon.call(0, 'C:\WINDOWS\SYSTEM32\INETCPL.CPL', 22) # Red minus
#===---
tiptxt = "hai"
nid = NotifyIconData.new
nid.cbsize = Marshal.dump(NotifyIconData).size#6*4+64
nid.hwnd = 0
nid.uid = 'ruby'.hash
nid.uflags = NIF_INFO
nid.ucallbackmessage = 0
nid.hicon = hicoY
ret = Shell_NotifyIcon.call( NIM_ADD, nid.pack << tiptxt << "\0"*(64 - tiptxt.size) )
p 'Err: NIM_ADD' if ret == 0
sleep(3) # <----<<
# pnid = [6*4+64, 0, 'ruby'.hash, NIF_ICON | NIF_TIP, 0, hicoN].pack('LLIIIL') << tiptxt << "\0"*(64 - tiptxt.size)
# ret = Shell_NotifyIcon.call(NIM_MODIFY, pnid)
# p 'Err: NIM_MODIFY' if ret == 0
# sleep(6) # <----<<
nid.uflags = 0
ret = Shell_NotifyIcon.call( NIM_DELETE, nid.pack << "\0" )
p 'Err: NIM_DELETE' if ret == 0