vb实现托盘(含程序)

合集下载

VB.NET编写托盘程序

VB.NET编写托盘程序

编写托盘程序
编写托盘程序
托盘程序作为一类特殊的窗体,其快捷图标显示在系统托盘中,窗体本身则隐藏不可见。

在.NET之前版本的VB中编写托盘程序是十分困难的,但是提供的新的NotifyIcon组件却使VB初学者也能轻松编写一个这样的程序:
新建“Windows应用程序”,设置主窗体Opacity属性为0,FormBorderStyle属性为None,ShowInTaskbar属性为False,这样窗体将在启动后隐藏。

在窗体上放置一个NotifyIcon组件NotifyIcon1,一个ContextMenu(弹出菜单)组件 ContextMenu1,并根据需要为ContextMenu1添加菜单项。

设置NotifyIcon1的ICON属性,这个图标就是应用程序出现在系统托盘中的快捷图标;设置NotifyIcon1的Text属性为“ 托盘程序”,这就是鼠标移动到托盘图标时弹出的文字说明;设置NotifyIcon1的ContextMenu属性为ContextMenu1,也就是右键单击快捷图标时的弹出菜单为 ContextMenu1。

OK,按F5运行!
几乎不用编写代码,一个托盘程序就这样轻松实现了。

用VisualC#做托盘程序.net-电脑资料

用VisualC#做托盘程序.net-电脑资料

用VisualC#做托盘程序.net-电脑资料下一页1 2 3 所谓托盘程序顾名思义就是象托起的盘子一样的程序,。

而所谓的托起的盘子就是程序运行中显示出的图标,而托起的位置就是视窗系统的的工具栏了。

托盘程序具有直观、占用屏幕空间较小并且可以为它定义多个功能菜单,这就给操作者带来了方便,所以下一页 1 2 3所谓托盘程序顾名思义就是象托起的盘子一样的程序。

而所谓的托起的盘子就是程序运行中显示出的图标,而托起的位置就是视窗系统的的工具栏了。

托盘程序具有直观、占用屏幕空间较小并且可以为它定义多个功能菜单,这就给操作者带来了方便,所以越来越多的程序设计者都把程序设计成托盘这种方式。

我们已经看过了用其他语言设计托盘程序的例子,其中的大部分,整个设计过程还是相对烦琐的。

而对于微软公司极力推荐的下一代程序开发语言--Visual C#来说,却可以十分方便设计出一个托盘程序。

本文就是介绍Visual C#设计托盘程序的具体过程。

首先来介绍一下本文中设计托盘程序所需要的环境:(1).微软公司视窗2000服务器版(2) FrameWork SDK Beta 2一.托盘程序的主要步骤及解决方法:为什么说用Visual C#可以十分方便的做一个托盘程序,主要的原因是在.Net框架的软件开发包( .Net FrameWork SDK )中的WinForm 组件中定义了一个专门用来开发托盘程序的组件--NotifyIcon组件,电脑资料《用Visual C#做托盘程序.net》(https://www.)。

下面就来介绍一下这个组件的具体用法和程序设计中的主要的技巧。

(1).如何在程序运行后隐藏窗体:我们知道托盘程序运行后是无法看见主窗体的,他只会显示在工具栏上。

在用Visual C#设计此类程序的时候,可以用二种方法使得程序运行后不显示主窗体。

其中一种方法是重载主窗体中的OnActivated( )事件,OnActivated( )事件是在窗体激活的时候才触发的。

托盘程序

托盘程序

//窗体的后台写private wf.NotifyIcon notifyIcon = null;public MainWi ndow(){InitializeComponent();InitialTray();}private void InitialTray(){this.Visibility = Visibility.Visible;notifyIcon = new wf.NotifyIcon();notifyIcon.BalloonTipText = "我的托盘应用程序正在运行。

";notifyIcon.Text = "浪潮ERP服务云平台";notifyIcon.Icon = new System.Drawing.Icon("SkinBox.ico"); //pack://application:,,,/Resources/Safe.ico 这时候在debug文件夹下加入Safe.iconotifyIcon.Visible = true;notifyIcon.ShowBalloonTip(200);notifyIcon.MouseClick += new System.Wi ndows.Forms.MouseEventHandler(notifyIcon_MouseClick);wf.MenuItem mSetting_one = new wf.MenuItem("皮肤");wf.MenuItem mSetting_two = new wf.MenuItem("皮肤");wf.MenuItem mSetting_three = new wf.MenuItem("设置", new wf.MenuItem[] { mSetting_one,mSetting_two });wf.MenuItem help = new wf.MenuItem("帮助");wf.MenuItem about = new wf.MenuItem("关于");wf.MenuItem exit = new wf.MenuItem("退出");exit.Click += new EventHandler(exit_Click);wf.MenuItem[] childen = new wf.MenuItem[] { mSetting_three, help, about, exit };notifyIcon.ContextMenu = new wf.ContextMenu(childen);this.StateChanged += new EventHandler(Mai nWindow_StateChanged);//this.StateChanged += new RoutedEventHandler(mniButton_Click);}private void MainWindow_StateChanged(object sender, EventArgs e){if (this.WindowState == Wi ndowState.Minimized){this.Visibility = Visibility.Hidden;}}private void exit_Click(object sender, EventArgs e){if (MessageBox.Show("你确定要退出程序吗?", "系统提示", MessageBoxButton.YesNo, MessageBoxImage.Questi on, MessageBoxResult.No) == MessageBoxResult.Yes) {Application.C urrent.Shutdown();}}private void notifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e) {if (e.Button == wf.MouseButtons.Left){if (this.Visibility == Visibility.Hidden){this.Visibility = Visibility.Visible;this.Activate();}}}。

怎么能让VB 程序最小化时在托盘显示呀急急急!~希望VB高手帮忙!

怎么能让VB 程序最小化时在托盘显示呀急急急!~希望VB高手帮忙!

给你个例子吧建立一个frmmain主窗体Option ExplicitPrivate Sub Form_Load()'初始化imgIcon控件的Picture属性imgIcon.Picture = LoadPicture(App.Path & "\" & "heart.ico") mnuTrayShow.Enabled = False'初始化NOTIFYICONDATA数据结构Dim nid As NOTIFYICONDATAnid.cbSize = Len(nid)nid.hwnd = frmTemp.hwndnid.uId = 1&nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE nid.uCallbackMessage = WM_NOTIFYICONnid.hicon = imgIcon.Picturenid.szTip = "系统托盘中的图标" & Chr(0)'调用Shell_NotifyIcon函数将图标加入到系统托盘中Shell_NotifyIcon NIM_ADD, nidEnd SubPrivate Sub Form_Unload(Cancel As Integer)'设置自定义类型的变量的内容Dim nid As NOTIFYICONDATAnid.cbSize = Len(nid)nid.hwnd = frmTemp.hwndnid.uId = 1&nid.uFlags = 0'调用Shell_NotifyIcon方法来删除系统托盘中的图标Shell_NotifyIcon NIM_DELETE, nidUnload frmTempEnd SubPrivate Sub mnuTrayExit_Click()Unload MeEnd SubPrivate Sub mnuTrayHide_Click()'隐藏窗体,并相应改变菜单项的状态Me.HidemnuTrayHide.Enabled = FalsemnuTrayShow.Enabled = TrueEnd SubPrivate Sub mnuTrayIcon_Click()'设置dlgOpen公用对话框的属性,以便取得相应的图标文件dlgOpen.Filter = "图标文件(*.ico)|*.ico"dlgOpen.Flags = cdlOFNFileMustExistdlgOpen.ShowOpenIf dlgOpen.FileName = "" Then Exit Sub'根据所取得图标文件,来改变imgIcon文件的属性imgIcon.Picture = LoadPicture(dlgOpen.FileName)'设置NOTIFYICONDATA数据结构Dim nid As NOTIFYICONDATAnid.cbSize = Len(nid)nid.hwnd = frmTemp.hwndnid.uId = 1&nid.uFlags = NIF_ICONnid.hicon = imgIcon.Picture'调用Shell_NotifyIcon函数来修改系统托盘中的图标Shell_NotifyIcon NIM_MODIFY, nidEnd SubPrivate Sub mnuTrayShow_Click()'显示窗体,并相应改变菜单项的状态Me.ShowmnuTrayHide.Enabled = TruemnuTrayShow.Enabled = FalseEnd SubPrivate Sub mnuTrayTip_Click()Dim str As Stringstr = InputBox("输入系统托盘中的图标的提示信息:", "提示信息") If str = "" Then Exit Sub'设置NOTIFYICONDATA数据结构Dim nid As NOTIFYICONDATAnid.cbSize = Len(nid)nid.hwnd = frmTemp.hwndnid.uId = 1&nid.uFlags = NIF_TIPnid.szTip = str & Chr(0)'调用Shell_NotifyIcon函数来修改系统托盘中的图标的提示Shell_NotifyIcon NIM_MODIFY, nidEnd Sub再建立一个空窗体叫frmTemp再建一个模块,名字随便了Option Explicit'声明Shell_NotifyIcon函数,用于将图标加入到系统托盘中Public Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean'声明自定义的数据类型NOTIFYICONDATAPublic Type NOTIFYICONDATAcbSize As Longhwnd As LonguId As LonguFlags As LonguCallbackMessage As Longhicon As LongszTip As String * 64End Type'声明常量Public Const NIM_ADD = &H0Public Const NIM_MODIFY = &H1Public Const NIM_DELETE = &H2Public Const NIF_MESSAGE = &H1Public Const NIF_ICON = &H2Public Const NIF_TIP = &H4'定义SetWindowLong,用以改变frmTemp窗体的窗体函数Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long'定义事件的常数代码Public Const WM_LBUTTONDOWN = &H201Public Const WM_RBUTTONDOWN = &H204Public Const WM_USER = &H400'定义自定义的事件WM_NOTIFYICONPublic Const WM_NOTIFYICON = WM_USER + &H100'替换窗口处理函数Public Const GWL_WNDPROC = (-4)'该函数为frmTest的窗体处理函数。

