tags:

views:

126

answers:

1

I'm trying to make a little class in ruby allowing me to make a system tray icon and use the notification balloons. But I'm having problems using structs with Win32API calls.

http://www.ruby-forum.com/topic/18102
Shell_NotifyIcon Function
NOTIFYICONDATA Struct

Heres the code I have now, all it does is add an icon to the system tray:

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
A: 

check out visualuruby and its demo/example files--they got me some balloons quite readily.

rogerdpack