MASM32编程访问系统托盘区图标/07-09-29更新

;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; FileName: TrayIcon.asm
;   Function: Demo the way to enum the icons in system tray
;       Author: Purple Endurer | 紫郢剑侠㊣ (PurpleEndurer@163.com)
;     DevEnv: Win2000 pro SP4, MASM32 v8
;
;  log
; ----------------------------------------------------------------------------------
; 2007-09-29  Can run under Win XP
; 2007-09-22  Created!
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


.386
.model  flatstdcall
option  casemap: none

include /masm32/ include/windows.inc

include /masm32/ include/kernel32.inc
includelib /masm32/ lib/kernel32.lib

include /masm32/ include/user32.inc
includelib /masm32/ lib/user32.lib

GetSysTrayToolBarHandle  proto
EnumSubCtl  proto :HWND, :LPARAM
GetSysTrayIconCount  proto
EnumSysTrayIcon  proto

.data
g_szAppName  db  "EnumSysTrayIcon", 0
g_szTaskBarCls  db  "Shell_TrayWnd", 0
g_szSysPagerCls  db  "SysPager", 0     ;WinXP need!
g_szTrayNotifyWndCls  db  "TrayNotifyWnd", 0
g_szToolbarWindow32Cls  db  "ToolbarWindow32", 0
g_szFailGetSysTray  db  "Fail to get system tray!", 0
g_hSysTray      HANDLE ?
g_dwTrayIconCount  dword ?
g_stTbButton TBBUTTON <>
g_szIconText  db MAX_PATH dup (?)

.code
start:
invoke GetSysTrayToolBarHandle
test    eaxeax  ; .if  eax==NULL
.if ZERO?
     invoke MessageBox, NULL,  addr g_szFailGetSysTray,  addr g_szAppName, MB_ICONERROR
.else
     mov     g_hSysTray,  eax
     invoke EnumSysTrayIcon
.endif
invoke ExitProcess,NULL

;
; Function: Get the handle ToolbarWindow32 of in system tray
;           Shell_TrayWnd -> TrayNotifyWnd -> (WinXP:SysPager) -> ToolbarWindow32
;    Onput: if  fail eax=NULL, else eax = handle
;/
GetSysTrayToolBarHandle  proc
    ;--- Get the handle of task bar
    invoke FindWindow,  addr g_szTaskBarCls, NULL
    cmp      eax, NULL
    je         @GetSysTrayToolBarHandleRet  ; fail

      ; HWND FindWindowEx(
      ;     HWND hwndParent,    // handle to parent window
      ;     HWND hwndChildAfter,    // handle to a child window 
      ;     LPCTSTR lpszClass,    // pointer to class name
      ;     LPCTSTR lpszWindow    // pointer to window name
      ; );

     ;--- Get the handle of TrayNotifyWnd in task bar
     invoke FindWindowEx,  eax, NULL,   addr  g_szTrayNotifyWndCls,   NULL
     cmp     eax, NULL
     je         @GetSysTrayToolBarHandleRet

     ;--- (WinXP Only) Get the handle of g_szSysPager in TrayNotifyWnd 
     push  eax
     invoke FindWindowEx,  eax, NULL,   addr  g_szSysPagerCls, NULL
     .if    ( eax==NULL)
         pop  eax
     .else
         pop  edi
     .endif

     ;--- Get the handle of ToolbarWindow32 in TrayNotifyWnd
     invoke FindWindowEx,  eax, NULL,   addr  g_szToolbarWindow32Cls,   NULL

@GetSysTrayToolBarHandleRet:
     ret
GetSysTrayToolBarHandle  endp


;/
; Function: Enum the Child window in task bar
;/
EnumSubCtl  proc  proc  hWnd: HWND, lParam: LPARAM
     invoke GetClassName, hWnd,  addr g_szIconText, sizeof g_szIconText
     invoke MessageBox, NULL,  addr g_szIconText,  addr g_szIconText, MB_OK

     mov      eaxTRUE
     ret
EnumSubCtl  endp


;/
; Function: Get the count of icon in system tray
;/
GetSysTrayIconCount  proc
     invoke SendMessage,g_hSysTray, TB_BUTTONCOUNT, 0, 0
     mov     g_dwTrayIconCount,  eax    
     ret
GetSysTrayIconCount  endp

;///
; Function: Enum the icon in system tray
;///
EnumSysTrayIcon  proc
     local dwProcID, dwReaded:  dword
     local hProcess: HANDLE
     local pMem:  dword

     invoke GetSysTrayIconCount

     invoke GetWindowThreadProcessId, g_hSysTray,  addr dwProcID
     invoke OpenProcess, PROCESS_VM_OPERATION  or PROCESS_VM_READ  or PROCESS_VM_WRITE,  FALSE, dwProcID
     mov    hProcess,  eax
     invoke VirtualAllocEx, hProcess,  NULL, 1024, MEM_RESERVE  or MEM_COMMIT,  PAGE_READWRITE
     mov    pMem,  eax

     xor       eaxeax
     .while ( eax < g_dwTrayIconCount)
         push    eax

         invoke SendMessage, g_hSysTray, TB_GETBUTTON,  eax,  pMem
         invoke ReadProcessMemory, hProcess, pMem,  addr g_stTbButton, sizeof g_stTbButton,   addr dwReaded

         invoke SendMessage, g_hSysTray, TB_GETBUTTONTEXT, g_stTbButton.idCommand,  pMem

         inc        eax   ; If fail, the return value is  -1
         jz         @F    ; Fail, skip

         invoke ReadProcessMemory, hProcess,  pMem,    addr g_szIconText, sizeof g_szIconText,  addr dwReaded
         invoke MessageBox, NULL,  addr g_szIconText,  addr g_szAppName, NULL
@@:
         pop       eax
         inc        eax
     .endw

     invoke VirtualFreeEx, hProcess, pMem,  0,  MEM_RELEASE
     invoke CloseHandle, hProcess

     ret
EnumSysTrayIcon  endp

end start
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫郢剑侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值