利用Visual C++实现系统托盘程序

利用Visual C++实现系统托盘程序

利用Visual C++实现系统托盘程序这些程序运行时不显示运行窗口,只在任务栏上显示一个图标,表示程序正在运行,用户可以通过鼠标与应用程序交互,程序开发人员有时也需要编制一些仅在后台运行的类似程序,为了不干扰前台程序的运行界面和不显示不必要的窗口,应使程序运行时的主窗口不可见。

同时将一个图标显示在任务栏右端静态通告区中并响应用户的鼠标动作。

本实例就介绍Visual C++开发这类程序的设计方法,该程序编译运行后,如果双击托盘图标,程序会弹出一个消息列表窗口,只要鼠标在托盘图标上移动或点击(无论是左右键的单击或双击),产生的消息都会显示在这个窗口里;当鼠标光标移到托盘图标上时,在图标附近会显示提示信息;单击右键时弹出上下文菜单,这个菜单中应包含打开属性页的命令或者打开与图标相关的其它窗口的命令,另外,该程序还可以动态的改变托盘的图标。

参照这个例子,相信读者能轻松自如地在自己的程序中应用系统托盘。

一、实现方法为了实现拖盘程序,首先要使程序的主窗口不可见,这点实现起来十分容易,只要调用ShowWindow(SW_HIDE)就可以了,本实例采用的就是这种方法,还有一种思路是通过分别设置主边框窗口的风格和扩展风格来隐藏主框架:在任务条上显示图标是利用系统API函数Shell_NotifyIcon()来将一个图标显示在任务栏的通告区中。

该函数的原型为:该函数的第一个参数dwMessage类型为DWORD,表示要进行的动作,它可以是下面的值之一:NIM_ADD:添加一个图标到任务栏。

NIM_MODIFY:修改状态栏区域的图标。

NIM_DELETE:删除状态栏区域的图标。

NIM_SETFOCUS:将焦点返回到任务栏通知区域。

当完成用户界面操作时,任务栏图标必须用此消息。

例如,如果任务栏图标正显示上下文菜单,但用户按下"ESCAPE"键取消操作,这时就必须用此消息将焦点返回到任务栏通知区域。

VB实现在窗口的标题栏上添加一个按钮的功能实现程序最小化到系统托盘

VB实现在窗口的标题栏上添加一个按钮的功能实现程序最小化到系统托盘

