Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,942 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,418 people online right now. Registration is fast and FREE... Join Now!




Setting the image of a tray icon with the Win32 API

 
Reply to this topicStart new topic

Setting the image of a tray icon with the Win32 API

RodgerB
24 Jan, 2008 - 03:58 AM
Post #1

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,166



Thanked: 17 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
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! sad.gif

Here is the code I am using:

CODE

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! tongue.gif

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

Thanks in advance! smile.gif

This post has been edited by RodgerB: 24 Jan, 2008 - 04:22 AM
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Setting The Image Of A Tray Icon With The Win32 API
24 Jan, 2008 - 05:12 AM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,143



Thanked: 77 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
....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 =-)

CODE

@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

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Setting The Image Of A Tray Icon With The Win32 API
24 Jan, 2008 - 05:41 AM
Post #3

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Setting The Image Of A Tray Icon With The Win32 API
24 Jan, 2008 - 11:52 AM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
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
User is offlineProfile CardPM
+Quote Post

RodgerB
RE: Setting The Image Of A Tray Icon With The Win32 API
24 Jan, 2008 - 02:42 PM
Post #5

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,166



Thanked: 17 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
QUOTE(PsychoCoder @ 25 Jan, 2008 - 12: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


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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 04:46PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month