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 Declare Function CallWindowProc Lib "user32" 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 Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long |
|
|
#3 得分:21
回复于: 2002-11-15 18:13:24
Shell_NotifyIcon
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 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_LBUTTONDBLCLK = &H203 Private Const WM_LBUTTONDOWN = &H201 Private Const WM_RBUTTONUP = &H205 Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean Dim TrayI As NOTIFYICONDATA Private Sub Form_Load() TrayI.cbSize = Len(TrayI) 'Set the window's handle (this will be used to hook the specified window) TrayI.hWnd = pichook.hWnd 'Application-defined identifier of the taskbar icon TrayI.uId = 1& 'Set the flags TrayI.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE 'Set the callback message TrayI.ucallbackMessage = WM_LBUTTONDOWN 'Set the picture (must be an icon!) TrayI.hIcon = imgIcon(2).Picture 'Set the tooltiptext TrayI.szTip = "Recent" & Chr$(0) 'Create the icon Shell_NotifyIcon NIM_ADD, TrayI Me.Hide End Sub Private Sub Form_Unload(Cancel As Integer) 'remove the icon TrayI.cbSize = Len(TrayI) TrayI.hWnd = pichook.hWnd TrayI.uId = 1& Shell_NotifyIcon NIM_DELETE, TrayI End End Sub Private Sub mnuPop_Click(Index As Integer) Select Case Index Case 0 MsgBox "KPD-Team 1998" + , vbInformation + vbOKOnly Case 2 Unload Me End Select End Sub Private Sub pichook_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Msg = X / Screen.TwipsPerPixelX If Msg = WM_LBUTTONDBLCLK Then 'Left button double click mnuPop_Click 0 ElseIf Msg = WM_RBUTTONUP Then 'Right button click Me.PopupMenu mnuPopUp End If End Sub Private Sub Timer1_Timer() Static Tek As Integer 'Animate the icon Me.Icon = imgIcon(Tek).Picture TrayI.hIcon = imgIcon(Tek).Picture Tek = Tek + 1 If Tek = 3 Then Tek = 0 Shell_NotifyIcon NIM_MODIFY, TrayI End Sub SendMessage Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Any) As Long Const LB_FINDSTRING = &H18F Private Sub Form_Load() With List1 .AddItem "Computer" .AddItem "Screen" .AddItem "Modem" .AddItem "Printer" .AddItem "Scanner" .AddItem "Sound Blaster" .AddItem "Keyboard" .AddItem "CD-Rom" .AddItem "Mouse" End With End Sub Private Sub Text1_Change() 'Retrieve the item's listindex List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal CStr(Text1.Text)) End Sub SetWindowLong Private Sub Form_Load() 'KPD-Team 1999 'URL: http://www.allapi.net/ 'E-Mail: [email protected] 'Subclass this form HookForm Me 'Register this form as a Clipboardviewer SetClipboardViewer Me.hwnd End Sub Private Sub Form_Unload(Cancel As Integer) 'Unhook the form UnHookForm Me End Sub Private Sub Command1_Click() 'Change the clipboard Clipboard.Clear Clipboard.SetText "Hello !" End Sub Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Declare Function SetClipboardViewer Lib "user32" (ByVal hwnd As Long) As Long Public Const WM_DRAWCLIPBOARD = &H308 Public Const GWL_WNDPROC = (-4) Dim PrevProc As Long Public Sub HookForm(F As Form) PrevProc = SetWindowLong(F.hwnd, GWL_WNDPROC, AddressOf WindowProc) End Sub Public Sub UnHookForm(F As Form) SetWindowLong F.hwnd, GWL_WNDPROC, PrevProc End Sub Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long WindowProc = CallWindowProc(PrevProc, hwnd, uMsg, wParam, lParam) If uMsg = WM_DRAWCLIPBOARD Then MsgBox "Clipboard changed ..." End If End Function |
|
|
#4 得分:9
回复于: 2002-11-15 18:17:41
CallWindowProc
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Any, ByVal wParam As Any, ByVal lParam As Any) As Long Private Sub Form_Load() On Error Resume Next Dim lb As Long, pa As Long 'map 'user32' into the address space of the calling process. lb = LoadLibrary("user32") 'retrieve the address of 'SetWindowTextA' pa = GetProcAddress(lb, "SetWindowTextA") 'Call the SetWindowTextA-function CallWindowProc pa, Me.hWnd, "Hello !", ByVal 0&, ByVal 0& 'unmap the library's address FreeLibrary lb End Sub GetWindowLong Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Const GWL_STYLE = (-16) Const ES_NUMBER = &H2000& Public Sub SetNumber(NumberText As TextBox, Flag As Boolean) Dim curstyle As Long, newstyle As Long 'retrieve the window style curstyle = GetWindowLong(NumberText.hwnd, GWL_STYLE) If Flag Then curstyle = curstyle Or ES_NUMBER Else curstyle = curstyle And (Not ES_NUMBER) End If 'Set the new style newstyle = SetWindowLong(NumberText.hwnd, GWL_STYLE, curstyle) 'refresh NumberText.Refresh End Sub Private Sub Form_Load() SetNumber Text1, True Me.Caption = "Now, try typing some letters into the textbox" End Sub |
8
|
#5 得分:12
回复于: 2002-11-15 21:56:40
shell_notifyicon是操作托盘图标
NOTIFYICONDATA.hWnd 是接受消息的窗体句柄 NOTIFYICONDATA.ucallbackMessage 是回调消息的消息编号 回调消息的lParam常数就是在托盘上的鼠标消息 SendMessage是发送Windows消息,wParam、lParam根据发送的消息的不同而不同 CallWindowProc是向消息处理函数传递消息,主要用于 子类处理消息 技术 GetWindowLong,SetWindowLong是用于取得、设置窗体的某些属性,GWL_WNDPROC是用于取得、设置窗体的消息处理程序地址 |
|
#6 得分:8
回复于: 2002-11-16 01:02:32
[名称] VB托盘程序源代码
[语言种类] Visual Basic [类别一] 界面 [类别二] 空 [类别三] 空 [数据来源] 未知 [保存时间] 2002-01-05 [关键字一] API [关键字二] 托盘 [关键字三] 空 [内容简介] 空 [源代码内容] 'Type NOTIFYICONDATA ' cbSize As Long 需填入NOTIFYICONDATA数据结构的长度 ' HWnd As Long 设置成窗口的句柄 ' Uid As Long 为图标所设置的ID值 ' UFlags As Long 用来设置以下三个参数uCallbackMessage、hIcon、szTip是否有效 ' UCallbackMessage As Long 消息编号 ' HIcon As Long 显示在状态栏上的图标 ' SzTip As String * 64 提示信息 'End Type '---- 其中参数uCallbackMessage、hIcon、szTip也应在模块中声明为以下的常量: 'Public Const NIF_MESSAGE = 1 'Public Const NIF_ICON = 2 'Public Const NIF_TIP = 4 Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long Public Const WM_USER = &H400 Public Const WM_LBUTTONUP = &H202 Public Const WM_MBUTTONUP = &H208 Public Const WM_RBUTTONUP = &H205 Public Const TRAY_CALLBACK = (WM_USER + 1001&) Public Const GWL_WNDPROC = (-4) Public Const GWL_USERDATA = (-21) Public Const NIF_ICON = &H2 Public Const NIF_TIP = &H4 Public Const NIM_ADD = &H0 Public Const NIF_MESSAGE = &H1 Public Const NIM_MODIFY = &H1 Public Const NIM_DELETE = &H2 '记录 设置托盘图标的数据 的数据类型NOTIFYICONDATA Public 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 'TheData变量记录设置托盘图标的数据 Private TheData As NOTIFYICONDATA ' ********************************************* ' 新的窗口过程--主程序中采用SetWindowLong函数改变了窗口函数的地址,消息转向由NewWindowProc处理 ' ********************************************* Public Function NewWindowProc(ByVal HWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long '如果用户点击了托盘中的图标,则进行判断是点击了左键还是右键 If Msg = TRAY_CALLBACK Then '如果点击了左键 If lParam = WM_LBUTTONUP Then '而这时窗体的状态是最小化时 If TheForm.WindowState = vbMinimized Then _ '恢复到最小化前的窗体状态 TheForm.WindowState = TheForm.LastState TheForm.SetFocus Exit Function End If End If '如果点击了右键 If lParam = WM_RBUTTONUP Then '则弹出右键菜单 TheForm.PopupMenu TheMenu Exit Function End If End If'如果是其他类型的消息则传递给原有默认的窗口函数 NewWindowProc = CallWindowProc(OldWindowProc, HWnd, Msg, wParam, lParam) End Function ' ********************************************* ' 把主窗体的图标(Form1.icon属性可改变)添加到托盘中 ' ********************************************* Public Sub AddToTray(frm As Form, mnu As Menu) '保存当前窗体和菜单信息 Set TheForm = frm Set TheMenu = mnu 'GWL_WNDPROC获得该窗口的窗口函数的地址 OldWindowProc = SetWindowLong(frm.HWnd, GWL_WNDPROC, AddressOf NewWindowProc) '知识点滴:HWnd属性 '返回窗体或控件的句柄。语法: object.HWnd '说明:Microsoft Windows 运行环境,通过给应用程序中的每个窗体和控件 '分配一个句柄(或 hWnd)来标识它们。hWnd 属性用于Windows API调用。 '将主窗体图标添加在托盘中 With TheData .Uid = 0 '忘了吗?参考一下前面内容,Uid图标的序号,做动画图标有用 .HWnd = frm.HWnd .cbSize = Len(TheData) .HIcon = frm.Icon.Handle .UFlags = NIF_ICON '指明要对图标进行设置 .UCallbackMessage = TRAY_CALLBACK .UFlags = .UFlags Or NIF_MESSAGE '指明要设置图标或返回信息给主窗体,此句不能省去 .cbSize = Len(TheData) '为什么呢?我们需要在添加图标的同时,让其返回信息 End With '给主窗体,Or的意思是同时进行设置和返回消息 Shell_NotifyIcon NIM_ADD, TheData '根据前面定义NIM_ADD,设置为“添加模式” End Sub ' ********************************************* ' 删除系统托盘中的图标 ' ********************************************* Public Sub RemoveFromTray() '删除托盘中的图标 With TheData .UFlags = 0 End With Shell_NotifyIcon NIM_DELETE, TheData '根据前面定义NIM_DELETE,设置为“删除模式” '恢复原有的设置 SetWindowLong TheForm.HWnd, GWL_WNDPROC, OldWindowProc End Sub ' ********************************************* ' 为托盘中的图标加上浮动提示(也就是鼠标移上去时出现的提示字条) ' ********************************************* Public Sub SetTrayTip(tip As String) With TheData .SzTip = tip & vbNullChar .UFlags = NIF_TIP '指明要对浮动提示进行设置 End With Shell_NotifyIcon NIM_MODIFY, TheData '根据前面定义NIM_MODIFY,设置为“修改模式” End Sub ' ********************************************* ' 设置托盘的图标(在本例中没有用到,如果要动态改变托盘内显示的图标,它非常有用) ' 例如:1、显示动画图标(方法你一定猜到了,对!使用Timer控件,不断调用此过程,注意把动画放在pic数组中) ' 2、程序处于不同状态时,显示不同的图标,方法是类似的 ' 有兴趣的话试一试吧。 ' ********************************************* Public Sub SetTrayIcon(pic As Picture) '判断一下pic中存放的是不是图标 If pic.Type <> vbPicTypeIcon Then Exit Sub '更换图标为pic中存放的图标 With TheData .HIcon = pic.Handle .UFlags = NIF_ICON End With Shell_NotifyIcon NIM_MODIFY, TheData End Sub 以上代码保存于: SourceCode Explorer(源代码数据库) 复制时间: 2002-11-16 01:01 软件版本: 1.0.753 软件作者: Shawls 个人主页: Http://Shawls.Yeah.Net E-Mail: [email protected] QQ: 9181729 |
|
#7 得分:8
回复于: 2002-11-16 01:02:50
[名称] VB托盘程序详解
[语言种类] Visual Basic [类别一] 界面 [类别二] 空 [类别三] 空 [数据来源] 未知 [保存时间] 2002-01-05 [关键字一] API [关键字二] 托盘 [关键字三] 空 [内容简介] 空 [源代码内容] 很多软件运行时会在系统托盘区(就是桌面右下角显示时间的区域)出现一个小图标,它作为程序运行的一个标志,我们可以通过使用小图标所弹出的菜单来控制应用程序的状态。本例就给出了一个功能比较完整的托盘程序,我们可以看到怎样用API函数Shell_NotifyIcon来添加、删除、更改托盘图标;而且例中还演示了为托盘图标添加右键菜单和浮动提示的方法。 程序(附后)用到了Shell_NotifyIcon、SendMessage、CallWindowProc、SetWindowLong等API函数,其中Shell_NotifyIcon是主要的函数,它用来添加、删除、更改系统托盘区(taskbar status area)的图标,所以我们先来看看这个函数的声明和参数: 使用API函数之前必须先在程序中声明如下: Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long 其中各参数的意义如下表: 参数: 意义 dwMessage 为消息设置值,它可以是以下的几个常数值:0、1、2 NIM_ADD = 0 加入图标到系统状态栏中 NIM_MODIFY = 1 修改系统状态栏中的图标 NIM_DELETE = 2 删除系统状态栏中的图标 LpData 用以传入NOTIFYICONDATA数据结构变量,其结构如下所示: Type NOTIFYICONDATA cbSize As Long 需填入NOTIFYICONDATA数据结构的长度 HWnd As Long 设置成窗口的句柄 Uid As Long 为图标所设置的ID值 UFlags As Long 设置uCallbackMessage,hIcon,szTip是否有效 UCallbackMessage As Long 消息编号 HIcon As Long 显示在状态栏上的图标 SzTip As String * 64 提示信息 End Type 返回值 Long,非零表示成功,零表示失败 在使用这个API函数之前我们应该先定义结构类型NOTIFYICONDATA: Public 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 然后定义一个NOTIFYICONDATA的变量TheData来记录设置托盘图标的数据 Private TheData As NOTIFYICONDATA 这时我们就可以使用这个函数来设置系统托盘图标了,具体方法如下: 1、添加图标 With TheData .Uid = 0 .HWnd = frm.HWnd 'frm.HWnd是程序主窗体的句柄 .cbSize = Len(TheData) .HIcon = frm.Icon.Handle 'frm.Icon.Handle指向主窗体的图标 .UFlags = NIF_ICON .UCallbackMessage = TRAY_CALLBACK '作用是允许返回消息,在下一节中会有详细解释。 .UFlags = .UFlags Or NIF_MESSAGE .cbSize = Len(TheData) End With Shell_NotifyIcon NIM_ADD, TheData '根据前面定义NIM_ADD,设置为“添加模式”,然后添加 2、删去图标 With TheData .UFlags = 0 End With Shell_NotifyIcon NIM_DELETE, TheData '根据前面定义NIM_DELETE,设置为“删除模式” 3、更改图标 With TheData .HIcon = pic.Handle 'pic是图片狂PictureBox,存放图标文件 .UFlags = NIF_ICON End With Shell_NotifyIcon NIM_MODIFY, TheData '根据前面定义NIM_MODIFY,设置为“更改模式” 4、为图标添加浮动提示信息 With TheData .SzTip = tip & vbNullChar 'tip是字符串string,存储提示信息 .UFlags = NIF_TIP '指明要对浮动提示进行设置 End With Shell_NotifyIcon NIM_MODIFY, TheData '根据前面定义NIM_MODIFY,设置为“修改模式” 通过以上几段代码我们就能根据自己需要添加、删除、更改系统托盘图标,并能添加系统图标上的浮动提示信息。但这时的托盘图标是孤立的,我们并不能利用它来控制应用程序的行为,怎么办呢?别急,请往下看……如果你下载(源程序下载)并运行这个例程序,你会发现如果我们在托盘图标上点击鼠标右键,则会弹出一个右键菜单。如果点击相应的菜单项,程序主窗体会随之变化,这样就可以控制程序的行为。而如果当主窗体处于最小化状态时,我们在托盘图标上点击左键,窗体会恢复到原来的大小。其实实现上述的功能都要依赖于WINDOWS操作系统的消息机制,要完全弄懂这个机制挺不容易的,但是我们可以按下述文字来理解它。 把WINDOWS操作系统看作人的大脑,它接收、处理、并发送各种各样的信息给我们的各个器官(当然是比喻各个应用程序了),也就是说它是消息的中枢。而每个应用程序(甚至每一个按钮、标签、窗体等等统称为窗口)在运行时都会被分配一个窗口过程WINDOWPROC,由这个窗口过程来接收和处理操作系统发来的消息(实际上存在一个消息队列),通常情况下这个窗口过程是由操作系统指定的,它会自动的响应并处理一些WINDOWS消息(如窗体移动、最大化、最小化、错误信息等)。好,到这我们先停一下,提出一个疑问,这些消息能否由我们自己写程序来处理呢?答案是肯定的,不过还得借助API函数的威力了,怎么用?我们还是先看看这些API函数的定义和参数吧。 程序中用到了SendMessage、CallWindowProc、SetWindowLong等API函数,其中SendMessage函数的作用是将一条消息发给某个窗口;CallWindowProc函数用来发送消息到一个窗口过程;而使用SetWindowLong函数来为窗口结构中为指定的窗口设置属性。使用API函数之前必须先在程序中声明如下: Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal HWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal HWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal HWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long 其中各参数的意义如下表: CallWindowProc函数 参数 意义 lpPrevWndFunc Long,原来的窗口过程地址 HWnd Long,窗口句柄 Msg Long,发送的消息 wParam Long,消息类型,参考wParam参数表 lParam Long,依据wParam参数的不同而不同 返回值 Long,依据发送的消息不同而变化 SetWindowLong函数 : 参数 意义 hwnd Long,欲为其取得信息的窗口的句柄 nIndex Long,请参考GetWindowLong函数的nIndex参数的说明 dwNewLong Long,由nIndex指定的窗口信息的新值 返回值 Long,指定数据的前一个值 SendMessage函数 : 参数 意义 hwnd Long,要接收消息的那个窗口的句柄 wMsg Long,消息的标识符 wParam Long,具体取决于消息 lParam Any,具体取决于消息 返回值 Long,由具体的消息决定 我们要自己写程序来处理消息,必须先更改窗口的属性,从原来由默认的窗口过程来处理消息变成由我们自己写的消息处理过程来处理消息。方法是使用SetWindowLong函数来取得默认窗口过程的地址,然后转向为我们自己写的窗口过程的地址,具体的实现方法如下代码: 'GWL_WNDPROC获得该窗口的窗口过程的地址,AddressOf是取址函数,NewWindowProc是我们写的过程 OldWindowProc = SetWindowLong(frm.HWnd, GWL_WNDPROC, AddressOf NewWindowProc) 然后在NewWindowProc函数中写入如下代码,需要注意的是下面代码中红色的TRAY_CALLBACK是由托盘区图标传来的消息,要让托盘图标传回消息,必须在添加托盘图标时指定: Public Function NewWindowProc(ByVal HWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long '如果用户点击了托盘中的图标,则进行判断是点击了左键还是右键 If Msg = TRAY_CALLBACK Then '如果点击了左键 If lParam = WM_LBUTTONUP Then '而这时窗体的状态是最小化时 If TheForm.WindowState = vbMinimized Then _ '恢复到最小化前的窗体状态 TheForm.WindowState = TheForm.LastState TheForm.SetFocus Exit Function End If End If '如果点击了右键 If lParam = WM_RBUTTONUP Then '则弹出右键菜单 TheForm.PopupMenu TheMenu Exit Function End If End If '如果是其他类型的消息则传递给原有默认的窗口函数 NewWindowProc = CallWindowProc(OldWindowProc, HWnd, Msg, wParam, lParam) End Function 这样我们就取得并处理了来自托盘图标的消息,现在的问题是在鼠标右键菜单弹出后,怎么控制程序主窗体的状态,这时我们需要用到SendMessage函数来向主窗体发送最大化、最小化、关 |