程序运行窗口在窗口的标题栏上添加了一个按钮,实现最小化到系统托盘右键菜单1、复制以下程序段到记事本中另存为文件:Type=ExeReference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\WINDOWS\system32\std ole2.tlb#OLE AutomationModule=TrayStartup="frmMain"HelpFile=""ExeName32="Project1.exe"Path32="..\..\..\..\..\..\WINDOWS\Desktop"Command32=""Name="Project1"HelpContextID="0"CompatibleMode="0"MajorVer=1MinorVer=0RevisionVer=0AutoIncrementVer=0ServerSupportFiles=0VersionCompanyName="None"CompilationType=0OptimizationType=0FavorPentiumPro(tm)=0CodeViewDebugInfo=0NoAliasing=0BoundsCheck=0OverflowCheck=0 FlPointCheck=0 FDIVCheck=0 UnroundedFP=0 StartMode=0 Unattended=0 Retained=0 ThreadPerObject=0 MaxNumberOfThreads=1[MS Transaction Server] AutoRefresh=1Begin VB.Form frmMainAutoRedraw = -1 'TrueCaption = "TitleBar Tray Button Demo"ClientHeight = 2040ClientLeft = 60ClientTop = 345ClientWidth = 4680LinkTopic = "Form1"ScaleHeight = 2040ScaleWidth = 4680StartUpPosition = 3 '窗口缺省Begin VB.Menu mnuPopUpCaption = ""Visible = 0 'FalseBegin VB.Menu mnuRestoreCaption = "Restore"EndEndEndAttribute VB_Name = "frmMain"Attribute VB_GlobalNameSpace = FalseAttribute VB_Creatable = FalseAttribute VB_PredeclaredId = TrueAttribute VB_Exposed = FalsePrivate Sub Form_Load()Print "Right Click For Menu"Me.ScaleMode = vbPixels 'The API works in pixelsHook Me 'FormHook Hook()End SubPrivate Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)If Button = 2 Then TrayMenu Me 'TrayNotify TrayMneu()End SubPrivate Sub Form_Unload(Cancel As Integer)UnHook 'FormHook UnHook()End SubAttribute VB_Name = "ToolTip"Const WS_EX_TOPMOST = &H8&Const TTS_ALWAYSTIP = &H1Const HWND_TOPMOST = -1Const SWP_NOACTIVATE = &H10Const SWP_NOMOVE = &H2Const SWP_NOSIZE = &H1Const WM_USER = &H400Const TTM_ADDTOOLA = (WM_USER + 4)Const TTF_SUBCLASS = &H10Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long 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 LongPublic Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongType TOOLINFOcbSize As LonguFlags As Longhwnd As Longuid As LongRECT As RECThinst As LonglpszText As StringlParam As LongEnd TypePublic hWndTT As LongPublic Sub CreateTip(hwndForm As Long, szText As String, rct As RECT)hWndTT = CreateWindowEx(WS_EX_TOPMOST, "tooltips_class32", "",TTS_ALWAYSTIP, _0, 0, 0, 0, hwndForm, 0&, App.hInstance, 0&)SetWindowPos hWndTT, HWND_TOPMOST, 0, 0, 0, 0, _SWP_NOMOVE Or SWP_NOSIZE Or SWP_NOACTIVATEDim TI As TOOLINFOWith TI.cbSize = Len(TI).uFlags = TTF_SUBCLASS.hwnd = hwndForm.uid = 1&.lpszText = szText & vbNullChar.RECT = rctEnd WithSendMessage hWndTT, TTM_ADDTOOLA, 0, TIEnd SubPublic Sub KillTip()DestroyWindow hWndTTEnd Sub4、复制以下程序段到记事本中另存为文件:Attribute VB_Name = "DrawButton"Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long Declare Function DrawFrameControl Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal un1 As Long, ByVal un2 As Long) As LongDeclare Function GetTitleBarInfo Lib "user32" (ByVal hwnd As Long, pti As TitleBarInfo) As BooleanDeclare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As LongDeclare Function GetSysColorBrush Lib "user32" (ByVal nIndex As Long) As LongDeclare Function OffsetRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As LongType RECTLeft As LongTop As LongRight As LongBottom As LongEnd TypeType TitleBarInfocbSize As LongrcTitleBar As RECT 'A RECT structure that receives the coordinates of the title barrgState(5) As Long 'An array that receives a DWORD value for each element of the title barEnd Type'rgState array Values'0 The titlebar Itself'1 Reserved'2 Min button'3 Max button'4 Help button'5 Close button''rgstate return constatnts'STATE_SYSTEM_FOCUSABLE = &H00100000'STATE_SYSTEM_INVISIBLE = &H00008000'STATE_SYSTEM_OFFSCREEN = &H00010000'STATE_SYSTEM_PRESSED = &H00000008'STATE_SYSTEM_UNAVAILABLE = &H00000001Const DFC_BUTTON = 4Const DFCS_BUTTONPUSH = &H10Const DFCS_PUSHED = &H200Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As LongPublic Declare Function PtInRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As LongPublic Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As LongPublic Type POINTAPIx As Longy As LongEnd TypeConst SM_CXFRAME = 32Const COLOR_BTNTEXT = 18Dim lDC As LongPublic R As RECTPublic Sub ButtonDraw(frm As Form, bState As Boolean)Dim TBButtons As IntegerDim TBarHeight As IntegerDim TBButtonHeight As IntegerDim TBButtonWidth As IntegerDim DrawWidth As IntegerDim TBI As TitleBarInfoDim TBIRect As RECTDim bRslt As BooleanDim WinBorder As IntegerWith frmIf .BorderStyle = 0 Then Exit Sub ' Don't draw a button if there is no titlebar'----How Many Buttons in TitleBar------------------------------------------If Not .ControlBox Then TBButtons = 0If .ControlBox Then TBButtons = 1If .ControlBox And .WhatsThisButton ThenIf .BorderStyle < 4 ThenTBButtons = 2ElsetButtons = 1End IfEnd IfIf .ControlBox And .MinButton And .BorderStyle = 2 Then TBButtons = 3If .ControlBox And .MinButton And .BorderStyle = 5 Then TBButtons = 1If .ControlBox And .MaxButton And .BorderStyle = 2 Then TBButtons = 3If .ControlBox And .MaxButton And .BorderStyle = 5 Then TBButtons = 1'------------------------------------------------------------------------'----Get height of Titlebar----------------------------------------------'Using this method gets the height of the titlebar regardless of the window'style. It does, however, restrict its use to Win98/2000. So if you want to'use this code in Win95, then call GetSystemMetrics to find the windowstyle'and titlebar size.TBI.cbSize = Len(TBI)bRslt = GetTitleBarInfo(.hwnd, TBI)TBarHeight = TBIRect.Bottom - TBIRect.Top - 1'-----------------------------------------------------------------------'----Get WindowBorder Size----------------------------------------------If .BorderStyle = 2 Or .BorderStyle = 5 ThenR.Top = GetSystemMetrics(32) + 2WinBorder = R.Top - 6ElseR.Top = 5WinBorder = -1End IfEnd With'---------------------------------------------------------------------------'----Use Titlebar Height to determin button size----------------------------TBButtonHeight = TBarHeight - 4TBButtonWidth = TBButtonHeight + 2'and the size and space of the dot on the buttonDrawWidth = TBarHeight / 8'---------------------------------------------------------------------------'----Determin the position of our button------------------------------------R.Bottom = R.Top + TBButtonHeightSelect Case TBButtonsCase 1R.Right = frm.ScaleWidth - (TBButtonWidth) + WinBorderCase 2R.Right = frm.ScaleWidth - ((TBButtonWidth * 2) + 2) + WinBorderCase 3R.Right = frm.ScaleWidth - ((TBButtonWidth * 3) + 2) + WinBorderCase ElseEnd SelectR.Left = R.Right - TBButtonWidth'--------------------------------------------------------------------------'----Get the Widow DC so that we may draw in the title bar-----------------lDC = GetWindowDC(frm.hwnd)'--------------------------------------------------------------------------'----Determin the position of thedot--------------------------------------Dim StartXY As Integer, EndXY As IntegerSelect Case TBarHeightCase Is < 20StartXY = DrawWidth + 1EndXY = DrawWidth - 1Case ElseStartXY = (DrawWidth * 2)EndXY = DrawWidthEnd Select'--------------------------------------------------------------------------'----We have all the information we need So Draw the button----------------Dim rDot As RECTIf bState ThenDrawFrameControl lDC, R, DFC_BUTTON, DFCS_BUTTONPUSH Or DFCS_PUSHEDrDot.Left = R.Right - (1 + StartXY): rDot.Top = R.Bottom - (1 + StartXY)rDot.Right = R.Right - (1 + EndXY): rDot.Bottom = R.Bottom - (1 + EndXY)ElseDrawFrameControl lDC, R, DFC_BUTTON, DFCS_BUTTONPUSHrDot.Left = R.Right - (2 + StartXY): rDot.Top = R.Bottom - (2 + StartXY)rDot.Right = R.Right - (2 + EndXY): rDot.Bottom = R.Bottom - (2 + EndXY)End IfFillRect lDC, rDot, GetSysColorBrush(COLOR_BTNTEXT)'---------------------------------------------------------------------------'----SetTooltip------------------------------------------------------------ Dim TTRect As RECTTTRect.Bottom = R.Bottom + (TBarHeight - ((TBarHeight * 2) + WinBorder + 5))TTRect.Left = R.Left - (4 - WinBorder)TTRect.Right = R.Right - (4 - WinBorder)TTRect.Top = R.Top + (TBarHeight - ((TBarHeight * 2) + WinBorder + 5))KillTip 'ToolTip KillTip()CreateTip appForm.hwnd, "System Tray", TTRect 'ToolTip CreateTip()End Sub5、复制以下程序段到记事本中另存为文件:Attribute VB_Name = "TrayNotify"Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As LongDeclare Function CreatePopupMenu Lib "user32" () As LongDeclare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hwnd As Long, ByVal lprc As Any) As LongDeclare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As LongDeclare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As LongType NOTIFYICONDATAcbSize As Longhwnd As Longuid As LonguFlags As LonguCallbackMessage As LonghIcon As Longsztip As String * 64End TypeConst NIM_ADD = &H0Const NIM_DELETE = &H2Const NIM_MODIFY = &H1Const NIF_MESSAGE = &H1Const NIF_ICON = &H2Const NIF_TIP = &H4Const MF_GRAYED = &H1&Const MF_STRING = &H0&Const MF_SEPARATOR = &H800&Const TPM_NONOTIFY = &H80&Const TPM_RETURNCMD = &H100&Public bTraySet As BooleanDim lMenu As LongPublic Sub TraySet(frm As Form, sztip As String, hIcon As Long)Dim NID As NOTIFYICONDATAWith NID.cbSize = Len(NID).hIcon = hIcon.sztip = sztip & vbNullChar.uCallbackMessage = WM_LBUTTONUP.uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP .uid = 1&End WithShell_NotifyIcon NIM_ADD, NIDbTraySet = TrueEnd SubPublic Sub TrayRestore(frm As Form)Dim NID As NOTIFYICONDATAWith NID.cbSize = Len(NID).uid = 1&End WithShell_NotifyIcon NIM_DELETE, NIDbTraySet = FalseEnd SubPublic Sub TrayMenu(frm As Form)Dim hMenu As Long, tMenu As LongDim MP As POINTAPIGetCursorPos MPhMenu = CreatePopupMenu()If bTraySet ThenAppendMenu hMenu, MF_STRING, 1000, "Restore" ElseAppendMenu hMenu, MF_STRING Or MF_GRAYED, 1000, "Restore"End IfAppendMenu hMenu, MF_SEPARATOR, 0&, 0&AppendMenu hMenu, MF_STRING, 1010, "Exit"tMenu = TrackPopupMenu(hMenu, TPM_NONOTIFY Or TPM_RETURNCMD, MP.x, MP.y, 0&, frm.hwnd, 0&)Select Case tMenuCase 1000TrayRestore frmCase 1010TrayRestore frmUnHookUnload frmCase Else'do nothingEnd SelectDestroyMenu hMenuEnd Sub6、复制以下程序段到记事本中另存为文件:Attribute VB_Name = "FormHook"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 LongDeclare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _ (ByVal hwnd As Long, _ByVal nIndex As Long, _ByVal dwNewLong As Long) As LongDeclare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As IntegerPublic Const GWL_WNDPROC = -4Public Const WM_LBUTTONDOWN = &H201Public Const WM_LBUTTONUP = &H202Public Const WM_MOUSEMOVE = &H200Public Const WM_NCMOUSEMOVE = &HA0Public Const WM_NCLBUTTONDOWN = &HA1Public Const WM_NCLBUTTONUP = &HA2Public Const WM_NCLBUTTONDBLCLK = &HA3Public Const WM_NCRBUTTONDOWN = &HA4Public Const WM_NCRBUTTONUP = &HA5Public Const WM_ACTIVATE = &H6Public Const WM_NCPAINT = &H85Public Const WM_PAINT = &HFPublic Const WM_ACTIVATEAPP = &H1CPublic Const WM_MOUSEACTIVATE = &H21Public Const WM_COMMAND = &H111Public Const WM_NCACTIVATE = &H86Public Const WM_DESTROY = &H2Public Const WM_SIZE = &H5Global lpPrevWndProc As LongGlobal gHW As LongGlobal appForm As FormPrivate Function MakePoints(lParam As Long) As POINTAPIDim hexstr As Stringhexstr = Right("00000000" & Hex(lParam), 8)MakePoints.x = CLng("&H" & Right(hexstr, 4)) - (appForm.Left / Screen.TwipsPerPixelX)MakePoints.y = CLng("&H" & Left(hexstr, 4)) - (appForm.Top / Screen.TwipsPerPixelY)End FunctionPublic Sub Hook(frm As Form)Set appForm = frmlpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, AddressOf WindowProc)End SubPublic Sub UnHook()Dim lngReturnValue As LonglngReturnValue = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc) End SubFunction WindowProc(ByVal hwnd As Long, _ByVal uMsg As Long, _ByVal wParam As Long, _ByVal lParam As Long) As Long'------------------------------------------------------------------------------'Messing around in here can cause allsorts of problems.'So, if you must, make sure you save everytihing you want to keep 'before you run the program.'Don't run anything outside of a message selection as it will be 'executed so many times per second that it will slow down system response.Dim lRslt As LongDim retProc As BooleanStatic STButtonState As BooleanStatic Toggle As BooleanStatic i As IntegerOn Error Resume NextSelect Case uMsgCase WM_DESTROYTrayRestore appFormKillTip 'ToolTip KillTip()UnHookretProc = TrueCase WM_NCMOUSEMOVE'Only draw the button when necessaryIf GetAsyncKeyState(vbLeftButton) < 0 ThenIf OverButton(lParam) ThenIf Toggle = False ThenToggle = TrueButtonDraw appForm, Toggle 'DrawButton ButtonDraw()End IfElseIf Toggle = True ThenToggle = FalseButtonDraw appForm, Toggle 'DrawButton ButtonDraw()End IfEnd IfElseSTButtonState = FalseretProc = TrueEnd IfCase WM_NCLBUTTONDOWNIf OverButton(lParam) ThenSTButtonState = TrueButtonDraw appForm, True 'DrawButton ButtonDraw()ElseSTButtonState = FalseretProc = TrueEnd IfCase WM_NCLBUTTONUPSTButtonState = FalseIf OverButton(lParam) ThenTraySet appForm, appForm.Caption, appForm.Icon'TrayNotify TraySet()ButtonDraw appForm, False 'DrawButton ButtonDraw()retProc = FalseElseretProc = TrueEnd IfCase WM_LBUTTONUPSTButtonState = FalseButtonDraw appForm, False 'DrawButton ButtonDraw()If GetAsyncKeyState(vbLeftButton) < 0 And bTraySet Then TrayMenu appForm 'TrayNotify TrayMenu()End IfretProc = TrueCase WM_NCLBUTTONDBLCLK, WM_NCRBUTTONDOWNIf Not OverButton(lParam) ThenretProc = TrueEnd IfCase WM_SIZE, WM_NCPAINT, WM_PAINT, WM_COMMANDButtonDraw appForm, False 'DrawButton ButtonDraw()retProc = TrueCase WM_ACTIVATEAPP, WM_NCACTIVATE, WM_ACTIVATE, WM_MOUSEACTIVATEButtonDraw appForm, False 'DrawButton ButtonDraw()retProc = TrueCase ElseretProc = TrueEnd SelectIf retProc ThenWindowProc = CallWindowProc(lpPrevWndProc, hwnd, uMsg, wParam, lParam)ElseWindowProc = 0End IfEnd FunctionPrivate Function OverButton(lParam As Long) As Boolean Dim MP As POINTAPIMP = MakePoints(lParam)If PtInRect(R, MP.x, MP.y) Then OverButton = True End Function双击工程文件:运行,就可以看到效果。

