//====================================================
//Program Name : ToolTips
//using to show how to create a tooltip for a control as a balloon.
//Written by China.Newlad in 2006.3.17
//====================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ToolTips
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
m_tooltip.AutoPopDelay = 5000;//display 5s.
m_tooltip.InitialDelay = 100;//after 1s the tooltip will show.
m_tooltip.ReshowDelay = 100;//reshow time.
m_tooltip.ShowAlways = true;//active or not ,it always show.
m_tooltip.IsBalloon = true;//change the style to balloon.
m_tooltip.ToolTipIcon = ToolTipIcon.Info;//set the icon.
m_tooltip.ToolTipTitle = "Hello";//set the caption
m_tooltip.SetToolTip(this.button1, "This is caption");//set which control to show this balloon.
}
}
}
//=============================================================
//Program Name : SalverBalloonTips.cs
//Show a balloon tips in the taskbar.
//Written by China.Newlad in 2006.3.17
//=============================================================
using System;
using System.Drawing;
using System.Windows.Forms;
class SalverBalloonTips
{
public static void Main(string[] args)
{
SalverBalloonTips a = new SalverBalloonTips();
NotifyIcon balloon = new NotifyIcon();
balloon.Visible = true;
balloon.Icon = new Icon(@"D:\Resource\source code\C#\NotifyIcon(SalverBalloonTips)\EYE.ICO");
balloon.ShowBalloonTip(10, "Balloon tips", "Hello,China.Newlad", ToolTipIcon.Info);
Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Private Type NOTIFYICONDATA
cbSize As Long ' 结构大小(字节)
hwnd As Long ' 处理消息的窗口的句柄
uId As Long ' 唯一的标识符
uFlags As Long ' Flags
uCallBackMessage As Long ' 处理消息的窗口接收的消息
hIcon As Long ' 托盘图标句柄
szTip As String * 128 ' Tooltip 提示文本
dwState As Long ' 托盘图标状态
dwStateMask As Long ' 状态掩码
szInfo As String * 256 ' 气球提示文本
uTimeoutOrVersion As Long ' 气球提示消失时间或版本
' uTimeout - 气球提示消失时间(单位:ms, 10000 -- 30000)
' uVersion - 版本(0 for V4, 3 for V5)
szInfoTitle As String * 64 ' 气球提示标题
dwInfoFlags As Long ' 气球提示图标
End Type
' uFlags to NOTIFYICONDATA structure
Private Const NIF_ICON As Long = &H2
Private Const NIF_INFO As Long = &H10
Private Const NIF_MESSAGE As Long = &H1
Private Const NIF_STATE As Long = &H8
Private Const NIF_TIP As Long = &H4
' dwMessage to Shell_NotifyIcon
Private Const NIM_ADD As Long = &H0
Private Const NIM_DELETE As Long = &H2
Private Const NIM_MODIFY As Long = &H1
Private Const NIM_SETFOCUS As Long = &H3
Private Const NIM_SETVERSION As Long = &H4
Private Sub Form_Load()
' 向托盘区添加图标
Dim IconData As NOTIFYICONDATA
Dim title As String
title = "托盘程序" & vbNullChar
With IconData
.cbSize = Len(IconData)
.hwnd = Me.hwnd
.uId = 0
.uFlags = NIF_TIP Or NIF_ICON Or NIF_MESSAGE Or NIF_INFO Or NIF_STATE
.uCallBackMessage = WM_NOTIFYICON
.szTip = title
.hIcon = Me.Icon.Handle
.dwState = 0
.dwStateMask = 0
.szInfo = "这是气球提示" & vbNullChar
.szInfoTitle = title
.dwInfoFlags = NIIF_INFO
.uTimeoutOrVersion = 10000
End With
Shell_NotifyIcon NIM_ADD, IconData
preWndProc = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Private Sub Form_Unload(Cancel As Integer)
' 删除托盘区图标
Dim IconData As NOTIFYICONDATA
With IconData
.cbSize = Len(IconData)
.hwnd = Me.hwnd
.uId = 0
.uFlags = NIF_TIP Or NIF_ICON Or NIF_MESSAGE
.uCallBackMessage = WM_NOTIFYICON
.szTip = "托盘程序"
.hIcon = Me.Icon.Handle
End With
Shell_NotifyIcon NIM_DELETE, IconData
SetWindowLong Me.hwnd, GWL_WNDPROC, preWndProc
' 卸载所有窗体
Dim frm As Form
For Each frm In Forms
Unload frm
Next
End Sub
标准模块:
Option Explicit
Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32.dll" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const WM_RBUTTONUP = &H205
Public Const WM_USER = &H400
Public Const WM_NOTIFYICON = WM_USER + 1 ' 自定义消息
Public Const WM_LBUTTONDBLCLK = &H203
Public Const GWL_WNDPROC = (-4)
' Form1 窗口入口函数
Function WindowProc(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
' 拦截 WM_NOTIFYICON 消息
If msg = WM_NOTIFYICON Then
Select Case lParam
Case WM_RBUTTONUP
' 右键单击图标是运行这里的代码, 可以在这里添加弹出右键菜单的代码
Case WM_LBUTTONDBLCLK
Unload Form1
Case NIN_BALLOONSHOW
Debug.Print "显示气球提示"
Case NIN_BALLOONHIDE
Debug.Print "删除托盘图标"
Case NIN_BALLOONTIMEOUT
Debug.Print "气球提示消失"
Case NIN_BALLOONUSERCLICK
Debug.Print "单击气球提示"
End Select
End If
WindowProc = CallWindowProc(preWndProc, hwnd, msg, wParam, lParam)
End Function