当前位置:编程学习 > VB >>

vb菜单的小图标

看到图片了吗?如何在旁边加小图标呢不要去百度复制,我找了很多没用的

追问:你的QQ多少,我问你去

答案:一种是在系统任务栏上右击弹出菜单,但是此菜单为系统标题栏菜单,需要用API添加
另一种在系统托盘处单机右键,需要建立一个系统ID号,仍然要用API,但是有的时候
这一种方法在VB中总是有点问题,98和2000下可以,XP中必须使用 unicode API ,
否则会出问题,在系统托盘处不能响应鼠标事件
用法如下:
Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
'定义消息
Private Const NIM_ADD = &H0 '添加图标到任务栏提示区
Private Const NIM_MODIFY = &H1 '图标特性已改变的消息
Private Const NIM_DELETE = &H2 '删除图标
'定义标识位
Private Const NIF_MESSAGE = &H1 '消息合法
Private Const NIF_ICON = &H2 '图标句柄合法
Private Const NIF_TIP = &H4 '提示合法

Private Const WM_MOUSEMOVE = &H200
'Private Const WM_MOUSEMOVE = 7680
'定义MouseMove消息,该消息将被发送到
'窗体的MouseMove事件处理函数中处理
'定义鼠标消息常数
'Private Const WM_LBUTTONDBLCLK = &H203
Private Const WM_LBUTTONDBLCLK = 7725
Private Const WM_LBUTTONDOWN = &H201
'Private Const WM_LBUTTONUP = &H202
Private Const WM_LBUTTONUP = 7710
Private Const WM_RBUTTONDBLCLK = &H206
Private Const WM_RBUTTONDOWN = &H204
'Private Const WM_RBUTTONUP = &H205
Private Const WM_RBUTTONUP = 7755
Private Type NOTIFYICONDATA
cbSize As Long
hWnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * 64
End Type

Dim mtIconData As NOTIFYICONDATA
Private Sub Form_Unload(Cancel As Integer)
If i易做图it = True Then
DeleteIconFromTray '在此调用图标清除函数,这样每次退出时自动清除图标
Set Form1 = Nothing
Else
Me.Visible = False
Cancel = 1
End If
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'在AddIconToTray中语句uCallbackMessage = WM_MOUSEMOVE
'将回调消息发送到此函数中进行处理
'实际的回调消息存储在参数X中
'注意在此情况下,X是一个消息而不是坐标


Static bBusy As Boolean
If bBusy = False Then '控制变量,确保每次事件发生只处理一次
bBusy = True

Select Case CLng(X)
Case WM_MOUSEMOVE

Case WM_LBUTTONDBLCLK
'处理双击消息,显示关于对话框
Me.Visible = Not Me.Visible
SetWindowPos Me.hWnd, -1, 0, 0, 0, 0, 1 Or 2
Case WM_LBUTTONDOWN
'处理左键按下消息,依次改变图标显示的图案

Case WM_LBUTTONUP '处理左键释放消息

'PopupMenu mnuTray, 2, , , mnuAbout
Case WM_RBUTTONDBLCLK '处理右键双击消息

Case WM_RBUTTONDOWN '处理右键按下消息
Case WM_RBUTTONUP
'处理右键释放消息,显示弹出菜单

PopupMenu mnuTray
End Select
bBusy = False
End If
End Sub
Private Sub AddIconToTray() '添加图标到提示区
With mtIconData
.cbSize = Len(mtIconData)
.hWnd = Me.hWnd '定义处理回调消息的窗口
.uCallbackMessage = WM_MOUSEMOVE
'告诉图标发送MouseMove消息
.uID = 1& '定义图标号
.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
.hIcon = Me.Icon
.szTip = "计时器已终止" & Chr(0)
'定义程序运行时显示的图标
'定义提示
If Shell_NotifyIcon(NIM_ADD, mtIconData) = 0 Then
'在任务栏提示区创建一图标

End If
End With
End Sub
Private Sub DeleteIconFromTray()
If Shell_NotifyIcon(NIM_DELETE, mtIconData) = 0 Then
End If
End Sub


Private Sub Label1_dblClick(Index As Integer)
Me.Visible = False
End Sub

Private Sub mnuAbout_Click()
Me.Visible = True
SetWindowPos Me.hWnd, -1, 0, 0, 0, 0, 1 Or 2
End Sub
Private Sub mnuExit_Click()
i易做图it = True
Unload Me
End Sub
关于系统菜单的添加方法有很多书都有介绍,就不多说了
这个需要用到API 我还不会 


点击菜单项,右边属性窗口:

Image属性选本地资源或项目资源,把图标加进来。

你这个是VB.Net,说VB的话会让人误以为是VB6,这两个是不一样的。


上一个:VB串口发送数组数据
下一个:VB 修改图片尺寸大小

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,