vb托盘程序代码(VB tray program code)

上传人:ca****in 文档编号:74768890 上传时间:2022-04-14 格式:DOC 页数:31 大小:60KB
返回 下载 相关 举报
vb托盘程序代码(VB tray program code)_第1页
第1页 / 共31页
vb托盘程序代码(VB tray program code)_第2页
第2页 / 共31页
vb托盘程序代码(VB tray program code)_第3页
第3页 / 共31页
亲,该文档总共31页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
vb托盘程序代码(VB tray program code)VB tray program Xiangjie flash flashMany software running in the system tray area (that is, the lower right corner of the desktop display area) a small icon, it is running a logo, we can use the small icon pop-up menu to control the state of the application. This case is given a tray program function relatively complete, we can see how to add, delete, change tray icon with the API Shell_NotifyIcon function; and patients also demonstrated a method to add the menu for the tray icon and floating tips.The program (attached) used in the Shell_NotifyIcon, SendMessage, CallWindowProc, SetWindowLong, API function, Shell_NotifyIcon is the main function of it is used to add, delete, change the system tray area (taskbar status area) icon, so lets take a look at the function declaration and parameters:Before you use the API function, you must declare in the program as follows:Declare, Function, Shell_NotifyIcon, Lib, shell32.dll, Alias, Shell_NotifyIconA, ByVal, As, dwMessage, Long, As, NOTIFYICONDATA, lpData, As, LongThe meaning of each parameter is as follows:Argument: meaningDwMessage sets the value for the message, which can be several constant values: 0, 1, and 2NIM_ADD = 0 adds icons to the system status barNIM_MODIFY = 1 modifies the icons in the system status barNIM_DELETE = 2 deletes the icon in the system status barLpData is used to pass into the NOTIFYICONDATA data structure variable, as follows:Type NOTIFYICONDATACbSize As Long needs to fill in the length of the NOTIFYICONDATA data structureHWnd As Long sets the handle to the windowThe ID value set by the Uid As Long for the iconThe UFlags As Long sets uCallbackMessage, hIcon, and szTip to be validUCallbackMessage As Long message numberHIcon As Long displays icons on the status barSzTip As String * 64 prompt informationEnd TypeThe return value is Long, nonzero means success, and zero means failureBefore using the API function, we should first define the structure type NOTIFYICONDATA:Public Type NOTIFYICONDATACbSize, As, Long, HWnd, As, LongUid, As, Long, UFlags, As, LongUCallbackMessage As LongHIcon As LongSzTip, As, String * 64End TypeThen define a NOTIFYICONDATA variable TheData to record the data that sets the tray iconPrivate TheData As NOTIFYICONDATAAt this point, you can use this function to set the system tray icon, as follows:1, add IconWith TheData.Uid = 0.HWnd = frm.HWndfrm.HWnd is the handle to the programs main form.cbSize = Len (TheData).HIcon = frm.Icon.Handlefrm.Icon.Handle points to the icon of the main form.UFlags = NIF_ICON.UCallbackMessage = TRAY_CALLBACKThe role is to allow the return message to be explained in detail in the next section.UFlags =.UFlags, Or, NIF_MESSAGE.cbSize = Len (TheData)End WithShell_NotifyIcon, NIM_ADD, TheDataas defined earlier, NIM_ADD is set to add mode , and then add2. Delete the iconWith TheData.UFlags = 0End WithShell_NotifyIcon, NIM_DELETE, TheDataas defined earlier, NIM_DELETE is set to delete mode3, change iconWith TheData.HIcon = pic.Handlepic is a picture maniac PictureBox, which stores icon files.UFlags = NIF_ICONEnd WithShell_NotifyIcon, NIM_MODIFY, TheDataas defined earlier, NIM_MODIFY is set to change mode4, add floating hint information for iconsWith TheData.SzTip = tip & vbNullChartip is string string, store prompt information.UFlags = NIF_TIPIndicates that the floating prompt should be setEnd WithShell_NotifyIcon, NIM_MODIFY, TheDataas defined earlier, NIM_MODIFY is set to modify modeThrough the above several sections of code, we can add, delete, change the system tray icon according to our own needs, and add the floating hint information on the system icon. But the tray icon at this time is isolated and we cant use it to control the behavior of the application. What can I do? Dont worry. Look down, please.If you download and run this example program, youll see that if we right-click on the tray icon, a right-click menu pops up. If you click the corresponding menu item, the main window of the program changes along with it, so you can control the behavior of the program. If the main form is minimized, we click the left button on the tray icon, and the form will revert to its original size. In fact, the implementation of the above functions relies on the messaging mechanism of the WINDOWS operating system, and it is not easy to fully understand the mechanism, but we can understand it as follows.Think of the WINDOWS operating system as a human brain,It receives, processes, and sends various kinds of information to our organs (of course, metaphor for each application), that is, it is the center of the message. Each application (or even every buttons, labels, etc. are collectively referred to as the form window) are assigned a WINDOWPROC window procedure at run time, and sent to the operating system by the window to process the received message (in fact there is a message queue), this window is usually specified by the the operating system, it will automatically respond and handle WINDOWS messages (such as mobile form, maximize and minimize the error information, etc.). OK, so lets stop here and ask a question. Can we write the news to ourselves? The answer is yes, but you have to use the power of the API function. How do you use it? Lets look at the definitions and parameters of these API functions first.Procedures used in the SendMessage, CallWindowProc, SetWindowLong and API function, the SendMessage function will be a message sent to a window; the CallWindowProc function to send a message to a window procedure; and use the SetWindowLong function to the window structure for the specified window setting properties. Before you use the API function, you must declare in the program as follows:Declare, Function, CallWindowProc, Lib, user32, Alias, CallWindowProcA, ByVal, As, Long, ByVal, HWnd, As, Long, ByVal, Msg, As, Long, ByVal, wParam, As, Long, ByVal, lParam, As, Long, lpPrevWndFunc, As, LongDeclare, Function, SetWindowLong, Lib, user32, Alias, SetWindowLongA (ByVal, HWnd, As, Long, ByVal, nIndex, As, Long, ByVal, dwNewLong, As, Long), As, LongPrivate, Declare, Function, SendMessage, Lib, user32, Alias, SendMessageA, ByVal, As, Long, ByVal, wMsg, As, Long, ByVal, wParam, As, Long, lParam, As, Any, HWnd, As, LongThe meaning of each parameter is as follows:CallWindowProc functionParameter meaningLpPrevWndFunc Long, the original window procedure addressHWnd Long, window handleMsg Long, messages sentWParam Long, message type, refer to the wParam parameter tableLParam Long is different depending on the wParam parameterThe return value, Long, varies depending on the message being sentSetWindowLong function:Parameter meaningHWnd Long, the handle to the window that you want to obtain information forNIndex Long, refer to the description of the nIndex parameter of the GetWindowLong functionDwNewLong Long, the new value of the window information specified by nIndexThe return value Long specifies the previous value of the dataSendMessage function:Parameter meaningHWnd Long, the handle of the window to receive the messageWMsg Long, the identifier of the messagewparam long, 具体取决于消息lparam (具体取决于消息返回值 long, 由具体的消息决定我们要自己写程序来处理消息, 必须先更改窗口的属性, 从原来由默认的窗口过程来处理消息变成由我们自己写的消息处理过程来处理消息.方法是使用setwindowlong函数来取得默认窗口过程的地址, 然后转向为我们自己写的窗口过程的地址, 具体的实现方法如下代码:gwl _ wndproc获得该窗口的窗口过程的地址, addressof是取址函数, newwindowproc是我们写的过程oldwindowproc = setwindowlong (frm.hwnd, gwl _ wndproc, addressof newwindowproc)然后在newwindowproc函数中写入如下代码, 需要注意的是下面代码中红色的tray _ callback是由托盘区图标传来的消息, 要让托盘图标传回消息, 必须在添加托盘图标时指定:public function newwindowproc (byval hwnd byval msg as long, as long, as long byval wparam, byval lparam as long) as long如果用户点击了托盘中的图标, 则进行判断是点击了左键还是右键if msg = callback then _ tray如果点击了左键if lparam = wm _ lbuttonup then而这时窗体的状态是最小化时if theform.windowstate = vbminimized then _恢复到最小化前的窗体状态theform.windowstate = theform.laststatetheform.setfocusexit functionend ifend if如果点击了右键if lparam = wm _ rbuttonup then则弹出右键菜单theform.popupmenu themenuexit functionend ifend if如果是其他类型的消息则传递给原有默认的窗口函数newwindowproc = callwindowproc (oldwindowproc, hwnd, msg, wparam, lparam)end function这样我们就取得并处理了来自托盘图标的消息, 现在的问题是在鼠标右键菜单弹出后, 怎么控制程序主窗体的状态, 这时我们需要用到sendmessage函数来向主窗体发送最大化、最小化、关闭、移动等消息, 具体的代码实现如下, 其中hwnd是主窗体的句柄, wm _ syscommand表示发送的是系统控制类的消息,SC_MOVE, SC_SIZE, and SC_RESTORE are messages to be sent:Tray icon right-click menu when the move item is clickedPrivate, Sub, mnuTrayMove_Click ()SendMessage, HWnd, WM_SYSCOMMAND, SC_MOVE, 0&End Subtray icon when the recovery item on the right-click menu is clickedPrivate, Sub, mnuTrayRestore_Click ()SendMessage, HWnd, WM_SYSCOMMAND, SC_RESTORE, 0&End SubTray icon right click menu when the exit item is clickedPrivate, Sub, mnuTraySize_Click ()SendMessage, HWnd, WM_SYSCOMMAND, SC_SIZE, 0&End SubFinally, I want to remind you that when you exit the program, you must restore the address of the window process to the default value and remove the tray icon at the same timeHere is the source code for learning convenience:use system tray program demoProgram description:this is a relatively complete example of using system tray, includingNow: add tray icon, remove tray icon, change tray icon dynamically,Add floating tooltip information for tray icons to achieve the right mouse button of the tray iconmenu, etc.- name - - roleForm1 main formmnuFile, mnuFileExit File menu, menu itemmnuTray, mnuTrayClose. Tray area, right-click menu, menu itemOption ExplicitThe role of theLastState variable is to indicate the original state of the primary formPublic LastState As IntegerVB statementPrivate, Declare, Function, SendMessage, Lib, user32, Alias, SendMessageA (ByVal, hWnd, As, Long, ByVal, wMsg, As, Long, ByVal, wParam, As, Long, lParam, As, Any) As, Longinstructionscall a window function of a window and send a message to that window. The function does not return unless the message is processed. SendMessageBynum,SendMessageByString is the type security declaration form of this functionreturn valueLong is decided by specific messagesparameter tablehWnd - Long, the window handle to the received messageWMsg - Long, message identifierwParam - Long, depending on the newslParam - Any, depending on the newsPrivate, Declare, Function, SendMessage, Lib, user32, Alias, SendMessageA, ByVal, As, Long, ByVal, wMsg, As, Long, ByVal, wParam, As, Long, lParam, As, Any, HWnd, As, LongIndicates that the system command is sentPrivate, Const, WM_SYSCOMMAND = &H112private const sc _ move = & hf010 &private const sc _ restore = & hf120 &private const sc _ size = & hf000 &当主窗体加载时private sub form _ load ()窗体的windowstate属性, 返回或设置一个值, 该值用来指定在运行时窗体窗口的可视状态vbnormal 0 (缺省值) 正常.vbminimized 1 最小化 (最小化为一个图标)vbmaximized 2 最大化 (扩大到最大尺寸)if windowstate = vbminimized thenlaststate = vbnormalelselaststate = windowstateend if将图标添加到托盘的函数, 参见模块中的解释注意了这是从主程序到模块的入口, 本例中并没有直接调用shell _ notifyicon函数addtotray me, mnutraysettraytip 托盘图标演示, 点击右键弹出菜单end sub在主窗体form1大小改变时, 相应改变右键菜单mnutray的菜单项的可用属性enabledprivate sub form _ resize ()select case windowstate如果窗体最小化了, 把菜单项 最大化 恢复 设为可用,而把 最小化 移动 大小 三项设为不可用.如果这时在托盘图标上点击鼠标右键, 会发现不可用项变为灰色case vbminimizedmnutraymaximize. enabled = truemnutrayminimize. enabled = falsemnutraymove. enabled = falsemnutrayrestore. enabled = truemnutraysize. enabled = false窗体最大化时case vbmaximizedmnutraymaximize. enabled = falsemnutrayminimize. enabled = truemnutraymove. enabled = falsemnutrayrestore. enabled = truemnutraysize. enabled = false一般状态下case vbnormalmnutraymaximize. enabled = truemnutrayminimize. enabled = truemnutraymove. enabled = truemnutrayrestore. enabled = falsemnutraysize. enabled = trueend selectif windowstate vbminimized then laststate = windowstateend sub保证在程序退出时删除托盘图标private sub form _ unload (cancel as integer)removefromtrayend sub文件 菜单的 退出 项被点击时private sub mnufileexit _ click ()unload meend sub托盘图标右键菜单上的 退出 项被点击时private sub mnutrayclose _ click ()unload meend sub托盘图标右键菜单上的 最大化 项被点击时私有子mnutraymaximize_click()vbmaximized WindowState =端子“托盘图标右键菜单上的”最小化”项被点击时私有子mnutrayminimize_click()vbminimized WindowState =端子“托盘图标右键菜单上的”移动”项被点击时私有子mnutraymove_click()SendMessage HWnd,wm_syscommand,_sc_move,0端子“托盘图标右键菜单上的”恢复”项被点击时私有子mnutrayrestore_click()SendMessage HWnd,wm_syscommand,_sc_restore,0端子“托盘图标右键菜单上的”退出”项被点击时私有子mnutraysize_click()SendMessage HWnd,wm_syscommand,_sc_size,0端子“以下为模块中的代码:明确选择市民OldWindowProc As Long公开的形式形式公共菜单作为菜单“【VB声明】声明函数CallWindowProc lib“user32”别名“callwindowproca”(ByVal lpPrevWndFunc为长,ByVal HWnd为长,ByVal Msg为长,ByVal wParam为长,ByVal lParam为长)长“【说明】“此函数发送消息到一个窗口过程“【返回值】长,依据发送的消息不同而变化“【参数表】“lpprevwndfunc -长,原来的窗口过程地址“窗口-长,窗口句柄“味精-长,发送的消息“得-长,消息类型,参考wParam参数表“指向-长,依据wParam参数的不同而不同声明函数CallWindowProc lib“user32”别名“callwindowproca”(ByVal lpPrevWndFunc为长,ByVal HWnd为长,ByVal Msg为长,ByVal wParam为长,ByVal lParam为长)长“【VB声明】“私人声明函数SetWindowLong lib“user32”别名“setwindowlonga”(ByVal hwnd为长,ByVal nIndex为长,长,长dwnewlong ByVal)“【说明】“在窗口结构中为指定的窗口设置信息“【返回值】长,指定数据的前一个值“【参数表】“窗口-长,欲为其取得信息的窗口的句柄“指定-长,请参考getwindowlong函数的nIndex参数的说明“dwnewlong -长,由nIndex指定的窗口信息的新值声明函数SetWindowLong lib“user32”别名“setwindowlonga”(ByVal HWnd为长,ByVal nIndex为长,长,长dwnewlong ByVal)“【VB声明】声明函数库”shell_notifyicon shell32. dll“别名”shell_notifyicona”(ByVal dwMessage为长,lpData为NOTIFYICONDATA)长“【说明】“【参数表】“参数dwmessage -为消息设置值,它可以是以下的几个常数值:0、1、2“nim_add = 0加入图标到系统状态栏中“nim_modify = 1修改系统状态栏中的图标“nim_delete = 2删除系统状态栏中的图标“参数lpData -用以传入NOTIFYICONDATA数据结构变量,我们也需要在”模块”中定义其结构如下:型NOTIFYICONDATA“只要需填入NOTIFYICONDATA数据结构的长度需“窗口句柄设置成窗口的句柄“只要为图标所设置的ID值UID“只要用来设置以下三个参数uFlags ucallbackmessage、惠康、sztip是否有效“ucallbackmessage只要消息编号“做为长显示在状态栏上的图标“sztip字符串的长度为64提示信息端型“-其中参数ucallbackmessage、惠康、sztip也应在模块中声明为以下的常量:公共建设nif_message = 1公共建设nif_icon = 2公共建设nif_tip = 4函数声明shell_notifyicon lib”shell32. dll“别名”shell_notifyicona”(ByVal dwMessage为长,lpData为NOTIFYICONDATA)长公共建设wm_user = & H400公共建设wm_lbuttonup =和H202公共建设wm_mbuttonup =和h208公共建设wm_rbuttonup = & H205公共建设tray_callback =(wm_user + 1001)公共建设gwl_wndproc =(- 4)公共建设gwl_userdata =(- 21)公共建设nif_icon =和H2公共建设nif_tip =和H4公共建设nim_add = & H0公共建设nif_message =和H1公共建设nim_modify =和H1公共建设nim_delete =和H2“记录设置托盘图标的数据的数据类型NOTIFYICONDATA公共型NOTIFYICONDATA需长窗口句柄UID长uFlags长ucallbackmessage长做为长sztip字符串的长度为64端式“数据变量记录设置托盘图标的数据private thedata as notifyicondatais 新的窗口过程 - 主程序中采用setwindowlong函数改变了窗口函数的地址, 消息转向由newwindowproc处理public function newwindowproc (byval hwnd as long, byval msg as long, byval wparam as long, byval lparam as long) as longis 如果用户点击了托盘中的图标, 则进行判断是点击了左键还是右键if msg = tray _ callback thenis 如果点击了左键if lparam = wm _ lbuttonup thenis 而这时窗体的状态是最小化时if theform.windowstate = vbminimized then _is 恢复到最小化前的窗体状态theform.windowstate = theform.laststatetheform.setfocusexit functionend ifend ifis 如果点击了右键if lparam = wm _ rbuttonup thenis 则弹出右键菜单theform.popupmenu themenuexit functionend ifend ifis 如果是其他类型的消息则传递给原有默认的窗口函数newwindowproc = callwindowproc (oldwindowproc, hwnd, msg, wparam, lparam)end functionis 把主窗体的图标 (form1.icon属性可改变) 添加到托盘中public sub addtotray (frm as form, mnu as menu)is 保存当前窗体和菜单信息set theform = frmset themenu = mnuis gwl _ wndproc获得该窗口的窗口函数的地址oldwindowproc = setwindowlong (frm.hwnd, gwl _ wndproc, addressof newwindowproc)is 知识点滴: hwnd属性is 返回窗体或控件的句柄.语法: object.hwndis 说明: microsoft windows 运行环境, 通过给应用程序中的每个窗体和控件is 分配一个句柄 (或 hwnd) 来标识它们.hwnd 属性用于windows api调用.is 将主窗体图标添加在托盘中with thedata. uid = 0 is 忘了吗? 参考一下前面内容, uid图标的序号, 做动画图标有用.hwnd = frm.hwnd.cbsize = len (thedata).hicon = frm.icon.handle.uflags = nifs 指明要对图标进行设置 _ icon.ucallbackmessage = tray _ callback.uflags =.uflags or nif _ message is 指明要设置图标或返回信息给主窗体, 此句不能省去.cbsize = len (thedata) 为什么呢 我们需要在添加图标的同时, 让其返回信息?end with 给主窗体, or的意思是同时进行设置和返回消息shell _ notifyicon nim _ add, thedatas 根据前面定义nim _ add, 设置为 添加模式end subis 删除系统托盘中的图标Public, Sub, RemoveFromTray ()remove the icons in the trayWith TheData.UFlags = 0End WithShell_NotifyIcon NIM_DELETE, TheData , as defined earlier, NIM_DELETE, set to delete modeRestore the original settingsSetWindowLong, TheForm.HWnd, GWL_WNDPROC, OldWindowProcEnd SubAdd a floating hint for the icon in the tray (that is, a note when the mouse is moving up)Public, Sub, SetTrayTip (tip, As, String)With TheData.SzTip = tip & vbNullChar.UFlags = NIF_TIP indicates that the floating prompt is setEnd WithShell_NotifyIcon NIM_MODIFY, TheData , as defined earlier, NIM_MODIFY is set to modify modeEnd Subset the tray icon (not used in this case, if its dynamically changing the icon displayed in the tray), its very useful)For example: 1, display animation icon (method you must have guessed, yes! Using the Timer control, keep calling the process, and keep the animation in the pic arrayWhen 2 and programs are in different states, different icons are displayed, and the methods are similarIf youre interested, give it a try.Public, Sub, SetTrayIcon (PIC, As, Picture)To determine whether the icons are stored in the picIf pic.Type vbPicTypeIcon Then Exit Sub Change the icon to the icon stored in picWith TheData.HIcon = pic.Handle.UFlags = NIF_ICONEnd WithShell_NotifyIcon, NIM_MODIFY, TheDataEnd Sub
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 大学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!