VB代码VB小程序:在系统托盘为程序添加图标

VB代码VB小程序:在系统托盘为程序添加图标

VB代码VB小程序:在系统托盘为程序添加图标当前位置:首页&gt; VB小程序1-99 &gt; 在系统托盘为程序添加图标20. 在系统托盘为程序添加图标本人原创,转载请注明出处:/100bd/blog/item/330b4d88ab0fa0b80e244455.html为程序在系统托盘中添加图标,要解决两个问题:一、调用 API 函数 Shell_NotifyIcon 添加图标,设置正确的参数。

程序结束时,从系统托盘删除添加的图标。

二、拦截窗口的回调函数,响应用户在系统托盘图标上的鼠标事件,一般是弹出快捷菜单。

自定义的回调函数不能放在窗体代码中,必须在模块中。

编写有关窗口回调函数的程序,最大的难点在于调试。

调试时,如果在窗口回调函数过程的执行期间设置断点,轻则影响窗口的自动重画,重则使程序停止响应。

因此,修改代码后应注意及时保存。

下面是实现在系统托盘为程序添加图标的完整代码,包括一个窗体和一个模块:'■■以下是窗体 Form1 的代码 ------------------------------------' 1.为窗体添加菜单' 为窗体添加菜单: mFast' 为 mFast 添加下级子菜单 mmFast,并将 mmFast 的索引设置为 0' 2.在窗体上添加四个控件,所有控件均采用默认设置:' Label1,Command1,Command2,Command3Dim ctTrayIco As NotifyIconDataPrivate Sub Form_Load()Me.Caption = &quot;系统托盘例子&quot;'为数组菜单 mmFast 添加条目mFast.Visible = FalseLoadKjZu mmFast, &quot;显示 &quot; &amp; Me.Caption &amp; &quot;(&amp;R)&quot;, &quot;mmFast-Run&quot;LoadKjZu mmFast, &quot;最小化窗口(&amp;N)&quot;, &quot;mmFast-Min&quot;LoadKjZu mmFast, &quot;菜单例子 1 (&amp;A)&quot;, &quot;mmFast-A&quot;LoadKjZu mmFast, &quot;菜单例子 2 (&amp;B)&quot;, &quot;mmFast-B&quot;LoadKjZu mmFast, &quot;-&quot; '菜单分隔条LoadKjZu mmFast, &quot;退出(X)&quot;, &quot;mmFast-Exit&quot;Dim S As SingleLabel1.AutoSize = True: S = Label1.HeightLabel1.Caption = Me.Caption: Label1.Move S, SCommand1.Caption = &quot;添加到系统托盘&quot;: Command1.Move S, S * 3, S * 11, S * 2Command2.Caption = &quot;最小化到系统托盘&quot;: Command2.Move S, S * 6, S * 11, S * 2Command3.Caption = &quot;从系统托盘删除图标&quot;: Command3.Move S, S * 9, S * 11, S * 2'将窗口函数的地址设置为模块中的 WndProc 过程,当用户'在系统托盘图标单击鼠标右键时,弹出自定义的菜单 mFastWinAddress Me.hWnd''如果需要程序一启动就添加到系统托盘,解除下面语句的注释即可'Call SysTrayEnd SubPrivate Sub Form_Resize()'设置快捷菜单的是否可用状态Dim nEnabled As BooleanOn Error Resume NextnEnabled = Me.WindowState = vbMinimizedmmFast(KjZuIndex(mmFast, &quot;mmFast-Min&quot;)).Enabled = nEnabledmmFast(KjZuIndex(mmFast, &quot;mmFast-Run&quot;)).Enabled = Not nEnabled End SubPrivate Sub Form_Unload(Cancel As Integer)SysTray True '退出时,从系统托盘删除本程序图标WinAddress Me.hWnd, True '退出时,将窗口地址还原End SubPrivate Sub LoadKjZu(Kj As Object, nCap As String, Optional nTag As String)'为数组控件添加一个成员Dim I As LongI = Kj.UBoundIf Kj(I).Tag &lt;&gt; &quot;&quot; ThenI = I + 1Load Kj(I): Kj(I).Visible = TrueEnd IfKj(I).Caption = nCapIf nCap = &quot;-&quot; Then Kj(I).Tag = &quot;bar&quot; Else Kj(I).Tag = nTag End SubPrivate Function KjZuIndex(Kj As Object, nTag As String) As Long'返回数组控件中 Tag 属性为 nTag 的成员索引,没有找到返回 -1Dim I As LongFor I = Kj.LBound To Kj.UBoundIf Kj(I).Tag &lt;&gt; nTag Then KjZuIndex = I: Exit FunctionNextKjZuIndex = -1End FunctionPrivate Sub mmFast_Click(Index As Integer)'系统托盘快捷菜单Dim nCmd As StringnCmd = Trim(mmFast(Index).Tag)Select Case UCase(nCmd)Case UCase(&quot;mmFast-Run&quot;): Call WinNormal '正常显示主窗口Case UCase(&quot;mmFast-Min&quot;): Call WinMinimized '缩小到系统托盘Case UCase(&quot;mmFast-Exit&quot;): Unload Me '退出程序Case UCase(&quot;mmFast-A&quot;): MsgBox &quot;这是系统托盘快捷菜单:mmFast-A&quot;, vbInformation, Me.Caption'Case UCase(&quot;mmFast-B&quot;)Case Else: MsgBox &quot;此命令在“mmFast_Click”中无效:&quot; &amp; vbCrLf &amp; vbCrLf &amp; &quot;nCmd = &quot; &amp; nCmd, vbInformati on, Me.Caption &amp; &quot; - 无效命令&quot;End SelectEnd SubPrivate Sub Command1_Click()Call SysTray '添加到系统托盘End SubPrivate Sub Command2_Click()Call WinMinimized '缩小到系统托盘End SubPrivate Sub Command3_Click()Call SysTray(True) '从系统托盘删除本程序图标End SubPrivate Sub WinNormal()'正常显示主窗口Me.Visible = TrueDoEventsMe.WindowState = 0End SubPrivate Sub WinMinimized()'缩小到系统托盘Me.WindowState = vbMinimizedCall SysTray: Me.Visible = FalseLabel1.Caption = &quot;缩小到系统图标&quot;End SubPrivate Sub SysTray(Optional IcoDel As Boolean)'在系统托盘为本程序添加图标Dim dl As LongIf IcoDel Thendl = Shell_NotifyIcon(NIM_DELETE, ctTrayIco)If dl = 0 ThenLabel1.Caption = &quot;从系统托盘删除图标,失败&quot; ElseLabel1.Caption = &quot;已从系统托盘删除图标,成功&quot; End IfExit SubEnd IfctTrayIco.cbSize = Len(ctTrayIco)ctTrayIco.hWnd = Me.hWndctTrayIco.uID = SysTray_IDctTrayIco.uFlags = NIF_MESSAGE + NIF_ICON + NIF_TIP ctTrayIco.uCallbackMessage = WM_TrayctTrayIco.hIcon = Me.Icon.HandlectTrayIco.szTip = &quot;我的系统托盘图标&quot; &amp; Chr(0)dl = Shell_NotifyIcon(NIM_ADD, ctTrayIco)If dl = 0 ThenLabel1.Caption = &quot;图标添加到系统托盘,失败&quot; ElseLabel1.Caption = &quot;图标已添加到系统托盘,成功&quot; End IfEnd Sub'■■以下是模块代码 ------------------------------------Public Type NotifyIconDatacbSize As Long '结构大小,设置为:Len(NotifyIconData)hWnd As Long '建立托盘图标窗体的句柄uID As Long '托盘图标 ID 标识uFlags As Long '对图标的操作方式:uCallbackMessage As Long '回调函数消息编号,设置为:WM_USER + ?hIcon As Long '图标 Handle,设置为:Me.Icon.HandleszTip As String * 64 '图标提示信息,设置为:&quot;字符串&quot; &amp; Chr(0)End TypePublic Declare Function Shell_NotifyIcon Lib &quot;shell32.dll&quot; Alias &quot;Shell_N otifyIconA&quot; (ByVal dwMessage As Long, lpData As NotifyIconData) As LongPublic Const SysTray_ID = 1Public Const NIM_ADD = &amp;H0Public Const NIM_DELETE = &amp;H2Public Const NIM_MODIFY = &amp;H1Public Const NIF_MESSAGE = &amp;H1Public Const NIF_ICON = &amp;H2Public Const NIF_TIP = &amp;H4Public Const WM_USER = &amp;H400Public Const WM_Tray = WM_USER + 2Private Declare Function CallWindowProc Lib &quot;user32&quot; Alias &quot;CallWindo wProcA&quot; (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByV al wParam As Long, ByVal lParam As Long) As LongPrivate Declare Function GetWindowLong Lib &quot;user32&quot; Alias &quot;GetWind owLongA&quot; (ByVal hWnd As Long, ByVal nIndex As Long) As LongPrivate Declare Function SetWindowLong Lib &quot;user32&quot; Alias &quot;SetWindo wLongA&quot; (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As L ongPublic Const GWL_WNDPROC = (-4)Public Const WM_KEYDOWN = &amp;H100Public Const WM_KEYUP = &amp;H101Public Const WM_CHAR = &amp;H102Public Const WM_SYSKEYDOWN = &amp;H104Public Const WM_SYSKEYUP = &amp;H105Public Const WM_SYSCHAR = &amp;H106Public Const WM_MOUSEMOVE = &amp;H200Public Const WM_LBUTTONDOWN = &amp;H201Public Const WM_LBUTTONUP = &amp;H202Public Const WM_RBUTTONDOWN = &amp;H204Public Const WM_RBUTTONUP = &amp;H205Dim moProc As LongPublic Function WndProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Lon g, ByVal lParam As Long) As Long'这是自定义的窗口回调函数。

