Display popup messages from system tray

elloco999

Well-known member
Joined
Dec 21, 2004
Messages
49
Programming Experience
5-10
Hi,

I've written an app that monitors the connection to one or more SQL servers. It gives a warning message when the connection is down. I use a standard messagebox to display the message, but I would like to use a popup message instead. (like when you plug in a USB memorystick, windows shows you a found new hardware message in a text balloon from the system tray.)

Can I use a tooltip to do this? If so, how can I show the tooltip when I want to, instead of when the user hovers the mouse over the notifyicon? Or is there a special class for this?

Greets,
El Loco
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner

elloco999

Well-known member
Joined
Dec 21, 2004
Messages
49
Programming Experience
5-10
Thanks for the info, butthis isn't exactly what I'm looking for.Unless I can change the look of this form to look like the win XP balloons... I did find a project that dows what I want, but it is in VB 6. I let VS convert it to .NET but it still uses a lot of VB 6 code.

http://www.visualbasicforum.com/showthread.php?t=188103&highlight=systray+message

There are so many application that use these text balloons, there just has to be an easier way to do this. I doubt very much that every application developer that uses these balloons has had to use an approach like this. Especially since all these text balloons look the same (same balloon, same color, same close button, same font etc) and I've heard the balloons change when you change your desktop theme. So this has to be an build in function in windows that you can call from your code...

Hopefully someone can tell me how to use the windows balloons, in the meantime I'll use this construction for my popups. Thanks again!

Greets,
El Loco
 
Last edited:

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
if you download the vb source from the link in paszt's post, it says right there that it supports skinning of which you can easily have the winxp style in that popup

you'd have to make the skin of course, but it's not that hard
 

elloco999

Well-known member
Joined
Dec 21, 2004
Messages
49
Programming Experience
5-10
Well, I guess I could make a skin... But if you'd have seen some of my previous graphical work you wouldn't say it would look anything like the win xp style! ;)
I'll ask a friend to make the skin, he's much better at that then me.

But still, the popup won't change when you change the XP theme... I still believe there has to be some built in functionality in windows you can use for this... And I would like to know how.
But like I said, Until I find out (if I find out :) ) I will probably use this.

Greets,
El Loco
 

elloco999

Well-known member
Joined
Dec 21, 2004
Messages
49
Programming Experience
5-10
Well, I found some C# code on msdn.microsoft.com that should do this. Normally converting C# code to VB code isn't to much of a problem for me, but this has got me puzzled. I think it's because of the struct used for NOTIFYICONDATA. Can anybody help me with this?

Here's the C# code:
VB.NET:
typedef struct _NOTIFYICONDATA {
	DWORD cbSize;
	HWND hWnd;
	UINT uID;
	UINT uFlags;
	UINT uCallbackMessage;
	HICON hIcon;
	TCHAR szTip[64];
	DWORD dwState;
	DWORD dwStateMask;
	TCHAR szInfo[256];
	union {
		UINT uTimeout;
		UINT uVersion;
	};
	TCHAR szInfoTitle[64];
	DWORD dwInfoFlags;
	GUID guidItem;
} NOTIFYICONDATA, *PNOTIFYICONDATA;
 
#define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))
 
NOTIFYICONDATA IconData = {0};
 
IconData.cbSize = SIZEOF(IconData);
IconData.hWnd = hwndNI;
IconData.uFlags = NIF_INFO;
 
HRESULT hr = StringCchCopy(IconData.szInfo, ARRAYSIZE(IconData.szInfo), TEXT("Your message text goes here."));
if(FAILED(hr))
{
// TODO: Write an error handler in case the call to StringCchCopy fails.
}
IconData.uTimeout = 15000; // in milliseconds
 
Shell_NotifyIcon(NIM_MODIFY, &IconData);
The link is here:
http://msdn.microsoft.com/library/en-us/shellcc/platform/commctls/tooltip/usingtooltips.asp?frame=true#tooltip_sample_balloon
Click on the "balloon tooltips" link to go directly to the part where I got my code.

If anyone can tell me how to use this struct thing in VB (and where to place it in my code) my biggest hurdle is overcome.

Greets,
El Loco
 
Last edited:

hagipmc

New member
Joined
Mar 15, 2005
Messages
1
Programming Experience
1-3
Try this!!!

VB.NET:
Option Strict Off 
Option Explicit On 
 
Friend Class Form1
 
Inherits System.Windows.Forms.Form
 
#Region "Windows Form Designer generated code "
 
Public Sub New()
 
MyBase.New()
 
'This call is required by the Windows Form Designer.
 
InitializeComponent()
 
End Sub
 
