none
vb2010 如何使用托盘? RRS feed

  • 问题

  • 如何能最小化到任务栏栏托盘?

     

    我是VB新手,请详细介绍一下,或者推荐一些好的中文教程。

    2010年7月28日 6:11

答案

全部回复

  • Option Explicit
    Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
    Private Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
    Private Declare Function SetMenuItemBitmaps Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal hBitmapUnchecked As Long, ByVal hBitmapChecked As Long) As Long
    Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
    
    Private Const MAX_TOOLTIP As Integer = 64
    Private Const NIF_ICON = &H2
    Private Const NIF_MESSAGE = &H1
    Private Const NIF_TIP = &H4
    Private Const NIM_ADD = &H0
    Private Const NIM_DELETE = &H2
    Private Const WM_MOUSEMOVE = &H200
    Private Const WM_LBUTTONUP = &H202
    Private Const WM_RBUTTONUP = &H205
    
    Private Type NOTIFYICONDATA
      cbSize      As Long
      hwnd       As Long
      uID       As Long
      uFlags      As Long
      uCallbackMessage As Long
      hIcon      As Long
      szTip      As String * MAX_TOOLTIP
    End Type
    
    Private Const MF_bitmap = &H4&
    Private nfIconData As NOTIFYICONDATA
    
    Private Sub Sys_Tray()
        Dim Mnu_Hwnd As Long
        Dim Mnu_Id As Long
        Dim i As Long
        nfIconData.hwnd = Me.hwnd
        nfIconData.uID = Me.Icon
        nfIconData.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
        nfIconData.uCallbackMessage = WM_MOUSEMOVE
        nfIconData.hIcon = Me.Icon.Handle
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        nfIconData.szTip = "改成你想要的文字" & vbNullChar
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        nfIconData.cbSize = Len(nfIconData)
        Call Shell_NotifyIcon(NIM_ADD, nfIconData)
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        Call Shell_NotifyIcon(NIM_DELETE, nfIconData)
    End Sub
    Private Sub Form_Load()
        Sys_Tray
        With Me
          .Visible = False
        End With
    End Sub
    
    ';--------------     以上没菜单的任务托盘
    
    
    
    Option Explicit
    Private Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
    Private Declare Function GetMenuItemID Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
    Private Declare Function SetMenuItemBitmaps Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal hBitmapUnchecked As Long, ByVal hBitmapChecked As Long) As Long
    Private Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
    
    Private Const MAX_TOOLTIP As Integer = 64
    Private Const NIF_ICON = &H2
    Private Const NIF_MESSAGE = &H1
    Private Const NIF_TIP = &H4
    Private Const NIM_ADD = &H0
    Private Const NIM_DELETE = &H2
    Private Const WM_MOUSEMOVE = &H200
    Private Const WM_LBUTTONUP = &H202
    Private Const WM_RBUTTONUP = &H205
    
    Private Type NOTIFYICONDATA
      cbSize      As Long
      hwnd       As Long
      uID       As Long
      uFlags      As Long
      uCallbackMessage As Long
      hIcon      As Long
      szTip      As String * MAX_TOOLTIP
    End Type
    
    Private Const MF_bitmap = &H4&
    Private nfIconData As NOTIFYICONDATA
    
    Private Sub Sys_Tray()
        Dim Mnu_Hwnd As Long
        Dim Mnu_Id As Long
        Dim i As Long
        nfIconData.hwnd = Me.hwnd
        nfIconData.uID = Me.Icon
        nfIconData.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
        nfIconData.uCallbackMessage = WM_MOUSEMOVE
        nfIconData.hIcon = Me.Icon.Handle
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        nfIconData.szTip = "改成你想要的文字" & vbNullChar
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        nfIconData.cbSize = Len(nfIconData)
        Call Shell_NotifyIcon(NIM_ADD, nfIconData)
    End Sub
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
       Dim lMsg As Single
        lMsg = X / Screen.TwipsPerPixelX
        If lMsg = WM_RBUTTONUP Then
         MyTrayMnu.Visible = True
         Me.PopupMenu 需要的右键菜单(就是菜单)
        End If
    
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        Call Shell_NotifyIcon(NIM_DELETE, nfIconData)
    End Sub
    Private Sub Form_Load()
        Sys_Tray
        With Me
          .Visible = False
        End With
    End Sub
    
    
    ''‘’‘’‘’‘’‘’‘’‘’  有右健菜单 
    2010年7月28日 6:33
  • 这个有点像VB6的源码...特别是API的引用和常量申明。。。
    2010年7月28日 6:41
  • 直接使用NotifyIcon控件就行了嘛。具体参见

    NotifyIcon 组件可以实现:

    http://msdn.microsoft.com/zh-cn/library/a551e3ba.aspx

    2010年7月28日 15:46