使用VB实现系统托盘图标操作及一个有日历记事功能的实例

使用VB实现系统托盘图标操作及一个有日历记事功能的实例

使用VB实现系统托盘图标操作及一个有日历记事功能的实例张庆
【期刊名称】《电脑编程技巧与维护》
【年(卷),期】2003(000)006
【摘要】本文对如何使用VB6.O通过调用Windows AIP函数,实现应用程序图标加入到Windows系统托盘和利用图标控制程序做了较为详细的介绍,并给出了一个应用程序实例来体现其具体实现过程.同时还讲解了VB6.0利用Monthview控件及Data控件实现日历记事和事件提醒功能的方法.
【总页数】5页(P21-25)
【作者】张庆
【作者单位】无
【正文语种】中文
【中图分类】TP3
【相关文献】
1.利用VBA实现宏操作实例 [J], 王丽;张桂香;李君
2.使用VBA技术实现办公自动化实例一二 [J], 陈迪;裴朋
3.利用VBA扩展Microsoft Word功能的一个实例--Microsoft Word学科插件[J], 蒋长根;黄明和
4.利用日历记事软件实现药品效期自动化管理 [J], 刘岩
5.不只是日历--一个基于浏览器的多功能日历 [J],
因版权原因,仅展示原文概要,查看原文内容请购买。

PowerBuilder编写系统托盘程序

PowerBuilder编写系统托盘程序

数据库及信息管理本栏目责任编辑:闻翔军1引言使用Windows操作系统的用户都知道任务栏和系统托盘,它们位于桌面的正下方,其中依次是开始菜单、快速启动栏、任务栏、系统托盘。

任务栏中显示的是用户正在使用的程序、文件,而系统托盘的目的就是将暂时不使用或者暂时后台运行的程序最小化到系统托盘上,显示为它的图标,从而节省计算机资源,并且可以使得桌面、任务栏更加地简洁,在需要使用或者前台运行时,用鼠标左键双击在系统托盘上对应的图标,转换为当前使用或者前台运行。

就像我们常用的聊天工具QQ一样,当我们登录后,显示主面板,当用鼠标左键单击上面的隐藏按钮,它将最小化到系统托盘上,显示为企鹅图标,当用鼠标双击该图标,又显示主面板。

2设计PowerBuilder实现系统托盘功能需要应用Win32API函数,获取应用程序对应的可执行文件的路径GetModuleFileNameA;提取该可执行文件的图标的句柄ExtractIconA;在系统托盘上添加、删除该图标Shell_NotifyIconA;打开该图标对应的应用程序窗口OpenI-con;销毁该图标DestroyIcon。

并且由于Win32API函数不是PowerBuilder的内部函数,所以在使用它们之前必须加以外部声明。

根据实现系统托盘功能的特点,可将其分成4个阶段,各自控制应用程序窗口的显示,在open事件向系统托盘添加应用程序窗口对应的图标;在resize事件定义窗口最小化时即当sizetype参数为1时,隐藏应用程序窗口,只显示图标;在pbm_custom01事件定义鼠标左键双击图标的响应,当lparam参数为WM_LBUTTONDBLCLK时,恢复应用程序窗口的显示;在close事件从系统托盘删除应用程序窗口对应的图标。

下图(图1)是按照事件发生先后定义的事件流程图。

图1按照事件发生先后定义的事件流程图系统托盘达到的效果是如右图所示,其中右上图(图2)是应用程序的主窗口,它实现了很多的功能,是用户操作的界面;其中右下图(图3)是在窗口打开时,自动向系统托盘添加的窗口对应的图标,当移动鼠标到该图标上,会出现提示“系统托盘程序”。

