vb将程序最小化到托盘
程序最小化到托盘
VC++6.0中实现将应用程序的图标加入到Windows的系统托盘中(就是为应用程序在桌面右下角添加一个图标,和QQ类似)有位老兄说我乱贴,可能这位老兄已经看得眼花缭乱了,Shell_NotifyIcon 是一个Windows SDK的一个函数,只要你在windows下编程,不管你是BCB、VC、VB,都可以调用它来实现托盘图标,只是调用方法不同罢了。
常常能见到一些优秀的软件在运行后会将其应用程序图标加入到系统托盘中。
如果能将自己编写的应用程序的图标也加入到系统托盘中,将会使你的程序显得很有专业水准。
其实这并不困难,与系统托盘通信的函数只有一个:Shell_NotifyIcon (UINT message, NOTIFYICONDATA &m_nid);首先看一下该函数的两个参数。
第一个参数message可以取以下值:NIM_ADD 向托盘中加入一个图标; NIM_MODIFY 修改托盘中的图标;NIM_DELETE 从托盘中删除一个图标;第二个参数m_nid是NOTIFYICONDATA结构的一个引用。
该结构的原型如下:typedef struct _NOTIFYICONDATA{DWORD cbSize;// 结构的大小,必须在程序中给出HWND hWnd; //是你程序中将要接收托盘消息的窗口句柄UINT uID; // 应用程序中定义的托盘图标ID,此参数用作标识UINT uFlags; //设置属性,低三位有意义,0--7,如下://第一位//#define NIF_MESSAGE 0x1 // uCallbackMessage参数有效//第二位//#define NIF_ICON 0x2 // hIcon参数有效//第三位//#define NIF_TIP 0x4 // szTip参数有效UINT uCallbackMessage; // 自定义的消息ID值,一定不要与以有的消息ID相重。
VBA修改窗口为最小化、最大化代码
VBA修改窗口为最小化、最大化代码 以下的VBA代码,均于Excel中的VBA有关,是用来修改应用程序的相关属性的。
代码收藏如下。
Application.WindowState = xlMinimized ‘窗口最小化 Application.WindowState =xlMaximized 最大化 Application.WindowState =xlNormal 为正常 Application.Cursor = xlIBeam ‘设置光标形状为Ⅰ字形 Application.Cursor = xlWait 为沙漏(等待)形 Application.Cursor = xlNormal 为正常 Application.StatusBar = "大众计算机" ’在地址栏中显示文本 Application.Caption= "大众计算机" 修改标题栏的文字. 其它的知识 Application.TemplatesPath ‘获取工作簿模板的位置 Application.CalculateFull ’重新计算所有打开的工作簿中的数据 Application.RecentFiles.Maximum = 3 ’将最近使用的文档列表数设为3 Application.RecentFiles(6).Open ’打开最近打开的文档中的第6个文档 Application.AutoCorrect.AddReplacement "good", "大众计算机" ’自动将输入的"good"更正为"大众计算机" Application.Dialogs(xlDialogPrint).Show ‘显示打印文档的对话框。
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运行!
几乎不用编写代码,一个托盘程序就这样轻松实现了。
最小化到推盘
1、在资源中的Icon中导入一个自己喜欢的图标,ID命名为IDR_MAINFRAME,将先前的IDR_MAINFRAME的图标删除掉;2、在自己的Dialog头文件中定义一个变量NOTIFYICONDATA m_nid,关于该结构体的具体信息可以查阅MSDN;3、添加消息响应函数OnInitDialog(),并在该函数中添加以下代码,这样程序一启动,就在托盘中显示出了自己应用程序的图标。
//---------------------------托盘显示---------------------------------//m_nid.cbSize = (DWORD)sizeof(NOTIFYICONDATA);m_nid.hWnd = this->m_hWnd;m_nid.uID = IDR_MAINFRAME;m_nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP ;m_nid.uCallbackMessage = WM_SHOWTASK; // 自定义的消息名称m_nid.hIcon =LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));strcpy(m_nid.szTip, "服务器程序"); // 信息提示条为"服务器程序",VS2008 UNICODE编码用wcscpy_s()函数Shell_NotifyIcon(NIM_ADD, &m_nid); // 在托盘区添加图标这时候编译,会报WM_SHOWTASK的错,因为该消息要自己定义。
4、在Dialog头文件中声明消息函数原型LRESULT OnShowTask(WPARAM wParam, LPARAM lParam);5、在Dialog源文件中进行消息映射ON_MESSAGE(WM_SHOWTASK,OnShowTask)6、在Dialog源文件中添加自定义的消息响应代码,左键双击弹出应用程序主窗口,右键单击弹出菜单。
最小化到托盘
If Me.WindowState = vbMinimized Then
cSysTray1.InTray = True
Me.Visible = FalseEnd If
End SLeabharlann b 点击托盘图标后让程序显示出来,如下:
Private Sub cSysTray1_MouseDown(Button As Integer, Id As Long)
Me.WindowState = vbNormal
Me.Visible = True
cSysTray1.InTray = False
Me.SetFocus
End Sub
然后在自己的VB程序中添加改控件(工程-部件-浏览)。
最好把这个ocx放到C:\WINDOWS\system32。
改控件的属性InTray属性用来设置是否显示在托盘中,True为显示在托盘,False为不显示。TrayIcon属性是在托盘中显示的图标式样。TrayTip属性是鼠标移到改控件上面显示的提示文字。如果要使程序最小化时显示到托盘,如下:
单击“最小化”按钮(标题栏的右边)将窗口缩小为任务栏上右边的图标,同时隐藏原窗体。单击或者右键托盘化的图标,可以重现原窗体。
vb中有部分api可以实现将窗体最小化的功能,但是非常繁琐。这里有一个非常简单的办法:可以用微软封装好的控件来实现,不过该控件并不在VB安装目录中,需要找到VB的安装盘(不是安装以后的目录)的COMMON\TOOLS\VB\UNSUPPRT\SYSTRAY目录,将Systray目录拷到硬盘上面并编译为ocx控件(编译前记得要先把只读属性修改掉,有时候会提示要先保存,直接保存。),
用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( )事件是在窗体激活的时候才触发的。
最小化到托盘
系统最小化到托盘一、托盘简介所谓的“托盘”,在Windows系统界面中,指的就是下面任务条右侧,有系统时间等等的标志的那一部分。
在程序最小化或挂起时,但有不希望占据任务栏的时候,就可以把程序放到托盘区。
二、托盘编程相关函数其实,把程序放到托盘上的本质就是先在托盘区绘制一个图标,然后把程序隐藏不见,再对托盘的图标进行消息处理,就可以了。
绘制图标以及确定图标所传送消息的函数只有一个,WINSHELLAPI BOOL WINAPI Shell_NotifyIcon(DWORD dwMessage,PNOTIFYICONDATA pnid);这个函数负责向系统传递消息,以添加、修改或删除托盘区的图标。
她的返回值是个布尔类型的。
就是说,如果返回0才成功。
参数dwMessage 是表示这个函数的应用功能是哪一方面,是添加、删除,还是修改图标。
如果是添加,则它的值为NIM_ADD;删除则是NIM_DELETE;而修改是NIM_MODIFY。
参数pnid就是具体的和程序在托盘区的图标有关系的结构了。
它的定义如下:typedef struct _NOTIFYICONDATA {DWORD cbSize;HWND hWnd;UINT uID;UINT uFlags;UINT uCallbackMessage;HICON hIcon;char szTip[64];} NOTIFYICONDATA, *PNOTIFYICONDATA;下面就对该结构各个参数进行刨析:cbSize : 结构的长度,用“位”来做单位。
一般在程序中,我们用(DWORD)sizeof(NOTIFYICONDATA) 给它赋值。
HWnd : 一个句柄,如果对托盘中的图标进行操作,相应的消息就传给这个句柄所代表的窗口。
大多数情况下是this->m_hWnd。
uID : 在工程中定义的图标IDuFlags : 这个成员标志着其他哪些成员的数据是有效的,分别为NIF_ICON, NIF_MESSAGE, NIF_TIP,分别代表着数据有效的成员是hIcon, uCallbackMessage, szTip。
怎么能让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的窗体处理函数。
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源码+设计思路
闹铃软件D2.0版(附界面,VB源码)——值得参考的VB源码作者:西门吹雪本文适用于初,中级VB程序设计爱好者做研究参考。
高级程序设计者及精通API的飘过,看懂这个程序后相信你能很熟练VB。
笔者认为:程序设计主要靠两方面的能力,一是技术,二是思路。
技术上的问题,都不是问题;自己的思路则是你成功的首要前提。
本程序的一些巧妙的设计可以为大家提供借鉴。
下面先介绍一下主要功能吧:1·启动定时后,时间到后播放器开始播放音乐;2·支持用户自定义歌曲;3·程序自动记录用户信息:歌曲列表,所定时间,选项,主题等,重启程序时加载用户信息;4·简单数据库记录歌曲路径等;5·支持用户定两个时间;6·用户点击关闭时询问;7·用户点击最小化按钮时,最小化到托盘;8·实现托盘菜单;9·支持顺序播放音乐列表。
10·实现标准时间显示以下是本程序的界面截图:1·托盘图标及托盘菜单(实现最小化到托盘)2·主菜单截图3·音乐框菜单截图(实现列表框右键选中项目)4·退出询问截图(实现关闭程序不直接退出)5·主界面截图主程序代码:'***********************************发送按键Private Declare Sub keybd_event Lib "user32" (ByV al bVk As Byte, ByV al bScan As Byte, ByV al dwFlags As Long, ByV al dwExtraInfo As Long)Const KEYEVENTF_KEYUP = &H2'************************************Option ExplicitPrivate Declare Function LockWorkStation Lib"user32.dll" () As Long'变量列表:a1,b1,c1,d1,d2,c3,d3,n,mDim a1 As String, b1 As String, c1 As String, d1 As StringDim d2 As String, c3 As Integer, d3 As String, n, shun As IntegerDim m, picu As String'************用户数据数组'变量列表:pta,mpn,lb1,lb2,op1,op2,conPrivate Type configpic As String '背景pta As String '用户路径mpn As String '歌曲名字lb1 As String '时间1lb2 As String '时间2op1 As Integer '开关1op2 As Integer '开关2End TypeDim con As config'歌曲信息数组'变量列表:nam,pth,patPrivate Type lujingnam As String * 16pth As String * 64End TypeDim pat As lujingDim thr As Integer'********************************************* ****API实现右键选中Private Declare Sub mouse_event Lib "user32" (ByV al dwFlags As Long, ByV al dx As Long, ByV al dy As Long, ByV al cButtons As Long, ByV al dwExtraInfo As Long)Const MOUSEEVENTF_LEFTDOWN = &H2Const MOUSEEVENTF_LEFTUP = &H4'载入自动编码函数(原创)'变量列表:mnFunction bianma(name As String) As IntegerDim mn As StringIf name = "" Then bianma = 1000If name <> "" Thenmn = Asc(Left(name, 1)) & Asc(Right(name, 1)) &Len(name)bianma = Abs(Mid(mn, 2, 1) & Right(mn, 1) & Len(mn) & Mid(mn, Fix(Len(mn) / 2), 1))End IfEnd Function'载入判断文件类型函数,成员为文件全名(原创)。
VB6.0实现窗体最小化时到系统托盘区
VB6.0将窗体最小化到系统托盘区使用说明:共有两个方法,经个人实际测试(环境:xpsp3 vb6.0)第二个控件法最有效果,第一个方法在启动时确实在右下角,但是弹出后再次最小化就跑到屏幕左下角了,无法最小化,不知道是啥原因,希望大家改进代码。
本人力荐第二种小巧有效简单的方法。
----------------------------------------------------------------------------------------------------方法一托盘,亦即程序最小化后程序图标隐藏到屏幕右下角的任务栏里。
这种最小化方法比普通的最小化节约空间,因此备受欢迎,很多软件都具有这样的功能。
在VB中,可用API 函数来实现托盘功能,但比较复杂,不太好操作。
相对而言采用一种非常简单易行的方法:控件法,才是最适合最方便的。
下面要使用已被微软封装好的控件:csystray1(名称可自定)。
说到控件,大家可能要皱眉头了:去哪找呀?放心!这个控件就躺在你的VB安装盘里(目录为:COMMON\TOOLS\VB\UNSUPPRT\SYSTRAY)!不过,得处理一下:请在Tool 文件夹里查找一个名叫Systray的目录,将它拷贝到硬盘,用VB打开该目录下的工程,必要的话可作些改进(如更换图标、添加功能等),然后编译成ocx控件,可以随意命名。
为方便使用起见,最好编译到Windows\System目录下。
好了,现在我们就可以在我们的程序中使用此控件来轻松实现托盘功能了。
vb中添加控件的方法就不用我说了吧?----------------------------------------------------------------------------------------------------------控件的属性和事件浅析:该控件的InTray属性是用来设置是否显示在托盘中,True为显示在托盘,False为不显示;该控件的TrayIcon属性是在托盘中显示的图标式样;该控件的TrayTip属性是鼠标移动到该控件上面时,显示的提示文字;该控件的重要事件是几个我们常用的鼠标事件:按下、放开、移动、双击,编程时就是利用这些事件达到在任务栏中控制程序的目的。
VB代码VB小程序:在系统托盘为程序添加图标
VB代码VB小程序:在系统托盘为程序添加图标当前位置:首页> VB小程序1-99 > 在系统托盘为程序添加图标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 = "系统托盘例子"'为数组菜单 mmFast 添加条目mFast.Visible = FalseLoadKjZu mmFast, "显示 " & Me.Caption & "(&R)", "mmFast-Run"LoadKjZu mmFast, "最小化窗口(&N)", "mmFast-Min"LoadKjZu mmFast, "菜单例子 1 (&A)", "mmFast-A"LoadKjZu mmFast, "菜单例子 2 (&B)", "mmFast-B"LoadKjZu mmFast, "-" '菜单分隔条LoadKjZu mmFast, "退出(X)", "mmFast-Exit"Dim S As SingleLabel1.AutoSize = True: S = Label1.HeightLabel1.Caption = Me.Caption: Label1.Move S, SCommand1.Caption = "添加到系统托盘": Command1.Move S, S * 3, S * 11, S * 2Command2.Caption = "最小化到系统托盘": Command2.Move S, S * 6, S * 11, S * 2Command3.Caption = "从系统托盘删除图标": 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, "mmFast-Min")).Enabled = nEnabledmmFast(KjZuIndex(mmFast, "mmFast-Run")).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 <> "" ThenI = I + 1Load Kj(I): Kj(I).Visible = TrueEnd IfKj(I).Caption = nCapIf nCap = "-" Then Kj(I).Tag = "bar" 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 <> 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("mmFast-Run"): Call WinNormal '正常显示主窗口Case UCase("mmFast-Min"): Call WinMinimized '缩小到系统托盘Case UCase("mmFast-Exit"): Unload Me '退出程序Case UCase("mmFast-A"): MsgBox "这是系统托盘快捷菜单:mmFast-A", vbInformation, Me.Caption'Case UCase("mmFast-B")Case Else: MsgBox "此命令在“mmFast_Click”中无效:" & vbCrLf & vbCrLf & "nCmd = " & nCmd, vbInformati on, Me.Caption & " - 无效命令"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 = "缩小到系统图标"End SubPrivate Sub SysTray(Optional IcoDel As Boolean)'在系统托盘为本程序添加图标Dim dl As LongIf IcoDel Thendl = Shell_NotifyIcon(NIM_DELETE, ctTrayIco)If dl = 0 ThenLabel1.Caption = "从系统托盘删除图标,失败" ElseLabel1.Caption = "已从系统托盘删除图标,成功" 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 = "我的系统托盘图标" & Chr(0)dl = Shell_NotifyIcon(NIM_ADD, ctTrayIco)If dl = 0 ThenLabel1.Caption = "图标添加到系统托盘,失败" ElseLabel1.Caption = "图标已添加到系统托盘,成功" 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 '图标提示信息,设置为:"字符串" & Chr(0)End TypePublic Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_N otifyIconA" (ByVal dwMessage As Long, lpData As NotifyIconData) As LongPublic Const SysTray_ID = 1Public Const NIM_ADD = &H0Public Const NIM_DELETE = &H2Public Const NIM_MODIFY = &H1Public Const NIF_MESSAGE = &H1Public Const NIF_ICON = &H2Public Const NIF_TIP = &H4Public Const WM_USER = &H400Public Const WM_Tray = WM_USER + 2Private Declare Function CallWindowProc Lib "user32" Alias "CallWindo wProcA" (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 "user32" Alias "GetWind owLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As LongPrivate Declare Function SetWindowLong Lib "user32" Alias "SetWindo wLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As L ongPublic Const GWL_WNDPROC = (-4)Public Const WM_KEYDOWN = &H100Public Const WM_KEYUP = &H101Public Const WM_CHAR = &H102Public Const WM_SYSKEYDOWN = &H104Public Const WM_SYSKEYUP = &H105Public Const WM_SYSCHAR = &H106Public Const WM_MOUSEMOVE = &H200Public Const WM_LBUTTONDOWN = &H201Public Const WM_LBUTTONUP = &H202Public Const WM_RBUTTONDOWN = &H204Public Const WM_RBUTTONUP = &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'这是自定义的窗口回调函数。
启动后缩小到托盘
ExitMainForm();
n 双击托盘上图标时,显示窗体
private void notifyIcon_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
#endregion
4。可以为NotifyIcon加一个ContextMenuStrip右键菜单menu_Notify。
5。本例子禁用了窗体最大化按钮。(设置窗体的属性MaximizeBox的属性为false)
主体实现代码:
C#代码
#region 私有方法 处理窗体的 显示 隐藏 关闭(退出)
private void ExitMainForm()
{
e.Cancel = true;
HideMainForm();
}
#endregion
在这里需要注意的是控件的名称要与这里的处理函数体一致。
{
this.notifyIcon.Visible = false;
this.Close();
this.Dispose();
Application.Exit();
ShowMainForm();
}
}
#endregion
#region 点最小化按钮时,最小化到托盘
private void frmMain_SizeChanged(object sender, EventArgs e)
}
#endregion
WinForm最小化到系统托盘实例代码
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
public int GetCount(string str,char s)
{
e.Cancel = false;
}
else
Hale Waihona Puke { e.Cancel = true;
NormalToMinimized();
MessageBox.Show(GetCount(s,'h').ToString());
}
}
}
namespace System
{
public class MyClass
{
public int MyProperty { get; set; }
doc.Load(path);
var node = doc.SelectSingleNode(@"configuration/appSettings");
var l=node.ChildNodes.OfType<XmlElement>().ElementAt(0);
#endregion
#endregion
var s = ConfigurationManager.AppSettings.Get("CloseApp");
MessageBox.Show(s);
}
}
/// <summary>
/// 点击托盘图标时,显示窗体
/// </summary>
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消息。
如何用VISUAL BASIC编写托盘程序
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
利用VisualC++实现系统托盘程序
利用VisualC++实现系统托盘程序利用Visual C++实现系统托盘程序这些程序运行时不显示运行窗口,只在任务栏上显示一个图标,表示程序正在运行,用户可以通过鼠标与应用程序交互,程序开发人员有时也需要编制一些仅在后台运行的类似程序,为了不干扰前台程序的运行界面和不显示不必要的窗口,应使程序运行时的主窗口不可见。
同时将一个图标显示在任务栏右端静态通告区中并响应用户的鼠标动作。
本实例就介绍Visual C++开发这类程序的设计方法,该程序编译运行后,如果双击托盘图标,程序会弹出一个消息列表窗口,只要鼠标在托盘图标上移动或点击(无论是左右键的单击或双击),产生的消息都会显示在这个窗口里;当鼠标光标移到托盘图标上时,在图标附近会显示提示信息;单击右键时弹出上下文菜单,这个菜单中应包含打开属性页的命令或者打开与图标相关的其它窗口的命令,另外,该程序还可以动态的改变托盘的图标。
参照这个例子,相信读者能轻松自如地在自己的程序中应用系统托盘。
一、实现方法为了实现拖盘程序,首先要使程序的主窗口不可见,这点实现起来十分容易,只要调用ShowWindow(SW_HIDE)就可以了,本实例采用的就是这种方法,还有一种思路是通过分别设置主边框窗口的风格和扩展风格来隐藏主框架:在任务条上显示图标是利用系统API函数Shell_NotifyIcon()来将一个图标显示在任务栏的通告区中。
该函数的原型为:该函数的第一个参数dwMessage类型为DWORD,表示要进行的动作,它可以是下面的值之一:NIM_ADD:添加一个图标到任务栏。
NIM_MODIFY:修改状态栏区域的图标。
NIM_DELETE:删除状态栏区域的图标。
NIM_SETFOCUS:将焦点返回到任务栏通知区域。
当完成用户界面操作时,任务栏图标必须用此消息。
例如,如果任务栏图标正显示上下文菜单,但用户按下"ESCAPE"键取消操作,这时就必须用此消息将焦点返回到任务栏通知区域。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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 LongPublic 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 + &H100 Public 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 Long Dim 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消息。
ano.uCallbackMessage = WM_NOTIFYICONIcon_Add = Shell_NotifyIcon(NIM_ADD, ano)End FunctionFunction DialogProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long'该函数为Form2的窗口处理函数。
Dim l As LongDim l1 As LongDim po As POINTAPISelect Case uMsgCase WM_INITDIALOGCase WM_DESTROYCase WM_COMMANDCase WM_DRAWITEMCase WM_NOTIFYICON '有鼠标事件产生Select Case lParamCase WM_LBUTTONDOWN '按下鼠标左键'提示是否删除图标l = MsgBox("Delete icon?", vbYesNo)If l = vbYes Then'删除图标同时恢复窗口处理函数l = Icon_Del(hwnd)l = SetWindowLong(Form2.hwnd, GWL_WNDPROC, lproc)Form1.ShowElseEnd IfCase WM_RBUTTONDOWN '按下鼠标右键弹出菜单If submenu Thenl = GetCursorPos(po) '获的光标位置'在光标位置处弹出菜单l1 = TrackPopupMenu(submenu, (TPM_LEFTALIGN Or TPM_RIGHTBUTTON), po.x, po.y, 0, Form1.hwnd, vbNull)End IfCase ElseEnd SelectCase ElseDialogProc = FalseEnd SelectDialogProc = TrueEnd FunctionPrivate Sub CAdd_Click()Dim l As LongIf (Icon_Add(Form2.hwnd, Picture1.Picture)) Thenxb = CMenu() '添加弹出菜单CAdd.Enabled = FalseForm1.Hide'将DialogProc函数设置为Form2的窗口处理函数并且保存原来窗口处理函数句柄lproc = SetWindowLong(Form2.hwnd, GWL_WNDPROC, AddressOf DialogProc) End IfEnd SubPrivate Sub CEnd_Click()EndEnd SubPrivate Sub mItem_Click(Index As Integer)MsgBox "You click item " + Str$(Index + 1)End Sub。