'Form overrides dispose to clean up the component list.
 
Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
 
If Disposing Then
 
If Not components Is Nothing Then
 
components.Dispose()
 
End If
 
End If
 
MyBase.Dispose(Disposing)
 
End Sub
 
'Required by the Windows Form Designer
 
Private components As System.ComponentModel.IContainer
 
Public ToolTip1 As System.Windows.Forms.ToolTip
 
Public WithEvents Text2 As System.Windows.Forms.TextBox
 
Public WithEvents Command1 As System.Windows.Forms.Button
 
Public WithEvents Text1 As System.Windows.Forms.TextBox
 
Public WithEvents Label6 As System.Windows.Forms.Label
 
Public WithEvents Label4 As System.Windows.Forms.Label
 
Public WithEvents Label5 As System.Windows.Forms.Label
 
Public WithEvents Label2 As System.Windows.Forms.Label
 
Public WithEvents Picture1 As System.Windows.Forms.Panel
 
Public WithEvents Image1 As System.Windows.Forms.PictureBox
 
Public WithEvents Frame1 As System.Windows.Forms.Panel
 
Public WithEvents Label3 As System.Windows.Forms.Label
 
Public WithEvents Label1 As System.Windows.Forms.Label
 
'NOTE: The following procedure is required by the Windows Form Designer
 
'It can be modified using the Windows Form Designer.
 
'Do not modify it using the code editor.
 
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
 
Me.components = New System.ComponentModel.Container
 
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
 
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
 
Me.Text2 = New System.Windows.Forms.TextBox
 
Me.Command1 = New System.Windows.Forms.Button
 
Me.Text1 = New System.Windows.Forms.TextBox
 
Me.Picture1 = New System.Windows.Forms.Panel
 
Me.Label6 = New System.Windows.Forms.Label
 
Me.Label4 = New System.Windows.Forms.Label
 
Me.Label5 = New System.Windows.Forms.Label
 
Me.Label2 = New System.Windows.Forms.Label
 
Me.Frame1 = New System.Windows.Forms.Panel
 
Me.Image1 = New System.Windows.Forms.PictureBox
 
Me.Label3 = New System.Windows.Forms.Label
 
Me.Label1 = New System.Windows.Forms.Label
 
Me.Picture1.SuspendLayout()
 
Me.Frame1.SuspendLayout()
 
Me.SuspendLayout()
 
'
 
'Text2
 
'
 
Me.Text2.AcceptsReturn = True
 
Me.Text2.AutoSize = False
 
Me.Text2.BackColor = System.Drawing.SystemColors.Window
 
Me.Text2.Cursor = System.Windows.Forms.Cursors.IBeam
 
Me.Text2.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.Text2.ForeColor = System.Drawing.SystemColors.WindowText
 
Me.Text2.Location = New System.Drawing.Point(3, 175)
 
Me.Text2.MaxLength = 0
 
Me.Text2.Name = "Text2"
 
Me.Text2.RightToLeft = System.Windows.Forms.RightToLeft.No
 
Me.Text2.Size = New System.Drawing.Size(333, 25)
 
Me.Text2.TabIndex = 10
 
Me.Text2.Text = "Put your mouse on me"
 
'
 
'Command1
 
'
 
Me.Command1.BackColor = System.Drawing.SystemColors.Control
 
Me.Command1.Cursor = System.Windows.Forms.Cursors.Default
 
Me.Command1.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.Command1.ForeColor = System.Drawing.SystemColors.ControlText
 
Me.Command1.Location = New System.Drawing.Point(224, 64)
 
Me.Command1.Name = "Command1"
 
Me.Command1.RightToLeft = System.Windows.Forms.RightToLeft.No
 
Me.Command1.Size = New System.Drawing.Size(108, 23)
 
Me.Command1.TabIndex = 8
 
Me.Command1.Text = "Test Password"
 
'
 
'Text1
 
'
 
Me.Text1.AcceptsReturn = True
 
Me.Text1.AutoSize = False
 
Me.Text1.BackColor = System.Drawing.SystemColors.Window
 
Me.Text1.Cursor = System.Windows.Forms.Cursors.IBeam
 