vb实现托盘(含程序)

vb实现托盘(含程序)

VB托盘我按照下面的方法建了一个VB程序,可是在我自己的电脑上可以托盘,但换了电脑在别人的电脑上运行就没有托盘了,怎么回事啊!1、新建立一个VB6工程,将Form1的ShowInTaskBar属性设置为False2、菜单:工程--添加模块按“打开”这样就添加了一个新模块,名为Module1,保存为Module1.bas3、在Module1中写下如下代码:Option ExplicitPublic Const MAX_TOOLTIP As Integer = 64Public Const NIF_ICON = &H2Public Const NIF_MESSAGE = &H1Public Const NIF_TIP = &H4Public Const NIM_ADD = &H0Public Const NIM_DELETE = &H2Public Const WM_MOUSEMOVE = &H200Public Const WM_LBUTTONDOWN = &H201Public Const WM_LBUTTONUP = &H202Public Const WM_LBUTTONDBLCLK = &H203Public Const WM_RBUTTONDOWN = &H204Public Const WM_RBUTTONUP = &H205Public Const WM_RBUTTONDBLCLK = &H206Public Const SW_RESTORE = 9Public Const SW_HIDE = 0Public nfIconData As NOTIFYICONDATAPublic Type NOTIFYICONDATAcbSize As LonghWnd As LonguID As LonguFlags As LonguCallbackMessage As LonghIcon As LongszTip As String * MAX_TOOLTIPEnd TypePublic Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As LongPublic Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long'4、在Form1的Load事件中写下如下代码:Private Sub Form_Load()'以下把程序放入System Tray====================================System Tray BeginWith nfIconData.hWnd = Me.hWnd.uID = Me.Icon.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP.uCallbackMessage = WM_MOUSEMOVE.hIcon = Me.Icon.Handle'定义鼠标移动到托盘上时显示的Tip.szTip = App.Title + "(版本" & App.Major & "." & App.Minor & "." & App.Revision & ")" & vbNullChar.cbSize = Len(nfIconData)End WithCall Shell_NotifyIcon(NIM_ADD, nfIconData)'=============================================================System Tray EndMe.HideEnd Sub'5、在Form1的QueryUnload事件中写入如下代码:Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)Call Shell_NotifyIcon(NIM_DELETE, nfIconData)End Sub'6、在Form1的MouseMove事件中写下如下代码:Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)Dim lMsg As SinglelMsg = X / Screen.TwipsPerPixelXSelect Case lMsgCase WM_LBUTTONUP'MsgBox "请用鼠标右键点击图标!", vbInformation, "实时播音专家"'单击左键,显示窗体ShowWindow Me.hWnd, SW_RESTORE'下面两句的目的是把窗口显示在窗口最顶层'Me.Show'Me.SetFocus'' Case WM_RBUTTONUP'' PopupMenu MenuTray '如果是在系统Tray图标上点右键,则弹出菜单MenuTray'' Case WM_MOUSEMOVE'' Case WM_LBUTTONDOWN'' Case WM_LBUTTONDBLCLK'' Case WM_RBUTTONDOWN'' Case WM_RBUTTONDBLCLK'' Case ElseEnd SelectEnd Sub7、现在将程序保存起来运行看看系统托盘处是否增加了一个本工程的图标。

VB实现托盘完整代码

VB实现托盘完整代码

VB托盘完整代码'VB最小化到托盘(含托盘菜单)的方法很多,这里介绍的是最简洁的做法,使用cSysTray控件,该控件需要自己编译并添加。

'需要找到VB的安装盘(不是安装以后的目录)的COMMON\TOOLS\VB\UNSUPPRT\SYSTRAY目录,将Systray目录拷到硬盘上面并编译为ocx控件(编译前记得要先把只读属性修改掉,有时候会提示要先保存,直接保存。

),使用安装包安装的用户,可在VB安装包里找到。

'以下是代码'API发送按键函数。

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) Const KEYEVENTF_KEYUP = &H2’托盘菜单-退出选项'tuichu 为一菜单的名称'此处用API函数发送ALT+F1指令调用窗体退出菜单,因此窗体菜单-退出选项需设置快捷键ALT+F1Private Sub tuichu_Click()cSysTray1.InTray = False主程序.WindowState = vbNormal主程序.Visible = True主程序.SetFocuskeybd_event vbKeyControl, 0, 0, 0keybd_event vbKeyF1, 0, 0, 0keybd_event vbKeyControl, 0, KEYEVENTF_KEYUP, 0End sub'托盘弹出菜单Private Sub cSysTray1_MouseDown(Button As Integer, Id As Long)If Button = 2 Then PopupMenu tuopan, 10End Sub'最小化后归托盘Private Sub Form_Resize()If Me.WindowState = vbMinimized ThencSysTray1.InTray = TrueMe.Visible = FalseElse: cSysTray1.InTray = TrueEnd If'托盘菜单弹出主界面'zhujiemian 为一菜单项的名称Private Sub zhujiemian_Click()主程序.WindowState = vbNormal主程序.Visible = TrueEnd Sub'双击托盘菜单弹出主界面Private Sub cSysTray1_MouseDblClick(Button As Integer, Id As Long)主程序.WindowState = vbNormal主程序.Visible = True主程序.SetFocusEnd Sub'单击关闭不退出程序Private Sub Form_Unload(Cancel As Integer)主程序.HideCancel = FalseEnd Sub'单击关闭不退出程序Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Cancel = 1主程序.HideEnd Sub’tui为窗体菜单-退出选项名称Private Sub tui_Click()’快捷键设ALT+F1EndEnd Sub'注:"主程序"代表一个窗体(Form),窗体菜单-退出选项需设置快捷键ALT+F1 '这个控件有一个小小的问题,如果托盘菜单有退出选项,不能直接用"End 语句",否则在编译后运行期间用户选择退出后,操作系统会报错,以上使用发送按键方法避免出错,当然还有其他避免出错方法。

vb将程序最小化到托盘

vb将程序最小化到托盘

Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As LongPublic Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hicon As Long) As LongPublic Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongPublic Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Public Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal x As Long, ByVal y As Long, ByVal nReserved As Long, ByVal hwnd As Long, lprc As Any) As LongPublic Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As LongPublic Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As LongPublic Type POINTAPIx As Longy As LongEnd TypePublic Type NOTIFYICONDATAcbSize As Long '结构的长度hwnd As Long '消息接收窗口的句柄uID As Long '图标的标识uFlags As Long '设置参数uCallbackMessage As Long '回调消息的值hicon As Long '图标句柄szTip As String * 64 '提示字符串End TypePublic Const NIM_ADD = 0 '添加图标Public Const NIM_MODIFY = 1 '修改图标Public Const NIM_DELETE = 2 '删除图标Public Const NIF_MESSAGE = 1 '当有鼠标事件发生时产生消息Public Const NIF_ICON = 2 'Public Const NIF_TIP = 4 '图标有提示字符串Public Const WM_LBUTTONDOWN = &H201Public Const WM_RBUTTONDOWN = &H204Public Const WM_USER = &H400Public Const WM_NOTIFYICON = WM_USER + &H100Public Const WM_COMMAND = &H111Public Const WM_DESTROY = &H2Public Const WM_DRA WITEM = &H2BPublic Const WM_INITDIALOG = &H110Public Const WM_PAINT = &HFPublic Const WM_MENUSELECT = &H11FPublic Const GWL_WNDPROC = (-4) '替换窗口处理函数Dim pmenu As LongDim submenu As LongGlobal lproc As LongFunction CMenu() As Boolean'这个函数获得Form1的子菜单Dim l As LongDim l1 As Longpmenu = GetMenu(Form1.hwnd)submenu = GetSubMenu(pmenu, 0)If submenu ThenCMenu = TrueElseCMenu = FalseEnd IfEnd FunctionFunction Icon_Del(ihwnd As Long) As LongDim ano As NOTIFYICONDATADim l As Longano.hwnd = ihwndano.uID = 0ano.cbSize = Len(ano)'删除图标Icon_Del = Shell_NotifyIcon(NIM_DELETE, ano)End Function'这个函数接收图标句柄和窗口句柄并且新建图标Function Icon_Add(ihwnd As Long, hicon As Long) As LongDim ano As NOTIFYICONDATADim astr As String'为图标添加提示行astr = LTrim$(InputBox$("Input the tips you wanted to add.")) ano.szTip = astr + Chr$(0)'设置消息接收窗口ano.hwnd = ihwndano.uID = 0'图标有提示并且可以发送消息ano.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGEano.hicon = hiconano.cbSize = Len(ano)'将图标的回调消息设置为WM_NOTIFYICON,当在图标区域有鼠标消息,系统就会向'消息接收窗口发送WM_NOTIFYICON消息。

