Balloon Tip Custom Icon

K

Kevin Cochran

Greetings,

I am using .NET 2.0, and I would like to specify a custom icon for the
balloon tip using NotifyIcon. I've noticed in the platform SDK that a custom
icon can be specified for a balloon tip using TTM_SETTITLE, but I can't
figure out how to get it to work.

Any help is appreciated.

Thanks,
Kevin
 
J

Jeroen Mostert

Kevin said:
I am using .NET 2.0, and I would like to specify a custom icon for the
balloon tip using NotifyIcon. I've noticed in the platform SDK that a custom
icon can be specified for a balloon tip using TTM_SETTITLE, but I can't
figure out how to get it to work.
For notify icons, there's already NotifyIcon.BalloonTipIcon. For general
tooltips, you can use ToolTip.IsBalloon and ToolTip.ToolTipIcon. The latter
does require you to draw the tooltip yourself if the icon isn't a standard
icon, but arguably, this is not a bad thing, because using TTM_SETTITLE to
specify an icon is only supported from XP SP2 onward.

That said, if you don't mind saddling your users with a requirement just to
display nice graphics and you're too lazy to draw the tooltip from scratch,
this should do the trick. I'm assuming you've already got an icon and a
tooltip laying around somewhere.

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr
wParam, IntPtr lParam);

const uint TTM_SETTITLEW = 1057;

IntPtr tmpStr = null;
try {
tmpStr = Marshal.StringToHGlobalUni(toolTip.ToolTipTitle);
SendMessage(toolTip.Handle, TTM_SETTITLEW, icon.Handle, tmpStr);
} finally {
Marshal.FreeHGlobal(tmpStr);
}

Warning: untested.
 
K

Kevin Cochran

Hi Jeroen,

Thanks for the advice. Oh, I'm lazy for sure, but in this case, I think I'd
prefer to draw the icon myself. It's just what's needed in this case. My app
is build around the NotifyIcon class because it's windowless, and the
NotifyIcon.BalloonTipIcon does not appear to let you do custom icons, you can
only choose from one of four predefined ones.

That said, how can I use the ToolTip class in place of the built-in one that
NotifyIcon uses?

Thanks again,
Kevin

Jeroen Mostert said:
Kevin said:
I am using .NET 2.0, and I would like to specify a custom icon for the
balloon tip using NotifyIcon. I've noticed in the platform SDK that a custom
icon can be specified for a balloon tip using TTM_SETTITLE, but I can't
figure out how to get it to work.
For notify icons, there's already NotifyIcon.BalloonTipIcon. For general
tooltips, you can use ToolTip.IsBalloon and ToolTip.ToolTipIcon. The latter
does require you to draw the tooltip yourself if the icon isn't a standard
icon, but arguably, this is not a bad thing, because using TTM_SETTITLE to
specify an icon is only supported from XP SP2 onward.

That said, if you don't mind saddling your users with a requirement just to
display nice graphics and you're too lazy to draw the tooltip from scratch,
this should do the trick. I'm assuming you've already got an icon and a
tooltip laying around somewhere.

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr
wParam, IntPtr lParam);

const uint TTM_SETTITLEW = 1057;

IntPtr tmpStr = null;
try {
tmpStr = Marshal.StringToHGlobalUni(toolTip.ToolTipTitle);
SendMessage(toolTip.Handle, TTM_SETTITLEW, icon.Handle, tmpStr);
} finally {
Marshal.FreeHGlobal(tmpStr);
}

Warning: untested.
 
J

Jeroen Mostert

Kevin said:
Jeroen Mostert said:
Kevin said:
I am using .NET 2.0, and I would like to specify a custom icon for the
balloon tip using NotifyIcon. I've noticed in the platform SDK that a custom
icon can be specified for a balloon tip using TTM_SETTITLE, but I can't
figure out how to get it to work.
For notify icons, there's already NotifyIcon.BalloonTipIcon. For general
tooltips, you can use ToolTip.IsBalloon and ToolTip.ToolTipIcon. The latter
does require you to draw the tooltip yourself if the icon isn't a standard
icon, but arguably, this is not a bad thing, because using TTM_SETTITLE to
specify an icon is only supported from XP SP2 onward.

That said, if you don't mind saddling your users with a requirement just to
display nice graphics and you're too lazy to draw the tooltip from scratch,
this should do the trick. I'm assuming you've already got an icon and a
tooltip laying around somewhere.

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr
wParam, IntPtr lParam);

const uint TTM_SETTITLEW = 1057;

IntPtr tmpStr = null;
try {
tmpStr = Marshal.StringToHGlobalUni(toolTip.ToolTipTitle);
SendMessage(toolTip.Handle, TTM_SETTITLEW, icon.Handle, tmpStr);
} finally {
Marshal.FreeHGlobal(tmpStr);
}

Warning: untested.
Thanks for the advice. Oh, I'm lazy for sure, but in this case, I think I'd
prefer to draw the icon myself. It's just what's needed in this case. My app
is build around the NotifyIcon class because it's windowless, and the
NotifyIcon.BalloonTipIcon does not appear to let you do custom icons, you can
only choose from one of four predefined ones.

That said, how can I use the ToolTip class in place of the built-in one that
NotifyIcon uses?
Silly me, I didn't notice that NotifyIcon.BalloonTipIcon is *also*
restricted to the default icons! Now look who's lazy.

Alright, yes, getting NotifyIcon to do a custom icon happens to be a *lot*
harder. The shell unfortunately does not use the tool tip control for notify
icons, so TTM_SETTITLE will not avail you here (and custom drawing is not
possible, the best you can do is a custom icon). NotifyIcon does not expose
its notification window handle or id, so you cannot call Shell_NotifyIcon
yourself either. Finally, you cannot determine the position of a notify
icon, so you cannot draw the balloon tip yourself either!

When do you need the balloon tip to appear? If it has to occur as a result
of the user clicking, you can still make it work by handling the MouseClick
event and drawing around that position. Here's a post where that solution is
used: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2551783&SiteID=1

If you really need the balloon tip to appear with a custom icon when you
want it, not when the user does something, then start reading up on
Shell_NotifyIcon and good luck writing your own class...
 

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

Similar Threads

system tray 1
Custom Balloon Tip? 0
Balloon Notifications 1
Tray icon balloon 5
NotifyIcon Balloon Tips 2
Hide Balloon Tip 2
NotifyIcon ShowBalloonTip not appearing 1
Hide balloon tip icon 2

Top