name22

Get TrayIcon Position (_Shell_NotifyIconGetRect)

May 12th, 2013
141
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WindowsConstants.au3>
  2. #include <GUIConstants.au3>
  3. #include <Constants.au3>
  4. #include <WinAPI.au3>
  5.  
  6. ; -Author: name22 (www.autoit.de)
  7.  
  8. ;Auszug aus WinAPIEx.au3:
  9. Global Const $NIM_ADD = 0x00
  10. Global Const $NIM_MODIFY = 0x01
  11. Global Const $NIM_DELETE = 0x02
  12. Global Const $NIM_SETFOCUS = 0x03
  13. Global Const $NIM_SETVERSION = 0x04
  14.  
  15. Global Const $NIF_MESSAGE = 0x01
  16. Global Const $NIF_ICON = 0x02
  17. Global Const $NIF_TIP = 0x04
  18. Global Const $NIF_STATE = 0x08
  19. Global Const $NIF_INFO = 0x10
  20. Global Const $NIF_GUID = 0x20
  21. Global Const $NIF_REALTIME = 0x40
  22. Global Const $NIF_SHOWTIP = 0x80
  23.  
  24. Global Const $NIS_HIDDEN = 0x01
  25. Global Const $NIS_SHAREDICON = 0x02
  26.  
  27. Global Const $NIIF_NONE = 0x00
  28. Global Const $NIIF_INFO = 0x01
  29. Global Const $NIIF_WARNING = 0x02
  30. Global Const $NIIF_ERROR = 0x03
  31. Global Const $NIIF_USER = 0x04
  32. Global Const $NIIF_NOSOUND = 0x10
  33. Global Const $NIIF_LARGE_ICON = 0x10
  34. Global Const $NIIF_RESPECT_QUIET_TIME = 0x80
  35. Global Const $NIIF_ICON_MASK = 0x0F
  36.  
  37. Global Const $tagNOTIFYICONDATA = 'dword Size;hwnd hWnd;uint ID;uint Flags;uint CallbackMessage;ptr hIcon;wchar Tip[128];dword State;dword StateMask;wchar Info[256];uint Version;wchar InfoTitle[64];dword InfoFlags;' ;aus WinAPIEx.au3
  38. Global Const $tagNOTIFYICONIDENTIFIER = "DWORD cbSize;HWND hWnd;UINT uID;BYTE guidItem[16]" ;Struktur um ein TrayIcon eindeutig zu identifizieren (http://msdn.microsoft.com/en-us/library/dd391553%28v=vs.85%29.aspx)
  39. Global Const $WM_NOTIFYICONCALLBACK = _WinAPI_RegisterWindowMessage("WM_NOTIFYICONCALLBACK") ;Registriert eine WM mit dem angegebenen String die garantiert noch frei ist.
  40.  
  41. Global $hIcon, $hGUI, $cLabel_Pos, $tIconData, $tIconData_Delete, $tIconIdentifier
  42. Global $iNotifyIconID = 1 ;Beliebige ID für das neue TrayIcon (darf noch nicht in Benutzung sein)
  43.  
  44. GUIRegisterMsg($WM_NOTIFYICONCALLBACK, "_IconCallback") ;Registriert den MessageHandler über unsere eigene WM
  45.  
  46. $hIcon = _WinAPI_ExtractIcon(@AutoItExe, 0, True) ;Lädt das AutoIt Icon aus der AutoIt3.exe
  47.  
  48. $hGUI = GUICreate("IconParent", 200, 100) ;Ein Fenster muss vorhanden sein um TrayIcon zu registrieren.
  49. $cLabel_Pos = GUICtrlCreateLabel("", 5, 5, 190, 25)
  50. GUISetState()
  51.  
  52. $tIconData = DllStructCreate($tagNOTIFYICONDATA) ;Enthält alle Daten um TrayIcons zu erstellen/modifizieren/löschen (http://msdn.microsoft.com/en-us/library/bb773352%28v=vs.85%29.aspx)
  53. DllStructSetData($tIconData, "Size", DllStructGetSize($tIconData))
  54. DllStructSetData($tIconData, "hWnd", $hGUI)
  55. DllStructSetData($tIconData, "ID", $iNotifyIconID)
  56. DllStructSetData($tIconData, "Flags", BitOR($NIF_ICON, $NIF_MESSAGE)) ;Flags um Icon zu setzen und eine CallbackMessage zu registrieren
  57. DllStructSetData($tIconData, "CallbackMessage", $WM_NOTIFYICONCALLBACK)
  58. DllStructSetData($tIconData, "hIcon", $hIcon) ;Iconhandle für das Icon des TrayItems
  59.  
  60. $tIconIdentifier = DllStructCreate($tagNOTIFYICONIDENTIFIER)
  61. DllStructSetData($tIconIdentifier, "cbSize", DllStructGetSize($tIconIdentifier))
  62. DllStructSetData($tIconIdentifier, "hWnd", $hGUI)
  63. DllStructSetData($tIconIdentifier, "uID", $iNotifyIconID)
  64. DllStructSetData($tIconIdentifier, "guidItem", 0)
  65.  
  66. _WinAPI_ShellNotifyIcon($NIM_ADD, $tIconData) ;Erstellt unser TrayIcon
  67.  
  68. AdlibRegister("_GetPosition", 500)
  69.  
  70. While True
  71.     Switch GUIGetMsg()
  72.         Case $GUI_EVENT_CLOSE
  73.             ;Struktur mit Daten um TrayIcon zu löschen
  74.             $tIconData_Delete = DllStructCreate($tagNOTIFYICONDATA)
  75.             DllStructSetData($tIconData_Delete, "Size", DllStructGetSize($tIconData))
  76.             DllStructSetData($tIconData, "hWnd", $hGUI)
  77.             DllStructSetData($tIconData, "ID", $iNotifyIconID)
  78.  
  79.             _WinAPI_ShellNotifyIcon($NIM_DELETE, $tIconData_Delete) ;Löscht unser TrayIcon
  80.             _WinAPI_DestroyIcon($hIcon) ;Icon aus RAM entfernen
  81.             Exit
  82.     EndSwitch
  83. WEnd
  84.  
  85. Func _IconCallback($hWnd, $iMsg, $wParam, $lParam) ;Callback Funktion
  86.     ConsoleWrite($iMsg & @TAB & $lParam & @CRLF)
  87. EndFunc
  88.  
  89. Func _GetPosition()
  90.     $tRect = DllStructCreate($tagRECT) ;Rect Struktur die Position erhalten wird
  91.     _Shell_NotifyIconGetRect(DllStructGetPtr($tIconIdentifier), DllStructGetPtr($tRect)) ;Identifier identifiziert das TrayIcon, Rect empfängt die Positionsdaten
  92.     GUICtrlSetData($cLabel_Pos, "X: " & DllStructGetData($tRect, "left") & @TAB & @TAB & "Y: " & DllStructGetData($tRect, "top"))
  93. EndFunc
  94.  
  95.  
  96. Func _Shell_NotifyIconGetRect($pIdentifier, $pRect)
  97.     ;Liest die Postition eines TrayIcons aus und schreibt sie in eine RECT Struktur (http://msdn.microsoft.com/en-us/library/dd378426%28v=vs.85%29.aspx)
  98.     ;Rückgabewert: Bei Erfolg = 0, sonst HRESULT Error Code (http://blogs.msdn.com/b/eldar/archive/2007/04/03/a-lot-of-hresult-codes.aspx)
  99.     ; -Author: name22 (www.autoit.de)
  100.  
  101.     $aRet = DllCall("shell32.dll", "LONG", "Shell_NotifyIconGetRect", "PTR", $pIdentifier, "PTR", $pRect)
  102.     Return $aRet[0]
  103. EndFunc
  104.  
  105.  
  106. ; #FUNCTION# ====================================================================================================================
  107. ; Name...........: _WinAPI_ShellNotifyIcon
  108. ; Description....: Sends a message to the taskbar's status area.
  109. ; Syntax.........: _WinAPI_ShellNotifyIcon ( $iMessage, $tNOTIFYICONDATA )
  110. ; Parameters.....: $iMessage        - The variable that specifies the action to be taken. It can have one of the following values.
  111. ;
  112. ;                                     $NIM_ADD
  113. ;                                     $NIM_MODIFY
  114. ;                                     $NIM_DELETE
  115. ;                                     $NIM_SETFOCUS
  116. ;                                     $NIM_SETVERSION
  117. ;
  118. ;                  $tNOTIFYICONDATA - $tagNOTIFYICONDATA structure. The content and size of this structure depends on the value
  119. ;                                     of the $iMessage and version of the operating system.
  120. ; Return values..: Success          - 1.
  121. ;                  Failure          - 0 and sets the @error flag to non-zero.
  122. ; Author.........: Yashied
  123. ; Modified.......:
  124. ; Remarks........: None
  125. ; Related........:
  126. ; Link...........: @@MsdnLink@@ Shell_NotifyIcon
  127. ; Example........: Yes
  128. ; ===============================================================================================================================
  129.  
  130. Func _WinAPI_ShellNotifyIcon($iMessage, $tNOTIFYICONDATA)
  131.  
  132.     Local $Ret = DllCall('shell32.dll', 'int', 'Shell_NotifyIconW', 'dword', $iMessage, 'ptr', DllStructGetPtr($tNOTIFYICONDATA))
  133.  
  134.     If (@error) Or (Not $Ret[0]) Then
  135.         Return SetError(1, 0, 0)
  136.     EndIf
  137.     Return 1
  138. EndFunc   ;==>_WinAPI_ShellNotifyIcon
  139.  
  140.  
  141. ; #FUNCTION# ====================================================================================================================
  142. ; Name...........: _WinAPI_ExtractIcon
  143. ; Description....: Extracts an icon from the specified executable file, DLL, or icon file.
  144. ; Syntax.........: _WinAPI_ExtractIcon ( $sIcon, $iIndex [, $fSmall] )
  145. ; Parameters.....: $sIcon  - The name of an executable file, DLL, or icon file from which icons will be extracted.
  146. ;                  $iIndex - The zero-based index of the icon to extract. If this value is a negative number, the function extracts
  147. ;                            the icon whose resource identifier is equal to the absolute value of $iIndex.
  148. ;                  $fSmall - Specifies whether extract a small icon, valid values:
  149. ;                  |TRUE   - Extract a small icon.
  150. ;                  |FALSE  - Extract a large icon. (Default)
  151. ; Return values..: Success - Handle to the extracted icon.
  152. ;                  Failure - 0 and sets the @error flag to non-zero.
  153. ; Author.........: Yashied
  154. ; Modified.......:
  155. ; Remarks........: When you are finished using the icon, destroy it using the _WinAPI_DestroyIcon() function.
  156. ; Related........:
  157. ; Link...........: @@MsdnLink@@ ExtractIconEx
  158. ; Example........: Yes
  159. ; ===============================================================================================================================
  160.  
  161. Func _WinAPI_ExtractIcon($sIcon, $iIndex, $fSmall = 0)
  162.  
  163.     Local $pLarge, $pSmall, $tPtr = DllStructCreate('ptr')
  164.  
  165.     If $fSmall Then
  166.         $pLarge = 0
  167.         $pSmall = DllStructGetPtr($tPtr)
  168.     Else
  169.         $pLarge = DllStructGetPtr($tPtr)
  170.         $pSmall = 0
  171.     Endif
  172.  
  173.     Local $Ret = DllCall('shell32.dll', 'uint', 'ExtractIconExW', 'wstr', $sIcon, 'int', $iIndex, 'ptr', $pLarge, 'ptr', $pSmall, 'uint', 1)
  174.  
  175.     If (@error) Or (Not $Ret[0]) Then
  176.         Return SetError(1, 0, 0)
  177.     EndIf
  178.     Return DllStructGetData($tPtr, 1)
  179. EndFunc   ;==>_WinAPI_ExtractIcon
RAW Paste Data