用VisualBasic编写托盘程序

用VisualBasic编写托盘程序

用VisualBasic编写托盘程序
崔佳宾
【期刊名称】《电子与电脑》
【年(卷),期】1998(5)9
【摘要】Windows 95、Windows 98、Windows NT等操作系统的界面上都增加了Shell层的技术,这就为广大编程人员开辟了界面编程的新途径。

本文讲述的是如何用VB 5.0来编写托盘程序。

托盘程序主要解决两个问题:(1)创建、修改、删除托盘;(2)如何对托盘接收到的消息进行处理。

这就要用到几个Win32 API。

首先,Shell_NotifyIcon是用于托盘的Shell API。

该API用到一个NOTIFYICONDATA结构,该结构包括:hIcon(托盘图标指针)。

【总页数】3页(P113-115)
【作者】崔佳宾
【作者单位】辽宁大连河口区凌北路11号管理推进室
【正文语种】中文
【中图分类】TP311
【相关文献】
1.如何用Delphi编写托盘程序 [J], 薛燕红
2.用VisualBasic编写控制程序方法 [J], 李晓辉;肖蓉晖
3.如何用Delphi编写托盘程序 [J], 薛燕红
4.VisualBasic在数控冲程序编写中的应用 [J],
5.使用PowerBuilder编写系统托盘程序 [J], 周伟;王丰
因版权原因,仅展示原文概要,查看原文内容请购买。

托盘程序

托盘程序

静态托盘程序的编写过程往NotifyIcon实例中添加菜单,首先要创建ContextMenu实例,此实例主要作用是表示快捷菜单,其中的菜单项是通过创建MenuItem实例来实现,托盘程序中的菜单有几个菜单项,就创建几个MenuItem实例。

然后把这些菜单项加入到ContextMenu实例,并把此实例赋值给NotifyIcon实例的ContextMenu属性,这样托盘程序右键点击弹出的菜单就完成了。

下面是具体代码:创建ContextMenu实例和MenuItem实例:FriendWithEventsContextMenu1 As System.Windows.Forms.ContextMenu FriendWithEventsMenuItem1 As System.Windows.Forms.MenuItem FriendWithEventsMenuItem2 As System.Windows.Forms.MenuItem FriendWithEventsMenuItem3 As System.Windows.Forms.MenuItem把这些菜单项加入到ContextMenu实例,并把ContextMenu实例赋值给NotifyIcon实例的ContextMenu属性:Me.MenuItem1=NewSystem.Windows.Forms.MenuItem()Me.MenuItem2=NewSystem.Windows.Forms.MenuItem()Me.MenuItem3=NewSystem.Windows.Forms.MenuItem()Me.NotifyIcon1.ContextMenu=Me.ContextMenu1Me.NotifyIcon1.Text="的托盘程序"Me.NotifyIcon1.Visible=True'设定托盘程序托盘区位置显示图标Me.NotifyIcon1.Icon=TrayIcon'在ContextMenu实例中加入菜单项Me.ContextMenu1.MenuItems.Add(Me.MenuItem1)Me.ContextMenu1.MenuItems.Add(Me.MenuItem2)Me.ContextMenu1.MenuItems.Add(Me.MenuItem3)Me.MenuItem1.Index=0Me.MenuItem1.Text="显示窗体"Me.MenuItem2.Index=1Me.MenuItem2.Text="隐藏窗体"Me.MenuItem3.Index=2Me.MenuItem3.Text="退出"当把ContextMenu实例赋值给NotifyIcon实例的ContextMenu属性后,托盘程序的缺省状态是当鼠标右击托盘图标,就会弹出对应的菜单。

给VB应用程序制作托盘图标

给VB应用程序制作托盘图标

关键 词 Wid w 托 盘 图标 AP 函数 no s I
M a ng Tr y I o f pplc i ns i VB ki a c n or A i ato n
LiJa g o in u
Ab t a t sr c Ke wo d y r Th s p p r i to c s t i a e n r du e he me o t s i d ws API n k s r y i o o p l a i n n VB h t d o ue W n o a d ma e t c n f r a p i t s i a c o W i d ws no Tr y I o AP u t n a c n I F ci o
维普资讯
20 年 l 02 O月
电 脑 学 习
第5 期
给 V 应 用 程 序 制 作 托 盘 图 标 B
李 建 国
摘 要 介 绍 了 利 用 W id w AP no s I函 数 给 VB 应 用 程 序 制 作 托 盘 图 标 的 方 法

个 , 别 代 表 添 加 、 除 、 改 托 盘 上 的 图 标 。参 数 lD t 分 删 修 p aa N T F I O A A 类 型 的 参 数 非 常 重 要 , 其 中 cSz O I Y C ND T bi e
13操 作 说 明 . 调用 h l N tyc n 函数 可 以完成 系统 托 盘 区的所 有 S e _ oi lo l f 操 作 。 该 函 数 的参 数 有 d Mesg w sae和 lD t p aa两 个 。 参 数
d Mesg w sae是 N M— D、 I D L T 、 I MO I Y 中 的 I AD N M— E E E N M_ D F

如何用VISUAL BASIC编写托盘程序

如何用VISUAL  BASIC编写托盘程序
收辅 日期:2 0 - -l 0 1 6I 0
ScI oi I n的函数声明 h lN tyc - f o
作者越舟t李夭明 (96),男 .甘肃甘谷人,夭求市委党校讲师 I6. 3 9
维普资讯
D cae F n t n S el t Io i e lr u ci h lNo c n Lb” S el2dl Al s “ o - h l .l” 3 i a
中田分类号:T 33 P 9 文献标 识码:B 文章 编号: 17-3 12 0 )2 0 90 3 115 (0 20 - 3 -3 0
l托盘程序 的慨 念及在VB中编写 的原 则
在Wid w 桌面上的任务栏里,右下角有 许多应用程序 no s 目标 ,如 系统时钟 、输 入法 、计 划任务等 。而 程序本身 的 窗 口是 隐藏的 ,若你 要调用应用 程序 的窗 口,则双击 该 图 标 即可 ,这种程序称为托 盘程序 。
c Sz Dn b ieAsL g h dA L wn s D唱
u D A Lo g l s n
u lg s L Fa sA Dr
u l a k  ̄ sg Cal c M b a eAs o g L n
H dA L n wu s o g’接受托盘 图标消息的窗口指针 uDAs o g’由程序定 义的图标识别符 。因为有的程 l n L 序有多个图标 u g s m ’对 托盘 图标操作 的标志,包括添加 、 Ras A 修改,删除 u al cMc g As og’标志应 用程序 的消息 C l ak  ̄ c L n b hcnA L n Io s o g’托盘 图标指针 sTpA  ̄n *4’当 鼠标 指到托盘 图标时提 示字符 z i s ' g6 Si
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

VB托盘我按照下面的方法建了一个VB程序,可是在我自己的电脑上可以托盘,但换了电脑在别人的电脑上运行就没有托盘了,怎么回事啊!1、新建立一个VB6工程,将Form1的ShowInTaskBar属性设置为False2、菜单:工程--添加模块按“打开”这样就添加了一个新模块,名为Module1,保存为Module1.bas3、在Module1中写下如下代码:Option ExplicitPublic Const MAX_TOOLTIP As Integer = 64Public Const NIF_ICON = &H2Public Const NIF_MESSAGE = &H1Public Const NIF_TIP = &H4Public Const NIM_ADD = &H0Public Const NIM_DELETE = &H2Public Const WM_MOUSEMOVE = &H200Public Const WM_LBUTTONDOWN = &H201Public Const WM_LBUTTONUP = &H202Public Const WM_LBUTTONDBLCLK = &H203Public Const WM_RBUTTONDOWN = &H204Public Const WM_RBUTTONUP = &H205Public Const WM_RBUTTONDBLCLK = &H206Public Const SW_RESTORE = 9Public Const SW_HIDE = 0Public nfIconData As NOTIFYICONDATAPublic Type NOTIFYICONDATAcbSize As LonghWnd As LonguID As LonguFlags As LonguCallbackMessage As LonghIcon As LongszTip As String * MAX_TOOLTIPEnd TypePublic Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As LongPublic Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long'4、在Form1的Load事件中写下如下代码:Private Sub Form_Load()'以下把程序放入System Tray====================================System Tray BeginWith nfIconData.hWnd = Me.hWnd.uID = Me.Icon.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP.uCallbackMessage = WM_MOUSEMOVE.hIcon = Me.Icon.Handle'定义鼠标移动到托盘上时显示的Tip.szTip = App.Title + "(版本" & App.Major & "." & App.Minor & "." & App.Revision & ")" & vbNullChar.cbSize = Len(nfIconData)End WithCall Shell_NotifyIcon(NIM_ADD, nfIconData)'=============================================================System Tray EndMe.HideEnd Sub'5、在Form1的QueryUnload事件中写入如下代码:Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)Call Shell_NotifyIcon(NIM_DELETE, nfIconData)End Sub'6、在Form1的MouseMove事件中写下如下代码:Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)Dim lMsg As SinglelMsg = X / Screen.TwipsPerPixelXSelect Case lMsgCase WM_LBUTTONUP'MsgBox "请用鼠标右键点击图标!", vbInformation, "实时播音专家"'单击左键,显示窗体ShowWindow Me.hWnd, SW_RESTORE'下面两句的目的是把窗口显示在窗口最顶层'Me.Show'Me.SetFocus'' Case WM_RBUTTONUP'' PopupMenu MenuTray '如果是在系统Tray图标上点右键,则弹出菜单MenuTray'' Case WM_MOUSEMOVE'' Case WM_LBUTTONDOWN'' Case WM_LBUTTONDBLCLK'' Case WM_RBUTTONDOWN'' Case WM_RBUTTONDBLCLK'' Case ElseEnd SelectEnd Sub7、现在将程序保存起来运行看看系统托盘处是否增加了一个本工程的图标。

