找到一段代码,为什么在2000中可以,98中却没有效果?

yassee 2004-01-12 09:53:53
托盘问题
我实现托盘功能,最小化可以放到托盘中。但是只有一个问题,窗体中定义的Me.PopupMenu mnuTray点托盘下的图标右键却出不来,在2000中显示正常,98中却无法显示。为什么?
Private Sub MDIForm_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Select Case CLng(x)
Case WM_RBUTTONUP '注释:鼠标在图标上右击时弹出菜单
MDIForm1.PopupMenu mnuTray
Case WM_LBUTTONUP '注释:鼠标在图标上左击时窗口若最小化则恢复窗口位置
If MDIForm1.WindowState = vbMinimized Then
MDIForm1.Visible = True
MDIForm1.WindowState = LastState
End If
End Select
End Sub
Private Sub MDIForm_Resize()
Select Case WindowState
Case vbMinimized
mnuTrayMaximize.Enabled = True
mnuTrayMinimize.Enabled = False
mnuTrayRestore.Enabled = True
Me.Visible = False
Case vbMaximized
mnuTrayMaximize.Enabled = False
mnuTrayMinimize.Enabled = True
mnuTrayRestore.Enabled = True
Case vbNormal
mnuTrayMaximize.Enabled = True
mnuTrayMinimize.Enabled = True
mnuTrayRestore.Enabled = False
End Select
If WindowState <> vbMinimized Then LastState = WindowState
End Sub

Private Sub MDIForm_Load()
Timer1.Interval = 500 ' 设置计时器时间间隔
Timer1.Enabled = True ' 使计时器可用
' 隐藏图片
For Img_Int = 0 To 4
Icon_Img(Img_Int).Visible = False
Next Img_Int
Img_Int = 0 ' 初始图片索引
If WindowState = vbMinimized Then
LastState = vbNormal
Else
LastState = WindowState
End If
With myData
.cbSize = Len(myData)
.hwnd = Me.hwnd
.uID = 0
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
.uCallbackMessage = WM_MOUSEMOVE
.hIcon = Me.Icon.Handle '注释:默认为窗口图标
.szTip = "IC智能卡" & vbNullChar
End With
Shell_NotifyIcon NIM_ADD, myData
End Sub


module中的声明:

Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const WM_SYSCOMMAND = &H112
Public Const SC_RESTORE = &HF120&
Public LastState As Integer
Public Const NIM_ADD = &H0
Public Const NIM_DELETE = &H2
Public Const NIM_MODIFY = &H1

Public Const NIF_MESSAGE = &H1 '注释:NOTIFYICONDATA结构中uFlags的控制信息
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const WM_MOUSEMOVE = &H200 '注释:当鼠标指针移至图标上
Public Const WM_LBUTTONUP = &H202
Public Const WM_RBUTTONUP = &H205
Public Type NOTIFYICONDATA
cbSize As Long '注释:该数据结构的大小
hwnd As Long '注释:处理任务栏中图标的窗口句柄
uID As Long '注释:定义的任务栏中图标的标识
uFlags As Long '注释:任务栏图标功能控制,可以是以下值的组合(一般全包括)
'注释: NIF_MESSAGE表示发送控制消息;
'注释:NIF_ICON表示显示控制栏中的图标;
'注释: NIF_TIP表示任务栏中的图标有动态提示?
uCallbackMessage As Long '注释:任务栏图标通过它与用户程序交换消息,处理该消息的窗口由hWnd决定
hIcon As Long '注释:任务栏中的图标的控制句柄
szTip As String * 64 '注释:图标的提示信息
End Type
请求高手帮助。
...全文
44 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yassee 2004-01-14
  • 打赏
  • 举报
回复
谢谢各位!
flc 2004-01-13
  • 打赏
  • 举报
回复
关注
LanceJin 2004-01-12
  • 打赏
  • 举报
回复
上面贴的不用睬,这里可能有问题,看看人家的鼠标右键是怎么处理的。
Private Sub MDIForm_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Select Case CLng(x)
Case WM_RBUTTONUP '注释:鼠标在图标上右击时弹出菜单
MDIForm1.PopupMenu mnuTray
Case WM_LBUTTONUP '注释:鼠标在图标上左击时窗口若最小化则恢复窗口位置
If MDIForm1.WindowState = vbMinimized Then
MDIForm1.Visible = True
MDIForm1.WindowState = LastState
End If
End Select
End Sub


SYSTRAY.OCX控件拥有MouseDblClick、MouseDown、MouseMove、MouseUp四个事件,响应先后为MouseMove、MouseDown、MouseUp、MouseDblClick。下面编程很清楚地说明他们的响应时间次序。
  Private Sub cSysTray1_MouseDblClick(Button As Integer, Id As Long)
  MsgBox "Hei!You have DblClick the mouse!"
  End Sub
  Private Sub cSysTray1_MouseDown(Button As Integer, Id As Long)
  If Button = 2 Then
  MsgBox "Hei!You have clicked me in right button!"
  End If
  End Sub
  Private Sub cSysTray1_MouseMove(Id As Long)
  MsgBox "Hello!I am here!"
  End Sub
  Private Sub cSysTray1_MouseUp(Button As Integer, Id As Long)
  MsgBox "Hei!You clicked me just now!"
  End Sub
  Private Sub Form_Load()
  cSysTray1.TrayTip = "Hello! I am the King of the world!"
  cSysTray1.InTray = True
  cSysTray1.TrayIcon = "c:\fittings\FACE.ico"
  End Sub