Me.Text1.Font = New System.Drawing.Font("Courier New", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.Text1.ForeColor = System.Drawing.SystemColors.WindowText
 
Me.Text1.Location = New System.Drawing.Point(78, 64)
 
Me.Text1.MaxLength = 0
 
Me.Text1.Name = "Text1"
 
Me.Text1.PasswordChar = Microsoft.VisualBasic.ChrW(42)
 
Me.Text1.RightToLeft = System.Windows.Forms.RightToLeft.No
 
Me.Text1.Size = New System.Drawing.Size(139, 22)
 
Me.Text1.TabIndex = 2
 
Me.Text1.Text = ""
 
'
 
'Picture1
 
'
 
Me.Picture1.BackColor = System.Drawing.Color.White
 
Me.Picture1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
 
Me.Picture1.Controls.Add(Me.Label6)
 
Me.Picture1.Controls.Add(Me.Label4)
 
Me.Picture1.Controls.Add(Me.Label5)
 
Me.Picture1.Controls.Add(Me.Label2)
 
Me.Picture1.Cursor = System.Windows.Forms.Cursors.Default
 
Me.Picture1.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.Picture1.ForeColor = System.Drawing.SystemColors.ControlText
 
Me.Picture1.Location = New System.Drawing.Point(2, 207)
 
Me.Picture1.Name = "Picture1"
 
Me.Picture1.RightToLeft = System.Windows.Forms.RightToLeft.No
 
Me.Picture1.Size = New System.Drawing.Size(507, 116)
 
Me.Picture1.TabIndex = 1
 
Me.Picture1.TabStop = True
 
'
 
'Label6
 
'
 
Me.Label6.BackColor = System.Drawing.Color.Transparent
 
Me.Label6.Cursor = System.Windows.Forms.Cursors.Default
 
Me.Label6.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.Label6.ForeColor = System.Drawing.SystemColors.ControlText
 
Me.Label6.Location = New System.Drawing.Point(4, 56)
 
Me.Label6.Name = "Label6"
 
Me.Label6.RightToLeft = System.Windows.Forms.RightToLeft.No
 
Me.Label6.Size = New System.Drawing.Size(122, 17)
 
Me.Label6.TabIndex = 7
 
Me.Label6.Text = "API"
 
'
 
'Label4
 
'
 
Me.Label4.BackColor = System.Drawing.Color.Transparent
 
Me.Label4.Cursor = System.Windows.Forms.Cursors.Default
 
Me.Label4.Font = New System.Drawing.Font("Courier New", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.Label4.ForeColor = System.Drawing.Color.FromArgb(CType(128, Byte), CType(0, Byte), CType(0, Byte))
 
Me.Label4.Location = New System.Drawing.Point(4, 74)
 
Me.Label4.Name = "Label4"
 
Me.Label4.RightToLeft = System.Windows.Forms.RightToLeft.No
 
Me.Label4.Size = New System.Drawing.Size(498, 53)
 
Me.Label4.TabIndex = 6
 
Me.Label4.Text = "ClientToScreen, CreateWindowEx, DestroyWindow, GetKeyState, InitCommonControls, S" & _
 
"endMessage, SendMessageLong"
 
'
 
'Label5
 
'
 
Me.Label5.BackColor = System.Drawing.Color.Transparent
 
Me.Label5.Cursor = System.Windows.Forms.Cursors.Default
 
Me.Label5.Font = New System.Drawing.Font("Courier New", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.Label5.ForeColor = System.Drawing.Color.Blue
 
Me.Label5.Location = New System.Drawing.Point(4, 21)
 
Me.Label5.Name = "Label5"
 
Me.Label5.RightToLeft = System.Windows.Forms.RightToLeft.No
 
Me.Label5.Size = New System.Drawing.Size(498, 41)
 
Me.Label5.TabIndex = 4
 
Me.Label5.Text = "This demo will show you how to create a balloon style tooltip using API. For more" & _
 
" information please check our API Dictionary."
 
'
 
'Label2
 
'
 
Me.Label2.BackColor = System.Drawing.Color.Transparent
 
Me.Label2.Cursor = System.Windows.Forms.Cursors.Default
 
Me.Label2.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.Label2.ForeColor = System.Drawing.SystemColors.ControlText
 
Me.Label2.Location = New System.Drawing.Point(4, 3)
 
Me.Label2.Name = "Label2"
 
Me.Label2.RightToLeft = System.Windows.Forms.RightToLeft.No
 
Me.Label2.Size = New System.Drawing.Size(122, 17)
 
Me.Label2.TabIndex = 5
 
Me.Label2.Text = "Example Description"
 
'
 
'Frame1
 
'
 
Me.Frame1.BackColor = System.Drawing.Color.Black
 
Me.Frame1.Controls.Add(Me.Image1)
 
Me.Frame1.Cursor = System.Windows.Forms.Cursors.Default
 
Me.Frame1.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.Frame1.ForeColor = System.Drawing.SystemColors.WindowText
 
Me.Frame1.Location = New System.Drawing.Point(0, 0)
 
Me.Frame1.Name = "Frame1"
 
Me.Frame1.RightToLeft = System.Windows.Forms.RightToLeft.No
 
Me.Frame1.Size = New System.Drawing.Size(667, 49)
 
Me.Frame1.TabIndex = 0
 
'
 
'Image1
 
'
 
Me.Image1.Cursor = System.Windows.Forms.Cursors.Default
 
Me.Image1.Image = CType(resources.GetObject("Image1.Image"), System.Drawing.Image)
 
Me.Image1.Location = New System.Drawing.Point(8, 8)
 
Me.Image1.Name = "Image1"
 
Me.Image1.Size = New System.Drawing.Size(276, 33)
 
Me.Image1.TabIndex = 0
 
Me.Image1.TabStop = False
 
'
 
'Label3
 
'
 
Me.Label3.BackColor = System.Drawing.SystemColors.Control
 
Me.Label3.Cursor = System.Windows.Forms.Cursors.Default
 
Me.Label3.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.Label3.ForeColor = System.Drawing.Color.FromArgb(CType(128, Byte), CType(0, Byte), CType(0, Byte))
 
Me.Label3.Location = New System.Drawing.Point(6, 89)
 
Me.Label3.Name = "Label3"
 
Me.Label3.RightToLeft = System.Windows.Forms.RightToLeft.No
 
Me.Label3.Size = New System.Drawing.Size(322, 30)
 
Me.Label3.TabIndex = 9
 
Me.Label3.Text = "To test on demand warning balloon tool tip turn CAPS LOCK on"
 
'
 
'Label1
 
'
 
Me.Label1.BackColor = System.Drawing.SystemColors.Control
 
Me.Label1.Cursor = System.Windows.Forms.Cursors.Default
 
Me.Label1.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.Label1.ForeColor = System.Drawing.SystemColors.ControlText
 
Me.Label1.Location = New System.Drawing.Point(8, 69)
 
Me.Label1.Name = "Label1"
 
Me.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No
 
Me.Label1.Size = New System.Drawing.Size(64, 19)
 
Me.Label1.TabIndex = 3
 
Me.Label1.Text = "Password"
 
'
 
'Form1
 
'
 
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
 
Me.BackColor = System.Drawing.SystemColors.Control
 
Me.ClientSize = New System.Drawing.Size(511, 325)
 
Me.Controls.Add(Me.Text2)
 
Me.Controls.Add(Me.Command1)
 
Me.Controls.Add(Me.Text1)
 
Me.Controls.Add(Me.Picture1)
 
Me.Controls.Add(Me.Frame1)
 
Me.Controls.Add(Me.Label3)
 
Me.Controls.Add(Me.Label1)
 
Me.Cursor = System.Windows.Forms.Cursors.Default
 
Me.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
 
Me.ForeColor = System.Drawing.Color.Black
 
Me.Location = New System.Drawing.Point(4, 23)
 
Me.Name = "Form1"
 
Me.RightToLeft = System.Windows.Forms.RightToLeft.No
 
Me.Text = "Ballon Style ToolTip Demo"
 
Me.Picture1.ResumeLayout(False)
 
Me.Frame1.ResumeLayout(False)
 
Me.ResumeLayout(False)
 
End Sub
 
#End Region
 
Dim TT1 As New CBalloonToolTip '//Demo for On Demand tooltip
 
Dim TT2 As New CBalloonToolTip '//Demo for mouse over tooltip
 
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Integer) As Short
 
Public Function CapsLockOn() As Boolean
 
Dim iKeyState As Short
 
iKeyState = GetKeyState(System.Windows.Forms.Keys.Capital)
 
CapsLockOn = (iKeyState = 1 Or iKeyState = -127)
 
End Function
 
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
 
If Text1.Text <> "mypassword" Then
 
TT1.Style = CBalloonToolTip.ttStyleEnum.TTBalloon
 
TT1.Icon = CBalloonToolTip.ttIconType.TTIconError
 
TT1.Title = IIf(Text1.Text = "", "Blank Password", "Invalid Password")
 
TT1.TipText = "Please Enter Correct Password..."
 
TT1.PopupOnDemand = True
 
TT1.CreateToolTip(Text1.Handle.ToInt32)
 
'TT1.Show(0, VB6.PixelsToTwipsY(Text1.Height) / VB6.TwipsPerPixelX - 1, Text1.Handle.ToInt32) '//In Pixel only
 
TT1.Show(0, Text1.Height - 1, Text1.Handle.ToInt32) '//In Pixel only
 
End If
 
End Sub
 
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
 
TT2.Style = CBalloonToolTip.ttStyleEnum.TTBalloon
 
TT2.Icon = CBalloonToolTip.ttIconType.TTIconInfo
 
TT2.Title = "Information"
 
TT2.TipText = "tooltip with kool color and font for Textbox2"
 
TT2.PopupOnDemand = False
 
TT2.ForeColor = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.White)
 
TT2.BackColor = &HCE7110
 
TT2.VisibleTime = 6000 'After 6 Seconds tooltip will go away
 
Dim myFont As New Font("Tahoma", 10, FontStyle.Italic Or FontStyle.Underline)
 
TT2.TipFont = myFont
 
TT2.CreateToolTip(Text2.Handle.ToInt32)
 
End Sub
 
Private Sub Form1_MouseDown(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
 
Dim Button As Short = eventArgs.Button \ &H100000
 
Dim Shift As Short = System.Windows.Forms.Control.ModifierKeys \ &H10000
 
TT1.Destroy()
 
End Sub
 
Private Sub Form1_Paint(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
 
TT1.Destroy()
 
End Sub
 
Private Sub Form1_Resize(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Resize
 
TT1.Destroy()
 
End Sub
 
Private Sub Form1_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed
 
TT1.Destroy()
 
TT2.Destroy()
 
End Sub
 
Private Sub Text1_KeyDown(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyEventArgs) Handles Text1.KeyDown
 
Dim KeyCode As Short = eventArgs.KeyCode
 
Dim Shift As Short = eventArgs.KeyData \ &H10000
 
If CapsLockOn() Then
 
TT1.Style = CBalloonToolTip.ttStyleEnum.TTBalloon
 
TT1.Icon = CBalloonToolTip.ttIconType.TTIconWarning
 
TT1.Title = "Caps Lock is on"
 
TT1.TipText = "Check your caps lock key..."
 
TT1.CreateToolTip(Text1.Handle.ToInt32)
 
'TT1.Show(0, VB6.PixelsToTwipsY(Text1.Height) / VB6.TwipsPerPixelX - 1, Text1.Handle.ToInt32)
 
TT1.Show(0, Text1.Height - 1, Text1.Handle.ToInt32)
 
Else
 
TT1.Destroy()
 
End If
 
End Sub
 
Private Sub Text1_MouseDown(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.MouseEventArgs) Handles Text1.MouseDown
 
Dim Button As Short = eventArgs.Button \ &H100000
 
Dim Shift As Short = System.Windows.Forms.Control.ModifierKeys \ &H10000
 
TT1.Destroy()
 
End Sub
 
End Class






VB.NET:
Option[size=2][color=#0000ff]Strict[/color][/size][size=2][color=#0000ff]Off
 
Option[/color][/size][size=2][color=#0000ff]Explicit[/color][/size][size=2][color=#0000ff]On[/color][/size]
 
Public Class CBalloonToolTip 
Private Declare Sub InitCommonControls Lib "comctl32.dll" ()
 
Private Declare Function CreateWindowEx Lib "user32.dll" Alias "CreateWindowExA" ( _
 
ByVal dwExStyle As Integer, _
 
ByVal lpClassName As String, _
 
ByVal lpWindowName As String, _
 
ByVal dwStyle As Integer, _
 
ByVal X As Integer, _
 
ByVal Y As Integer, _
 
ByVal nWidth As Integer, _
 
ByVal nHeight As Integer, _
 
ByVal hWndParent As Integer, _
 
ByVal hMenu As Integer, _
 
ByVal hInstance As Integer, _
 
ByRef lpParam As Integer) As Integer
 
Private Declare Function SendMessageBT Lib "user32.dll" Alias "SendMessageA" ( _
 
ByVal hwnd As Integer, _
 
ByVal wMsg As Integer, _
 
ByVal wParam As Integer, _
 
ByRef lParam As TOOLINFO) As Integer
 
Private Declare Function SendMessageStr Lib "user32.dll" Alias "SendMessageA" ( _
 
ByVal hwnd As Integer, _
 
ByVal wMsg As Integer, _
 
ByVal wParam As Integer, _
 
ByVal lParam As String) As Integer
 
Private Declare Function SendMessageLong Lib "user32.dll" Alias "SendMessageA" ( _
 
ByVal hwnd As Integer, _
 
ByVal wMsg As Integer, _
 
ByVal wParam As Integer, _
 
ByVal lParam As Integer) As Integer
 
Private Declare Function DestroyWindow Lib "user32.dll" ( _
 
ByVal hwnd As Integer) As Integer
 
Private Declare Function ClientToScreen Lib "user32.dll" ( _
 
ByVal hwnd As Integer, _
 
ByRef lpPoint As POINTAPI) As Integer
 
'Windows API Constants
 
Private Const WM_USER As Short = &H400S
 
Private Const WM_SETFONT = &H30
 
Private Const CW_USEDEFAULT As Integer = &H80000000
 
'Windows API Types
 
Private Structure RECT
 
Dim left_Renamed As Integer
 
Dim top As Integer
 
Dim right_Renamed As Integer
 
Dim bottom As Integer
 
End Structure
 
Private Structure POINTAPI
 
Dim X As Integer
 
Dim Y As Integer
 
End Structure
 
'Tooltip Window Constants
 
Private Const TTS_NOPREFIX As Short = &H2S
 
Private Const TTF_TRANSPARENT As Short = &H100S
 
Private Const TTF_CENTERTIP As Short = &H2S
 
Private Const TTM_ADDTOOLA As Integer = (WM_USER + 4)
 
Private Const TTM_ACTIVATE As Integer = WM_USER + 1
 
Private Const TTM_UPDATETIPTEXTA As Integer = (WM_USER + 12)
 
Private Const TTM_SETMAXTIPWIDTH As Integer = (WM_USER + 24)
 
Private Const TTM_SETTIPBKCOLOR As Integer = (WM_USER + 19)
 
Private Const TTM_SETTIPTEXTCOLOR As Integer = (WM_USER + 20)
 
Private Const TTM_SETTITLE As Integer = (WM_USER + 32)
 
Private Const TTS_BALLOON As Short = &H40S
 
Private Const TTS_ALWAYSTIP As Short = &H1S
 
Private Const TTF_SUBCLASS As Short = &H10S
 
Private Const TTF_TRACK As Short = &H20S
 
Private Const TTF_IDISHWND As Short = &H1S
 
Private Const TTM_SETDELAYTIME As Integer = (WM_USER + 3)
 
Private Const TTDT_AUTOPOP As Short = 2
 
Private Const TTDT_INITIAL As Short = 3
 
Private Const TTM_TRACKACTIVATE As Integer = WM_USER + 17
 
Private Const TTM_TRACKPOSITION As Integer = WM_USER + 18
 
Private Const WS_POPUP As Integer = &H80000000
 
Private Const TOOLTIPS_CLASSA As String = "tooltips_class32"
 
''Tooltip Window Types
 
Private Structure TOOLINFO
 
Dim lSize As Integer
 
Dim lFlags As Integer
 
Dim hwnd As Integer
 
Dim lId As Integer
 
Dim lpRect As RECT
 
Dim hInstance As Integer
 
Dim lpStr As String
 
Dim lParam As Integer
 
End Structure
 
Public Enum ttIconType
 
TTNoIcon = 0
 
TTIconInfo = 1
 
TTIconWarning = 2
 
TTIconError = 3
 
End Enum
 
Public Enum ttStyleEnum
 
TTStandard
 
TTBalloon
 
End Enum
 
'local variable(s) to hold property value(s)
 
Private m_BackColor As Integer
 
Private m_Title As String
 
Private m_ForeColor As Integer
 
Private m_Icon As ttIconType
 
Private m_Centered As Boolean
 
Private m_Style As ttStyleEnum
 
Private m_TipText As String
 
Private m_VisibleTime As Integer
 
Private m_DelayTime As Integer
 
Private m_PopupOnDemand As Boolean
 
'private data
 
Private m_lTTHwnd As Integer ' hwnd of the tooltip
 
Private m_TipFont As Font
 
Private m_lParentHwnd As Integer ' hwnd of the window the tooltip attached to
 
Private ti As TOOLINFO
 
Public Sub New()
 
MyBase.New()
 
InitCommonControls()
 
m_DelayTime = 500
 
m_VisibleTime = 5000
 
m_PopupOnDemand = False
 
End Sub
 
Protected Overrides Sub Finalize()
 
Destroy()
 
MyBase.Finalize()
 
End Sub
 
'//////////////////////////////////////////////////////
 
Public Property VisibleTime() As Integer
 
Get
 
Return m_VisibleTime
 
End Get
 
Set(ByVal Value As Integer)
 
m_VisibleTime = Value
 
If m_lTTHwnd <> 0 Then
 
SendMessageLong(m_lTTHwnd, TTM_SETDELAYTIME, TTDT_AUTOPOP, m_VisibleTime)
 
End If
 
End Set
 
End Property
 
'//////////////////////////////////////////////////////
 
Public Property DelayTime() As Integer
 
Get
 
Return m_DelayTime
 
End Get
 
Set(ByVal Value As Integer)
 
m_DelayTime = Value
 
If m_lTTHwnd <> 0 Then
 
SendMessageLong(m_lTTHwnd, TTM_SETDELAYTIME, TTDT_INITIAL, m_DelayTime)
 
End If
 
End Set
 
End Property
 
'//////////////////////////////////////////////////////
 
Public Property TipFont() As Font
 
Get
 
Return m_TipFont
 
End Get
 
Set(ByVal Value As Font)
 
m_TipFont = Value
 
If m_lTTHwnd <> 0 Then
 
SendMessageLong(m_lTTHwnd, WM_SETFONT, m_TipFont.ToHfont.ToInt32, 1)
 
End If
 
End Set
 
End Property
 
'//////////////////////////////////////////////////////
 
Public Property Icon() As ttIconType
 
Get
 
Return m_Icon
 
End Get
 
Set(ByVal Value As ttIconType)
 
Dim sysNull As System.DBNull
 
m_Icon = Value
 
If m_lTTHwnd <> 0 And Not (m_Title Is sysNull) And m_Icon <> ttIconType.TTNoIcon Then
 
SendMessageStr(m_lTTHwnd, TTM_SETTITLE, CInt(m_Icon), m_Title)
 
End If
 
End Set
 
End Property
 
'//////////////////////////////////////////////////////
 
Public Property ForeColor() As Integer
 
Get
 
Return m_ForeColor
 
End Get
 
Set(ByVal Value As Integer)
 
m_ForeColor = Value
 
If m_lTTHwnd <> 0 Then
 
SendMessageLong(m_lTTHwnd, TTM_SETTIPTEXTCOLOR, m_ForeColor, 0)
 
End If
 
End Set
 
End Property
 
'//////////////////////////////////////////////////////
 
Public Property Title() As String
 
Get
 
Return ti.lpStr
 
End Get
 
Set(ByVal Value As String)
 
m_Title = Value
 
If m_lTTHwnd <> 0 And m_Title <> "" And m_Icon <> ttIconType.TTNoIcon Then
 
SendMessageStr(m_lTTHwnd, TTM_SETTITLE, CInt(m_Icon), m_Title)
 
End If
 
End Set
 
End Property
 
'//////////////////////////////////////////////////////
 
Public Property TipText() As String
 
Get
 
Return m_TipText
 
End Get
 
Set(ByVal Value As String)
 
m_TipText = Value
 
ti.lpStr = Value
 
If m_lTTHwnd <> 0 Then
 
SendMessageBT(m_lTTHwnd, TTM_UPDATETIPTEXTA, 0, ti)
 
End If
 
End Set
 
End Property
 
'//////////////////////////////////////////////////////
 
Public Property PopupOnDemand() As Boolean
 
Get
 
Return m_PopupOnDemand
 
End Get
 
Set(ByVal Value As Boolean)
 
m_PopupOnDemand = Value
 
'If m_lTTHwnd <> 0 Then
 
'End If
 
End Set
 
End Property
 
'//////////////////////////////////////////////////////
 
Public Property BackColor() As Integer
 
Get
 
Return m_BackColor
 
End Get
 
Set(ByVal Value As Integer)
 
m_BackColor = Value
 
If m_lTTHwnd <> 0 Then
 
SendMessageLong(m_lTTHwnd, TTM_SETTIPBKCOLOR, m_BackColor, 0)
 
End If
 
End Set
 
End Property
 
'//////////////////////////////////////////////////////
 
Public Property Style() As ttStyleEnum
 
Get
 
Style = m_Style
 
End Get
 
Set(ByVal Value As ttStyleEnum)
 
m_Style = Value
 
End Set
 
End Property
 
'//////////////////////////////////////////////////////
 
Public Property Centered() As Boolean
 
Get
 
Centered = m_Centered
 
End Get
 
Set(ByVal Value As Boolean)
 
m_Centered = Value
 
End Set
 
End Property
 
'X and Y are in Pixel so dont send vbTwips value
 
Public Sub Show(Optional ByRef X As Integer = 0, Optional ByRef Y As Integer = 0, Optional ByRef hWndClient As Integer = 0)
 
Dim pt As POINTAPI
 
Dim ptTip As Integer
 
Dim ret As Integer
 
With pt
 
.X = X
 
.Y = Y
 
End With
 
ret = ClientToScreen(hWndClient, pt)
 
ptTip = pt.Y * &H10000
 
ptTip = ptTip + pt.X
 
' These two messages will set the position of the tooltip:
 
ret = SendMessageLong(m_lTTHwnd, TTM_TRACKPOSITION, 0, ptTip)
 
ret = SendMessageBT(m_lTTHwnd, TTM_TRACKACTIVATE, True, ti)
 
End Sub
 
Public Function CreateToolTip(ByVal ParentHwnd As Integer) As Boolean
 
Dim lWinStyle As Integer
 
If m_lTTHwnd <> 0 Then
 
DestroyWindow(m_lTTHwnd)
 
End If
 
m_lParentHwnd = ParentHwnd
 
'create baloon style if desired
 
If m_Style = ttStyleEnum.TTBalloon Then lWinStyle = lWinStyle Or TTS_BALLOON
 
m_lTTHwnd = CreateWindowEx(0, TOOLTIPS_CLASSA, 0, lWinStyle, 0, 0, 0, 0, m_lParentHwnd, 0, 0, 0)
 
'now set our tooltip info structure
 
With ti
 
'NOTE: dont incude TTF_SUBCLASS for on demand
 
' if we want it centered, then set that flag
 
If m_Centered Then
 
If m_PopupOnDemand = False Then
 
.lFlags = TTF_SUBCLASS Or TTF_CENTERTIP Or TTF_IDISHWND
 
Else
 
.lFlags = TTF_IDISHWND Or TTF_TRACK Or TTF_CENTERTIP
 
End If
 
Else
 
If m_PopupOnDemand = False Then
 
.lFlags = TTF_SUBCLASS Or TTF_IDISHWND
 
Else
 
.lFlags = TTF_IDISHWND Or TTF_TRACK Or TTF_TRANSPARENT
 
End If
 
End If
 
'set the hwnd prop to our parent control's hwnd
 
.hwnd = m_lParentHwnd
 
.lId = m_lParentHwnd '0
 
.hInstance = 0 'VB6.GetHInstance.ToInt32
 
.lpStr = m_TipText
 
'.lpRect = lpRect
 
.lSize = Len(ti)
 
End With
 
''add the tooltip structure
 
SendMessageBT(m_lTTHwnd, TTM_ADDTOOLA, 0, ti)
 
'//Set all other property of tooltip
 
Title = m_Title
 
If m_BackColor <> 0 Then BackColor = m_BackColor
 
If m_ForeColor <> 0 Then ForeColor = m_ForeColor
 
If m_VisibleTime <> 0 Then VisibleTime = m_VisibleTime
 
If m_DelayTime <> 0 Then DelayTime = m_DelayTime
 
If Not (m_TipFont Is Nothing) Then TipFont = m_TipFont
 
End Function
 
Public Sub Destroy()
 
If m_lTTHwnd <> 0 Then
 
DestroyWindow(m_lTTHwnd)
 
End If
 
End Sub
 
End Class
 

elloco999

Well-known member
Joined
Dec 21, 2004
Messages
49
Programming Experience
5-10
Thank you very much!!! This is great! You've just helped me out big time!

But I still have a few questions:
1)
You use Text1.Handle.ToInt32 to tell the tooltip the location of the textbox (if I interpreted this correctly). But I want to display a tooltip from a notifyicon, and the SystemWindows.Forms.NotifyIcon class doesnt support "Handle". So how would I go about this then?

2)
I set the visible time to 2000 but the tooltip didn't disapear... Why not?

3)
What does the delaytime do?

4)
What does PopupOnDemand do? The only thing I could find was when PopupOnDemand is on the location of the textbox was used, else the location of the cursor.

Thanks again.
El Loco
 

elloco999

Well-known member
Joined
Dec 21, 2004
Messages
49
Programming Experience
5-10
I came across a realy easy way to implement these balloontips today! In the Coding4Fun column on msdn.microsoft.com (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncodefun/html/code4fun03282003.asp) Duncan Mackenzie uses the HansBlomme.Windows.Forms.NotifyIcon to display these balloons. This is a modified Windows.Forms.NotifyIcon by Hans Blomme and can be found here: http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=D78EC871-8838-46C3-BE25-C9F36B18A8FC

It's realy easy to use. Just declare a HansBlomme.Windows.Forms.NotifyIcon, make it visible and call the NotifyIcon.ShowBalloon(icon, message, title) function.

VB.NET:
Dim ntiMyNotifyicon As HansBlomme.Windows.Forms.NotifyIcon
ntiMyNotifyicon = New HansBlomme.Windows.Forms.NotifyIcon()
ntiMyNotifyicon.Icon = NewIcon("myIcon.ico")
ntiMyNotifyicon.Visible = True
 
ntiMyNotifyicon.ShowBalloon(ntiMyNotifyicon.EBalloonIcon.Info, "This is a test message", "Title")
This should do the trick. Just make sure you have a refference to the HansBlomme.Windows.Forms.NotifyIcon.dll and myIcon.ico is in the bin folder of your project.

Tip: If you don't want to compile the source for the Hans Blomme NotifyIcon, do as I did and grab the dll from the files in the source of the Coding4Fun article.

Greets,
El Loco
 
Top Bottom