VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Page 1 of 1
  • You cannot start a new topic
  • Reply Reply

Setting the image of a tray icon with the Win32 API Rate Topic: -----

#1 RodgerB  Icon User is offline

  • D.I.C Lover
  • Icon

Reputation: 42
  • View blog
  • Posts: 2,246
  • Joined: 21-September 07


Dream Kudos: 2200

Expert In: Dot Net Technologies

Share |

Setting the image of a tray icon with the Win32 API

Post icon  Posted 24 January 2008 - 03:58 AM

I know a lot of people here are experienced with the Win32 API here, and I came up with the idea of being able to hide and show tray icons, programmatically.

I've been fiddling with the Win32 API recently, and I found it very insightful as to the operations involved 'behind the scenes'. I am using the Shell_NotifyIconA function (a member of shell32), and am failing to remove or modify information.

I want to be able to hide and show an icon, so I thought of the idea of nulling the icon property, so the icon would still be there, but hidden, and then setting the icon back to it's previous state. However, the function doesn't seem to want to work, it always seems to want to return 0! :(

Here is the code I am using:

Imports System.Runtime.InteropServices

Public Class Form1

	' The NOTIFYICONDATA Struct
	Private Structure NOTIFYICONDATA
		Public cbSize As Long
		Public hWnd As Long
		Public uId As Long
		Public uFlags As Long
		Public uCallBackMessage As Long
		Public hIcon As Long
		<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=128)> _
		Public szTip As String
	End Structure

	' The messages to send to the function
	Private Const NIM_ADD As Int32 = &H0
	Private Const NIM_MODIFY As Int32 = &H1
	Private Const NIM_DELETE As Int32 = &H2

	' The variable we will be using to set the icon properties.
	Private pIcon As New NOTIFYICONDATA

	' The API Function we will be using.
	Private Declare Auto Function Shell_NotifyIconA Lib "shell32" (ByVal dwMessage As Integer, _
	ByRef pnid As NOTIFYICONDATA) As Integer

	' Null the MSN Icon (for test purposes, the only tray icon on my comp atm :P)
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		pIcon.hWnd = 5444
		pIcon.hIcon = IntPtr.Zero
		Dim res As Int32 = Shell_NotifyIconA(NIM_MODIFY, pIcon)
		' It seems to <3 returning zero :S.
		MessageBox.Show(res)
	End Sub

End Class



Any guidance or help would be appreciated, and before suspision arises I am not planning on using this to circumvent security, rather learn about the process involved for self-improvement.

EDIT: OPPS! I was using the process ID instead of a window handle. I'm so tired. LOL! I will test when I'm done using the correct parameters! :P

EDIT 2: Damn, thought I was on to something, that doesn't work either. :)

Thanks in advance! :)

This post has been edited by RodgerB: 24 January 2008 - 04:22 AM

Was This Post Helpful? 0
  • +
  • -


#2 no2pencil  Icon User is online

  • DIGI-TAL-HARD-CORE
  • Icon

Reputation: 816
  • View blog
  • Posts: 17,399
  • Joined: 10-May 07


Dream Kudos: 2925

Expert In: Goofing Off

Re: Setting the image of a tray icon with the Win32 API

Posted 24 January 2008 - 05:12 AM

....ok, I know that this is a VB.NET post, so don't get all fussy.

This is some 32bit asm code that I wrote for a program that *among other things* changes the icon in the tray, based on if the program is live (IconL) or silent (IconS). The API calls are the same, so maybe someone here that knows both asm & .net can make this work for you.

It's been... 4 (ish?) years since I've touched the code, so that is what you get =-)

@IconL:
			mov		IconState,0
			mov 	note.cbSize,sizeof NOTIFYICONDATA
			push 	hWnd
			pop 	note.hwnd
			mov 	note.uID,IDI_TRAY
			mov 	note.uFlags,NIF_ICON+NIF_MESSAGE+NIF_TIP
			mov 	note.uCallbackMessage,WM_SHELLNOTIFY
			push 	offset IconLarge
			push 	hInstance
			call 	LoadIcon

			mov	  note.hIcon,eax
			push 	OFFSET AppName
			push 	OFFSET note.szTip
			call 	lstrcpy

			push 	SW_HIDE
			push 	hWnd
			call 	ShowWindow

			push 	OFFSET note
			push 	NIM_ADD
			call 	Shell_NotifyIcon
			mov		esi,Lret
			mov		Lret,0
			cmp		esi,2
			je		@Lret2
			ret
@IconS:
			mov		IconState,1
			mov 	note.cbSize,sizeof NOTIFYICONDATA
			push 	hWnd
			pop 	note.hwnd
			mov 	note.uID,IDI_TRAY
			mov 	note.uFlags,NIF_ICON+NIF_MESSAGE+NIF_TIP
			mov 	note.uCallbackMessage,WM_SHELLNOTIFY
			
			push 	offset IconSmall
			push 	hInstance
			call 	LoadIcon
			
			mov	  note.hIcon,eax
			push 	OFFSET AppName
			push 	OFFSET note.szTip
			call 	lstrcpy

			push 	SW_HIDE
			push 	hWnd
			call 	ShowWindow
			
			push 	OFFSET note
			push 	NIM_ADD
			call 	Shell_NotifyIcon
			mov		esi,Sret
			mov		Sret,0
			cmp		esi,2
			je		@Sret2
			ret


Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • iHater.Init(this);
  • Icon

Reputation: 1364
  • View blog
  • Posts: 19,696
  • Joined: 26-July 07


Dream Kudos: 12925

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

Re: Setting the image of a tray icon with the Win32 API

Posted 24 January 2008 - 05:41 AM

My first and only question: If you're programming in 2.0 why not just use the notify icon buiot into the framework, no Win32 calls or anything
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • Icon

Reputation: 345
  • View blog
  • Posts: 9,235
  • Joined: 26-December 05


Dream Kudos: 500

Expert In: Everything

Re: Setting the image of a tray icon with the Win32 API

Posted 24 January 2008 - 11:52 AM

I would have to agree with PsychoCoder. In .net there is rarely a need to use the WIN API, as everything can be accessed using .NET classes.

The NotifyIcon has a Visible property much like every other control in .NET.

MSDN NotifyIcon
Was This Post Helpful? 0
  • +
  • -

#5 RodgerB  Icon User is offline

  • D.I.C Lover
  • Icon

Reputation: 42
  • View blog
  • Posts: 2,246
  • Joined: 21-September 07


Dream Kudos: 2200

Expert In: Dot Net Technologies

Re: Setting the image of a tray icon with the Win32 API

Posted 24 January 2008 - 02:42 PM

View PostPsychoCoder, on 25 Jan, 2008 - 12:41 AM, said:

My first and only question: If you're programming in 2.0 why not just use the notify icon buiot into the framework, no Win32 calls or anything


Because I want to be able to modify a tray icon of another application; not my own, hence why I wouldn't be using the .NET 2.0 NotifyIcon control.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1
  • You cannot start a new topic
  • Reply Reply


Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users