LanceJin 2004-01-12
  • 打赏
  • 举报
回复
The Shell's systray was introduced with Windows 95 and exists in all versions of Windows. And as enhancements to the Windows Shell have progressed, so too has the definition of the NOTIFYICONDATA structure used to add, modify and delete icons and data placed into the systray, as well as to display, modify and react to balloon tips in Windows 2000, Windows XP and Windows .NET server.
In its earliest form - known as version 3 as Shell 3.0 corresponded to the release of Internet Explorer 3, the NOTIFYICONDATA structure contained just seven members - cbSize, hWnd, uID, uFlags, uCallbackMessage, hIcon, and szTip. In Windows Shell versions prior to version 5, szTip was limited to 64 characters.

typedef struct _NOTIFYICONDATA {
DWORD cbSize;
HWND hWnd;
UINT uID;
UINT uFlags;
UINT uCallbackMessage;
HICON hIcon;
TCHAR szTip[64];
} NOTIFYICONDATA, *PNOTIFYICONDATA;
With the release of Shell 5 in Windows 2000, szTip was doubled to 128 characters and seven new structure parameters were added. Since two of the new structure members are defined as a C Union, which has no counterpart in VB, from our point of view these two structure members - uTimeout and uVersion - need to be represented as a single Long and thus from the VB point of view structure contained six new members.

Shell version 6 released with Windows XP added a further reserved member, itself another UDT defining a GUID. Although for all practical purposes this member is never populated in VB, it is nonetheless expected in the XP definition.

typedef struct _NOTIFYICONDATA {
DWORD cbSize;
HWND hWnd;
UINT uID;
UINT uFlags;
UINT uCallbackMessage;
HICON hIcon;
#if (_WIN32_IE < 0x0500)
TCHAR szTip[64];
#else
TCHAR szTip[128];
#endif
#if (_WIN32_IE >= 0x0500)
DWORD dwState;
DWORD dwStateMask;
TCHAR szInfo[256];
union {
UINT uTimeout;
UINT uVersion;
} DUMMYUNIONNAME;
TCHAR szInfoTitle[64];
DWORD dwInfoFlags;
#endif
#if (_WIN32_IE >= 0x600)
GUID guidItem;
#endif
} NOTIFYICONDATA, *PNOTIFYICONDATA;
In order for an application to properly use the NOTIFYICONDATA structure on all Windows versions the application must inform the shell of the version of the structure it is using. As is usual for a dynamic structure for Microsoft's APIs, the structure contains a cbSize member that is used to specify the size of the UDT being passed. Based on this size the API can take advantage of additional functionality more recent Shell versions provide.

If your application uses and specifies the size of the latest XP structure format, the application may not run with earlier versions of Shell32.dll which expect a smaller structure. The cbSize member must therefore always, as a minimum, be assigned a value that represents the correct size for the current target Shell version. Note that this does not necessarily correlate to an OS version as some version of Internet Explorer, as recent as IE5, upgraded the shell in order to provide the new version's functionality. Therefore it is insufficient to test merely for the OS version using GetVersion or GetVersionEx.

The Shell_NotifyIcon API requires the cbSize value be properly specified whenever a NOTIFYICONDATA structure is passed. Failing to set cbSize at all will cause the call to fail with no icon appearing in the systray. Passing a structure designed for use in later Windows versions to a prior shell version will cause either nothing to occur or if subclassed for messages, cause your app to crash. These points - shell version and structure size - are the two most popular issues developers seem to have difficulty with in designing systray applications to run on systems other than their development machine.
griefforyou 2004-01-12
  • 打赏
  • 举报
回复
每次遇到这种问题我都会推荐以下文章:

托盘程序详解(一)
http://www.hongen.com/pc/program/apitutor/api0012/api01.htm
铁拳 2004-01-12
  • 打赏
  • 举报
回复
我用vb写了个active dll,里面也有对任务栏图标的 mousemove,mousedown,mouseup,mousedblclick事件,这部份已经完成了,不过还没有发布,准备将它做得更专业一点,你可以试试看将你的 MDIForm1.PopupMenu mnuTray 改为
SetForegroundWindow MDIForm1.hWnd
MDIForm1.PopupMenu mnuTray
还有一句想说的话,就是感觉把你要处理的东西放到mousemove好像不太专业,建议使用对任务栏的消息处理方式会好一些,网上有不少这样的资料,或者在 http://www.applevb.com/mysoft.htm 最下面我以前写的程序里面也用到了这些,不过最好不要在98下使用,里面的自绘菜单有点问题,会导致GPF,看看关于模块里处理消息那一块。

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