当前位置:编程学习 > VB >>

vb最小化到系统托盘


ciowangr911暗组

本文演示如何使用 Visual Basic Windows 任务栏或任务栏通知区域的充分利用。 选择的图标放鼠标 rested 通过它,将还原应用程序,单击时, 并将显示弹出式菜单时,将显示你选择的工具提示在任务栏通知区域时右键单击。 这可能因直接处理回调的 Visual Basic 的能力,因此利用完整 Shell_NotifyIcon 函数,为导出的 Shell32.dll 文件。

下面的示例可以添加到任何 Visual Basic 项目具有至少一个窗体和标准模块。

分步示例:
1. 将以下代码添加到项目中的标准模块的声明节:复制内容到剪贴板代码:
      user defined type required by Shell_NotifyIcon API call
      Public Type NOTIFYICONDATA
       cbSize As Long
       hwnd As Long
       uId As Long
       uFlags As Long
       uCallBackMessage As Long
       hIcon As Long
       szTip As String * 64
      End Type

      constants required by Shell_NotifyIcon API call:
      Public Const NIM_ADD = &H0
      Public Const NIM_MODIFY = &H1
      Public Const NIM_Delete = &H2
      Public Const NIF_MESSAGE = &H1
      Public Const NIF_ICON = &H2
      Public Const NIF_TIP = &H4
      Public Const WM_MOUSEMOVE = &H200
      Public Const WM_LBUTTONDOWN = &H201     Button down
      Public Const WM_LBUTTONUP = &H202       Button up
      Public Const WM_LBUTTONDBLCLK = &H203   Double-click
      Public Const WM_RBUTTONDOWN = &H204     Button down
      Public Const WM_RBUTTONUP = &H205       Button up
      Public Const WM_RBUTTONDBLCLK = &H206   Double-click

      Public Declare Function SetForegroundWindow Lib "user32" _
      (ByVal hwnd As Long) As Long
      Public Declare Function Shell_NotifyIcon Lib "shell32" _
      Alias "Shell_NotifyIconA" _
      (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean

      Public nid As NOTIFYICONDATA2. 将以下代码添加到你要响应系统送纸器图标或通知图标为你的应用程序项目中的任何形式:复制内容到剪贴板代码:
      Private Sub Form_Load()
       the form must be fully visible before calling Shell_NotifyIcon
       Me.Show
       Me.Refresh
       With nid
        .cbSize = Len(nid)
        .hwnd = Me.hwnd
        .uId = vbNull
        .uFlags = NIF_ICON or NIF_TIP or NIF_MESSAGE
        .uCallBackMessage = WM_MOUSEMOVE
        .hIcon = Me.Icon
        .szTip = "Your ToolTip" & vbNullChar
       End With
       Shell_NotifyIcon NIM_ADD, nid
      End Sub

      Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
      this procedure receives the callbacks from the System Tray icon.
      Dim Result As Long
      Dim msg As Long
       the value of X will vary depending upon the scalemode setting
       If Me.ScaleMode = vbPixels Then
        msg = X
       Else
        msg = X / Screen.TwipsPerPixelX
       End If
       Select Case msg
        Case WM_LBUTTONUP        514 restore form window
         Me.WindowState = vbNormal
         Result = SetForegroundWindow(Me.hwnd)
         Me.Show
        Case WM_LBUTTONDBLCLK    515 restore form window
         Me.WindowState = vbNormal
         Result = SetForegroundWindow(Me.hwnd)
         Me.Show
        Case WM_RBUTTONUP        517 display popup menu
         Result = SetForegroundWindow(Me.hwnd)
         Me.PopupMenu Me.mPopupSys
       End Select
      End Sub

      Private Sub Form_Resize()
       this is necessary to assure that the minimized window is hidden
       If Me.WindowState = vbMinimized Then Me.Hide
      End Sub

      Private Sub Form_Unload(Cancel As Integer)
       this removes the icon from the system tray
       Shell_NotifyIcon NIM_Delete, nid
      End Sub

      Private Sub mPopExit_Click()
       called when user clicks the popup menu Exit command
       Unload Me
      End Sub

      Private Sub mPopRestore_Click()
       called when the user clicks the popup menu Restore command
       Dim Result As Long
       Me.WindowState = vbNormal
       Result = SetForegroundWindow(Me.hwnd)
       Me.Show
      End Sub3.使同一窗体添加以上代码上的以下的属性设置:引用:
   Property         Required Setting for Taskbar Notification Area example
   -----------------------------------------------------------------------
   Icon           = The icon you want to appear in the system tray.
   Minbutton      = True
   ShownInTaskbar = False4.向同一个窗体使用菜单编辑器中添加下面的菜单项:引用:
   Caption      Name          Enabled   Visible   Position
   ---------------------------------------------------------
   &SysTray     mPopupSys      True      False    Main L

补充:软件开发 , Vb ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,