分享
 
 
 

如何在系统托盘(panel)中显示应用程序图标和气球帮助信息(balloon tooltips)

王朝c#·作者佚名  2006-12-17
窄屏简体版  字體: |||超大  

1、界面如下图:

2、代码如下

Option Explicit

'转载自VBNET

Private Const APP_SYSTRAY_ID = 999

Private Const NOTIFYICON_VERSION = &H3

Private Const NIF_MESSAGE = &H1

Private Const NIF_ICON = &H2

Private Const NIF_TIP = &H4

Private Const NIF_STATE = &H8

Private Const NIF_INFO = &H10

Private Const NIM_ADD = &H0

Private Const NIM_MODIFY = &H1

Private Const NIM_DELETE = &H2

Private Const NIM_SETFOCUS = &H3

Private Const NIM_SETVERSION = &H4

Private Const NIM_VERSION = &H5

Private Const NIS_HIDDEN = &H1

Private Const NIS_SHAREDICON = &H2

'icon flags

Private Const NIIF_NONE = &H0

Private Const NIIF_INFO = &H1

Private Const NIIF_WARNING = &H2

Private Const NIIF_ERROR = &H3

Private Const NIIF_GUID = &H5

Private Const NIIF_ICON_MASK = &HF

Private Const NIIF_NOSOUND = &H10

Private Const WM_USER = &H400

Private Const NIN_BALLOONSHOW = (WM_USER + 2)

Private Const NIN_BALLOONHIDE = (WM_USER + 3)

Private Const NIN_BALLOONTIMEOUT = (WM_USER + 4)

Private Const NIN_BALLOONUSERCLICK = (WM_USER + 5)

'shell version / NOTIFIYICONDATA struct size constants

Private Const NOTIFYICONDATA_V1_SIZE As Long = 88 'pre-5.0 structure size

Private Const NOTIFYICONDATA_V2_SIZE As Long = 488 'pre-6.0 structure size

Private Const NOTIFYICONDATA_V3_SIZE As Long = 504 '6.0+ structure size

Private NOTIFYICONDATA_SIZE As Long

Private Type GUID

Data1 As Long

Data2 As Integer

Data3 As Integer

Data4(7) As Byte

End Type

Private Type NOTIFYICONDATA

cbSize As Long

hWnd As Long

uID As Long

uFlags As Long

uCallbackMessage As Long

hIcon As Long

szTip As String * 128

dwState As Long

dwStateMask As Long

szInfo As String * 256

uTimeoutAndVersion As Long

szInfoTitle As String * 64

dwInfoFlags As Long

guidItem As GUID

End Type

Private Declare Function Shell_NotifyIcon Lib "shell32.dll" _

Alias "Shell_NotifyIconA" _

(ByVal dwMessage As Long, _

lpData As NOTIFYICONDATA) As Long

Private Declare Function GetFileVersionInfoSize Lib "version.dll" _

Alias "GetFileVersionInfoSizeA" _

(ByVal lptstrFilename As String, _

lpdwHandle As Long) As Long

Private Declare Function GetFileVersionInfo Lib "version.dll" _

Alias "GetFileVersionInfoA" _

(ByVal lptstrFilename As String, _

ByVal dwHandle As Long, _

ByVal dwLen As Long, _

lpData As Any) As Long

Private Declare Function VerQueryValue Lib "version.dll" _

Alias "VerQueryValueA" _

