Shell_NotifyIcon P/Invoke

E

Ed Courtenay

I need to be able to set the icon on a Notification Icon Balloon Tip (a
balloon tip raised from the system tray), and I've had limited success
so far.

I've extended the standard NotifyIcon component (well, read disassembled
using Lutz Roeder's fantastic tool and rebuilt); I've added a new
Structure like this:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class NOTIFYICONDATAEX
{
public int cbSize;
public IntPtr hWnd;
public int uID;
public int uFlags;
public int uCallbackMessage;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x80)]
public string szTip;
public int dwState;
public int dwStateMask;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x100)]
public string szInfo;
public int uTimeoutOrVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x40)]
public string szInfoTitle;
public int dwInfoFlags;
public Guid guidItem;
public IntPtr hBalloonIcon;

public NOTIFYICONDATAEX()
{
cbSize = Marshal.SizeOf(typeof (NOTIFYICONDATAEX));
hIcon = IntPtr.Zero;
hBalloonIcon = IntPtr.Zero;
}
}

and I've added an extern declaration like so:

[DllImport("shell32.dll", CharSet = CharSet.Auto,
EntryPoint="Shell_NotifyIcon")]
public static extern int Shell_NotifyIconEx(int message,
NOTIFYICONDATAEX pnid);

If I use the following to invoke Shell_NotifyIcon:

NOTIFYICONDATAEX notifyicondata1 = new NOTIFYICONDATAEX();
notifyicondata1.hWnd = window.Handle;
notifyicondata1.uID = id;
notifyicondata1.uFlags = NIF_INFO | NIF_ICON;
notifyicondata1.uTimeoutOrVersion = timeout;
notifyicondata1.szInfoTitle = tipTitle;
notifyicondata1.szInfo = tipText;
notifyicondata1.dwInfoFlags = (int)ToolTipIcon.User;
notifyicondata1.hIcon = balloonIcon.Handle;
Shell_NotifyIconEx(1, notifyicondata1);

then I raise a balloon tip with a user-supplied icon. Unfortunately, it
changes the Notification Icon as well. However, according to the docs
when using Vista you can specify hBalloonIcon independently of hIcon.
However, when I try this:

NOTIFYICONDATAEX notifyicondata1 = new NOTIFYICONDATAEX();
notifyicondata1.hWnd = window.Handle;
notifyicondata1.uID = id;
notifyicondata1.uFlags = NIF_INFO | NIF_ICON;
notifyicondata1.uTimeoutOrVersion = timeout;
notifyicondata1.szInfoTitle = tipTitle;
notifyicondata1.szInfo = tipText;
notifyicondata1.dwInfoFlags = (int)ToolTipIcon.User;
notifyicondata1.hIcon = trayIcon.Handle;
notifyicondata1.hBalloonIcon = balloonIcon.Handle;
Shell_NotifyIconEx(1, notifyicondata1);

the function returns false and no balloon tip is shown. In addition,
Win32Exception states that "The operation completed successfully". Does
anyone have any ideas?

Many thanks

Ed Courtenay
 
V

VJ

I am attaching a cs file that I wrote a while back with some help from
online fourm postings... Hope this will help what you want, and also there
is at the end of cs, a how to use section.. see that also..

This works for me well, and we use it in our applications. If have issues
let us know..!

VJ

Ed Courtenay said:
I need to be able to set the icon on a Notification Icon Balloon Tip (a
balloon tip raised from the system tray), and I've had limited success
so far.

I've extended the standard NotifyIcon component (well, read disassembled
using Lutz Roeder's fantastic tool and rebuilt); I've added a new
Structure like this:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class NOTIFYICONDATAEX
{
public int cbSize;
public IntPtr hWnd;
public int uID;
public int uFlags;
public int uCallbackMessage;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x80)]
public string szTip;
public int dwState;
public int dwStateMask;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x100)]
public string szInfo;
public int uTimeoutOrVersion;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x40)]
public string szInfoTitle;
public int dwInfoFlags;
public Guid guidItem;
public IntPtr hBalloonIcon;

public NOTIFYICONDATAEX()
{
cbSize = Marshal.SizeOf(typeof (NOTIFYICONDATAEX));
hIcon = IntPtr.Zero;
hBalloonIcon = IntPtr.Zero;
}
}

and I've added an extern declaration like so:

[DllImport("shell32.dll", CharSet = CharSet.Auto,
EntryPoint="Shell_NotifyIcon")]
public static extern int Shell_NotifyIconEx(int message,
NOTIFYICONDATAEX pnid);

If I use the following to invoke Shell_NotifyIcon:

NOTIFYICONDATAEX notifyicondata1 = new NOTIFYICONDATAEX();
notifyicondata1.hWnd = window.Handle;
notifyicondata1.uID = id;
notifyicondata1.uFlags = NIF_INFO | NIF_ICON;
notifyicondata1.uTimeoutOrVersion = timeout;
notifyicondata1.szInfoTitle = tipTitle;
notifyicondata1.szInfo = tipText;
notifyicondata1.dwInfoFlags = (int)ToolTipIcon.User;
notifyicondata1.hIcon = balloonIcon.Handle;
Shell_NotifyIconEx(1, notifyicondata1);

then I raise a balloon tip with a user-supplied icon. Unfortunately, it
changes the Notification Icon as well. However, according to the docs
when using Vista you can specify hBalloonIcon independently of hIcon.
However, when I try this:

NOTIFYICONDATAEX notifyicondata1 = new NOTIFYICONDATAEX();
notifyicondata1.hWnd = window.Handle;
notifyicondata1.uID = id;
notifyicondata1.uFlags = NIF_INFO | NIF_ICON;
notifyicondata1.uTimeoutOrVersion = timeout;
notifyicondata1.szInfoTitle = tipTitle;
notifyicondata1.szInfo = tipText;
notifyicondata1.dwInfoFlags = (int)ToolTipIcon.User;
notifyicondata1.hIcon = trayIcon.Handle;
notifyicondata1.hBalloonIcon = balloonIcon.Handle;
Shell_NotifyIconEx(1, notifyicondata1);

the function returns false and no balloon tip is shown. In addition,
Win32Exception states that "The operation completed successfully". Does
anyone have any ideas?

Many thanks

Ed Courtenay
 
E

Ed Courtenay

VJ said:
I am attaching a cs file that I wrote a while back with some help from
online fourm postings... Hope this will help what you want, and also there
is at the end of cs, a how to use section.. see that also..

This works for me well, and we use it in our applications. If have issues
let us know..!

VJ

Thanks for taking the time, but the attached code does not address the
issue I'm dealing with; namely that I wish to use Shell_Notify to raise
a balloon tip with it's own icon (i.e. not one of the standard
Windows-supplied Information, Warning or Error icons). This, as I've
stated, I've already achieved, but I can't get the function to use two
different icons - one for the Notification area and a separate one for
the ballooon, which the documentation suggests is possible by populating
both the hIcon and hBalloonIcon fields in the appropriate structure.

Any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top