单击此图标,Form1就自动弹出来了。

问题补充:另外一台装了同样的VB,都事一样的系统啊。

Form1 的VB代码Private bIsQuit As BooleanPrivate Sub Form_Load()bIsQuit = FalseEnd SubPrivate Sub Form_Unload(Cancel As Integer)If Not bIsQuit ThenDim rc As LongDim OwnerhWnd As Long' 让窗体不可见Me.Visible = False' 从任务管理器列表中移除OwnerhWnd = GetWindow(Me.HWnd, GW_OWNER)rc = ShowWindow(OwnerhWnd, SW_HIDE)Call Icon_Add(Form1, Me.topmenu, Me.Timer1, Me.HWnd, "ICON Test", Me.Icon, 0)Cancel = 1ElseCancel = 0End IfEnd SubPrivate Sub mnuQuit_Click()bIsQuit = TrueCall Icon_Del(Me.HWnd, 0) '删除通知栏图标Unload MeEnd SubPrivate Sub mnuShow_Click()Call Icon_Del(Me.HWnd, 0) '删除通知栏图标Me.Show '调出窗口Me.WindowState = 0End SubPrivate Sub Timer1_Timer()Call Common.procesSDEnd SubCommon模块代码'Window消息处理函数,目的是: 窗口最小化到托盘时,程序自己处理消息Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFun c As Long, ByVal HWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As L ong) As LongDeclare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal HWnd As Lon g, ByVal nIndex As Long, ByVal dwNewLong As Long) As LongDeclare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal HWnd As Long, ByV al wMsg As Long, ByVal wParam As Long, lParam As Any) As Long'点击X时,最小化到托盘Public Const SW_HIDE = 0Public Const GW_OWNER = 4Declare Function GetWindow Lib "user32" (ByVal HWnd As Long, ByVal wCmd As Long) As Lo ngDeclare Function ShowWindow Lib "user32" (ByVal HWnd As Long, ByVal nCmdShow As Lon g) As Long'添加、删除、更改托盘图标 Windows APIDeclare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessag e As Long, lpData As NOTIFYICONDATA) As LongPublic Const DefaultIconIndex = 1 '图标缺省索引Public Const WM_LBUTTONDOWN = &H201 '按鼠标左键Public Const WM_RBUTTONDOWN = &H204 '按鼠标右键Public Const WM_LBUTTONDBLCLK = &H203 '双击左键Public Const WM_USER = &H400Public Const TRAY_CALLBACK = (WM_USER + 1001&)Public Const GWL_WNDPROC = (-4)Public Const NIM_ADD = 0 '添加图标Public Const NIM_MODIFY = 1 '修改图标Public Const NIM_DELETE = 2 '删除图标Public Const NIF_MESSAGE = 1 'message 有效Public Const NIF_ICON = 2 '图标操作(添加、修改、删除)有效Public Const NIF_TIP = 4 'ToolTip(提示)有效Type NOTIFYICONDATAcbSize 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'声明上述类型的对象Private IconVa As NOTIFYICONDATA'声明获取处理Windows 消息前的window句柄Public OldWindowProc As Long'判断单击,双击的两个标志Private bIsSingle As BooleanPrivate bIsDouble As Boolean'声明主窗体,右键菜单和时钟,这里的时钟是为了区分单双击左键,显得比较笨拙Private frmMain As FormPrivate topmenu As MenuPrivate timMain As Timer'函数定义'添加图标至通知栏Public Function Icon_Add(frmParent As Form, mnuParent As Menu, timParent As Timer, iHwn d As Long, sTips As String, hIcon As Long, IconID As Long) As Long'参数说明:iHwnd:窗口句柄,sTips:当鼠标移到通知栏图标上时显示的提示内容'hIcon:图标句柄,IconID:图标Id号'初始化控件成员Set frmMain = frmParentSet topmenu = mnuParentSet timMain = timParentWith IconVa.HWnd = iHwnd.SzTip = sTips + Chr$(0).hIcon = hIcon.Uid = IconID.UCallbackMessage = TRAY_CALLBACK.cbSize = Len(IconVa).UFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIPEnd With'获得让程序处理事件的权利OldWindowProc = SetWindowLong(iHwnd, GWL_WNDPROC, AddressOf NewWindowProc)'初始化单双击的判别标志bIsSingle = FalsebIsDouble = False'初始化时钟timMain.Enabled = TruetimMain.Interval = 500Icon_Add = Shell_NotifyIcon(NIM_ADD, IconVa)End Function'删除通知栏图标(参数说明同Icon_Add)Function Icon_Del(iHwnd As Long, lIndex As Long) As LongDim IconVa As NOTIFYICONDATADim L As LongWith IconVa.HWnd = iHwnd.Uid = lIndex.cbSize = Len(IconVa)End With'当图标从托盘删除后,关闭时钟,不在监视单双击事件timMain.Enabled = True'让操作系统处理消息SetWindowLong iHwnd, GWL_WNDPROC, OldWindowProcIcon_Del = Shell_NotifyIcon(NIM_DELETE, IconVa)End Function'修改通知栏图标(参数说明同Icon_Add)Public Function Icon_Modify(iHwnd As Long, sTips As String, hIcon As Long, IconID As Lon g) As LongDim IconVa As NOTIFYICONDATAWith IconVa.HWnd = iHwnd.SzTip = sTips + Chr$(0).hIcon = hIcon.Uid = IconID.UCallbackMessage = TRAY_CALLBACK.cbSize = Len(IconVa).UFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIPEnd WithIcon_Modify = Shell_NotifyIcon(NIM_MODIFY, IconVa)End Function'程序处理Windows传递的消息Public Static Function NewWindowProc(ByVal HWnd As Long, ByVal Msg As Long, ByVal wP aram As Long, ByVal lParam As Long) As Long'如果用户点击了托盘中的图标,则进行判断是点击了左键单双击,还是右键If Msg = TRAY_CALLBACK Then'如果点击了右键If lParam = WM_RBUTTONDOWN Then'则弹出右键菜单frmMain.PopupMenu topmenuExit FunctionEnd If'如果双击左键If lParam = WM_LBUTTONDBLCLK ThenbIsDouble = TruebIsSingle = FalseExit FunctionEnd If'如果点击了左键If lParam = WM_LBUTTONDOWN ThenbIsSingle = TruebIsDouble = FalseExit FunctionEnd IfEnd If'如果是其他类型的消息则传递给原有默认的窗口函数NewWindowProc = CallWindowProc(OldWindowProc, HWnd, Msg, wParam, lParam)End Function'被时钟调用,来区分单击,还是双击,以及事件处理内容Public Sub procesSD()'如果是双击,就恢复窗口,并把图标从托盘删除If bIsDouble ThenCall Icon_Del(frmMain.HWnd, 0) '删除通知栏图标frmMain.Show '调出窗口frmMain.WindowState = 0frmMain.SetFocusElseIf bIsSingle Then'如果是单击,就弹出左键菜单frmMain.PopupMenu topmenuEnd If'恢复单双击判断的标志bIsSingle = FalsebIsDouble = FalseEnd Sub。

相关文档
最新文档