(pBlock As Any, _

ByVal lpSubBlock As String, _

lpBuffer As Any, _

nVerSize As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" _

Alias "RtlMoveMemory" _

(Destination As Any, _

Source As Any, _

ByVal Length As Long)

Private Sub Form_Load()

Text1.Text = "Copyright ?2004-2008 Lipper Zhao, All Rights Reserved." & _

"now available for download from " & _

"http://www.somplace.com/myapp/update/."

Command1.Caption = "Add Systray Icon"

Command2.Caption = "Show Balloon Tip"

Command2.Enabled = False

Option1(0).Caption = "no icon"

Option1(1).Caption = "information icon"

Option1(2).Caption = "warning icon"

Option1(3).Caption = "error icon"

Option1(1).Value = True

End Sub

Private Sub Form_Unload(Cancel As Integer)

ShellTrayRemove

End Sub

Private Sub Command1_Click()

Call ShellTrayAdd

Command2.Enabled = True

End Sub

Private Sub Command2_Click()

ShellTrayModifyTip GetSelectedOptionIndex()

End Sub

Private Sub ShellTrayAdd()

Dim nid As NOTIFYICONDATA

If NOTIFYICONDATA_SIZE = 0 Then SetShellVersion

'set up the type members

With nid

.cbSize = NOTIFYICONDATA_SIZE

.hWnd = Form1.hWnd

.uID = APP_SYSTRAY_ID

.uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP

.dwState = NIS_SHAREDICON

.hIcon = Form1.Icon

'szTip is the tooltip shown when the

'mouse hovers over the systray icon.

'Terminate it since the strings are

'fixed-length in NOTIFYICONDATA.

.szTip = "New Download Watcher" & vbNullChar

.uTimeoutAndVersion = NOTIFYICON_VERSION

End With

'add the icon ...

Call Shell_NotifyIcon(NIM_ADD, nid)

'... and inform the system of the

'NOTIFYICON version in use

Call Shell_NotifyIcon(NIM_SETVERSION, nid)

End Sub

Private Sub ShellTrayRemove()

Dim nid As NOTIFYICONDATA

If NOTIFYICONDATA_SIZE = 0 Then SetShellVersion

With nid

.cbSize = NOTIFYICONDATA_SIZE

.hWnd = Form1.hWnd

.uID = APP_SYSTRAY_ID

End With

Call Shell_NotifyIcon(NIM_DELETE, nid)

End Sub

Private Sub ShellTrayModifyTip(nIconIndex As Long)

Dim nid As NOTIFYICONDATA

If NOTIFYICONDATA_SIZE = 0 Then SetShellVersion

With nid

.cbSize = NOTIFYICONDATA_SIZE

.hWnd = Form1.hWnd

.uID = APP_SYSTRAY_ID

.uFlags = NIF_INFO

.dwInfoFlags = nIconIndex

'InfoTitle is the balloon tip title,

'and szInfo is the message displayed.

'Terminating both with vbNullChar prevents

'the display of the unused padding in the

'strings defined as fixed-length in NOTIFYICONDATA.

.szInfoTitle = "New Download Available!" & vbNullChar

.szInfo = Text1.Text & vbNullChar

End With

Call Shell_NotifyIcon(NIM_MODIFY, nid)

End Sub

Private Sub SetShellVersion()

Select Case True

Case IsShellVersion(6)

NOTIFYICONDATA_SIZE = NOTIFYICONDATA_V3_SIZE '6.0+ structure size

Case IsShellVersion(5)

NOTIFYICONDATA_SIZE = NOTIFYICONDATA_V2_SIZE 'pre-6.0 structure size

Case Else

NOTIFYICONDATA_SIZE = NOTIFYICONDATA_V1_SIZE 'pre-5.0 structure size

End Select

End Sub

Private Function IsShellVersion(ByVal version As Long) As Boolean

'returns True if the Shell version

'(shell32.dll) is equal or later than

'the value passed as 'version'

Dim nBufferSize As Long

Dim nUnused As Long

Dim lpBuffer As Long

Dim nVerMajor As Integer

Dim bBuffer() As Byte

Const sDLLFile As String = "shell32.dll"

nBufferSize = GetFileVersionInfoSize(sDLLFile, nUnused)

If nBufferSize > 0 Then

ReDim bBuffer(nBufferSize - 1) As Byte

Call GetFileVersionInfo(sDLLFile, 0&, nBufferSize, bBuffer(0))

If VerQueryValue(bBuffer(0), "\", lpBuffer, nUnused) = 1 Then

CopyMemory nVerMajor, ByVal lpBuffer + 10, 2

IsShellVersion = nVerMajor >= version

End If 'VerQueryValue

End If 'nBufferSize

End Function

Private Function GetSelectedOptionIndex() As Long

'returns the selected item index from

'an option button array. Use in place

'of multiple If...Then statements!

'If your array contains more elements,

'just append them to the test condition,

'setting the multiplier to the button's

'negative -index.

GetSelectedOptionIndex = Option1(0).Value * 0 Or _

Option1(1).Value * -1 Or _

Option1(2).Value * -2 Or _

Option1(3).Value * -3

End Function

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有