Programm in die Taksbar

F

fLoOmY

Wie setzte ich ein Programm (also, das icon von einem...bessergesagt: meinem :) ) Programm in die Taskbar, also neben die Uhr?

Außerdem: Wie bekomme ich dann bei Klick ein Kontextmenü, wo ein Eintrag fett geschrieben ist?

*nerv*

Brauch ich für meine Software "Quicklogin" :)
 

Thomas Kuse

Erfahrenes Mitglied
tray

hier für deinen tray-icon:


Code:
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias _
"Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As _
NOTIFYICONDATA) As Long

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

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

'deine eigene konstante erstellen
Public Const NIF_DOALL = NIF_MESSAGE Or NIF_ICON Or NIF_TIP

Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_RBUTTONDOWN = &H2042) 

'neue form zum projekt dazu und dann den folgenden code einfügen

Public Sub CreateIcon()
Dim Tic As NOTIFYICONDATA
Tic.cbSize = Len(Tic)
Tic.hwnd = Picture1.hwnd
Tic.uID = 1&
Tic.uFlags = NIF_DOALL
Tic.uCallbackMessage = WM_MOUSEMOVE
Tic.hIcon = Picture1.Picture
Tic.szTip = "Visual Basic Demo Project" & Chr$(0)
erg = Shell_NotifyIcon(NIM_ADD, Tic)
End Sub

Public Sub DeleteIcon()
Dim Tic As NOTIFYICONDATA
Tic.cbSize = Len(Tic)
Tic.hwnd = Picture1.hwnd
Tic.uID = 1&
erg = Shell_NotifyIcon(NIM_DELETE, Tic)
End Sub3) 
'zwei buttons zufügen (command1 and command2) und ne picture box (picture1) picture eigenschaft der form -> icon auswählen

'To to Click event of Command1, add the following code:

DeleteIconTo ( MouseMove event von Picture1 ):

X = X / Screen.TwipsPerPixelXSelect Case X
Case WM_LBUTTONDOWN
Caption = "Left Click"
Case WM_RBUTTONDOWN
Caption = "Right Click"
Case WM_MOUSEMOVE
Caption = "Move"
Case WM_LBUTTONDBLCLK
Caption = "Double Click"
End Select4)

starte das projekt.
klicke auf Command1. das icon dass du Picture1 ausgewählt hast sollte jetzt im tray sein.
jetzt, bewege die mouse über das icon. caption sollte jetzt auf "Move" stehn.
links click auf das icon, und die caption ist "Left"
rechts click auf das icon, und die caption wird zu "Right"
Double Click auf das icon, und die caption ändert sich zu "Double".
soll das icon ausm tray raus clicke auf Command2.
 

Nagual

Mitglied
Hier noch eine Version dessen wie du die sache ins Tray senden kannst
mit dem rechten mausklick auf icon kannst du es dann wieder anzeigen lassen..



Option Explicit
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
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 Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205
Public Const WM_ACTIVATEAPP = &H1C
Public Const NIF_ICON = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_TIP = &H4
Public Const NIM_ADD = &H0
Public Const NIM_DELETE = &H2
Public Const MAX_TOOLTIP As Integer = 64
Public Const GWL_WNDPROC = (-4)

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
Public nfIconData As NOTIFYICONDATA
Private FHandle As Long
Private WndProc As Long
Private Hooking As Boolean

Public Sub AddIconToTray(MeHwnd As Long, MeIcon As Long, MeIconHandle As Long, Tip As String)
With nfIconData
.hwnd = MeHwnd
.uID = MeIcon
.uFlags = NIF_ICON Or NIF_MESSAGE Or NIF_TIP
.uCallbackMessage = WM_RBUTTONUP
.hIcon = MeIconHandle
.szTip = Tip & Chr$(0)
.cbSize = Len(nfIconData)
End With
Shell_NotifyIcon NIM_ADD, nfIconData
End Sub

Public Sub RemoveIconFromTray()
Shell_NotifyIcon NIM_DELETE, nfIconData
End Sub

Public Sub Hook(Lwnd As Long)
If Hooking = False Then
FHandle = Lwnd
WndProc = SetWindowLong(Lwnd, GWL_WNDPROC, AddressOf WindowProc)
Hooking = True
End If
End Sub

Public Sub Unhook()
If Hooking = True Then
SetWindowLong FHandle, GWL_WNDPROC, WndProc
Hooking = False
End If
End Sub

Public Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Hooking = True Then
If uMsg = WM_RBUTTONUP And lParam = WM_RBUTTONDOWN Then
frmMain.SysTrayMouseEventHandler
WindowProc = True
Exit Function
End If
WindowProc = CallWindowProc(WndProc, hw, uMsg, wParam, lParam)
End If
End Function


On the Main form, In my example, I was using a menu to to this:


Private Sub mnuTimeTray_Click()
mnuTimeTray.Enabled = False
mnuTimeRestore.Enabled = True
Hook Me.hwnd
AddIconToTray Me.hwnd, Me.Icon, Me.Icon.Handle, App.Title
Me.Hide
End Sub

Private Sub mnuTimeRestore_Click()
mnuTimeTray.Enabled = True
mnuTimeRestore.Enabled = False
Unhook
Me.Show
RemoveIconFromTray
End Sub

Public Sub SysTrayMouseEventHandler()
SetForegroundWindow Me.hwnd
PopupMenu mnuTime, vbPopupMenuRightButton
End Sub