Double clic sur icone system tray

Résolu
jordangj Messages postés 63 Date d'inscription lundi 12 juillet 2004 Statut Membre Dernière intervention 8 mai 2007 - 13 juil. 2004 à 00:16
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 - 13 juil. 2004 à 15:55
Bonjour je voudrais réouvrir mon form l'orsque l' on double clic sur l'icone.
merci
Jordan

10 réponses

Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
13 juil. 2004 à 10:56
n'utilise pas ce code.....
il utilise du SubClassing, et VB doit planter très souvent, non ?

ajoutes un bouton nommé "IconHook" et rends le invisible.

code du module :

Private 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

Private Const NIM_ADD = &H0
Private Const NIM_DELETE = &H2
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4

Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_RBUTTONUP = &H205

Private Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean

Dim NID As NOTIFYICONDATA

Public Sub AddIconToTray(HookHwnd As Long, Icon As Long, Tip As String)
    With NID
        .cbSize = Len(NID)
        .hWnd = HookHwnd
        .uId = 1&
        .uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
        .ucallbackMessage = WM_LBUTTONDOWN
        .hIcon = Icon
        .szTip = Tip & vbNullChar
    End With
    Shell_NotifyIcon NIM_ADD, NID
End Sub

Public Sub RemoveIconFromTray()
    Shell_NotifyIcon NIM_DELETE, NID
End Sub


et de la Form :
Private Sub IconHook_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    '# Si le message atteste d'un double click...
    If X / Screen.TwipsPerPixelX = WM_LBUTTONDBLCLK Then
        Me.Show
    End If
End Sub

Private Sub Form_Load()
    Me.Hide
    AddIconToTray IconHook.hWnd, Me.Icon.Handle, "This is a test tip"
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    RemoveIconFromTray
End Sub
3
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
13 juil. 2004 à 07:44
quand tu a utilisé l'API Shell_NotifyIcon, tu as du fournir un parametre de type NOTIFYICONDATA.

renseigne dans celui-ci les champs suivants :
hwnd -> le hWnd d'un controle lambda.... (un bouton, par exemple) :
NID.hWnd = Cmd_Hook.hwnd

ajoutes les constantes suivantes dans la partie declaration d'une form, ou dans un module :

Const WM_LBUTTONDBLCLK = &H203
Const WM_LBUTTONDOWN = &H201

Tu dois ensuite modifier le champ ucallbackMessage de ton NOTIFYICONDATA :
NID.ucallbackMessage = WM_LBUTTONDOWN

tu recevra un evenement Cmd_Hook_MouseDown lorsque tu va cliquer sur l'icone de la Systray.

Private Sub Cmd_Hook_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'# Si le message atteste d'un double click...
If X / Screen.TwipsPerPixelX = WM_LBUTTONDBLCLK Then
Me.show
End If
End Sub
0
jordangj Messages postés 63 Date d'inscription lundi 12 juillet 2004 Statut Membre Dernière intervention 8 mai 2007
13 juil. 2004 à 09:59
eu je débute voici le code de mon module:

Option Explicit
' This code was adapted from original system tray module published by Ben Baird.
' Tray Icon add/remove functions implemented within this module.
' Created by E.Spencer (elliot@spnc.demon.co.uk) - This code is public domain.
' Added call back to handle mouse events correctly

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     ' Storage for form handle
Private WndProc As Long     ' Address of our handler
Private Hooking As Boolean  ' Hooking indicator

' Add your application to the system tray.
' Param 1 = Handle of form (which deals with sys tray events)
' Param 2 = Icon (form icon - any icon)
' Param 3 = Handle of icon (form icon - any icon)
' Param 4 = Tip for sys tray icon.
'
' Example - AddIconToTray Me.Hwnd, Me.Icon, Me.Icon.Handle, "This is a test tip"
'
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

' Remove your application from the system tray.
' Call when you quit your application.
'
Public Sub RemoveIconFromTray()
Shell_NotifyIcon NIM_DELETE, nfIconData
End Sub

' Call this routine to ensure my app gets notified of all events
' Example - Hook Me.hWnd
'
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

' Call this routine to transfer event notification back to standard handler
' Example - Unhook
'
Public Sub Unhook()
If Hooking = True Then
   SetWindowLong FHandle, GWL_WNDPROC, WndProc
   Hooking = False
End If
End Sub

' Detect a right click event on our system tray icon - pass control to a handler routine
' in the main form (change as required)
Public Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
' Ensure that its our app thats affected and that its the right event
If Hooking = True Then   If uMsg WM_RBUTTONUP And lParam WM_RBUTTONDOWN Then
      Form1.SysTrayMouseEventHandler  ' Pass the event back to the form handler
      WindowProc = True               ' Let windows know we handled it
      Exit Function
   End If
   WindowProc = CallWindowProc(WndProc, hw, uMsg, wParam, lParam) ' Pass it along
End If
End Function



et voici le code dans mon form:

Hook Me.hwnd   ' Set up our handler
AddIconToTray Me.hwnd, Me.Icon, Me.Icon.Handle, "This is a test tip"
Me.Hide


merci
Jordan
0
jordangj Messages postés 63 Date d'inscription lundi 12 juillet 2004 Statut Membre Dernière intervention 8 mai 2007
13 juil. 2004 à 11:54
merci nikel
par contre comment avoir un menu quand on fai clic droit.
et comment faire poun utilisé l'icone reduire sur le form pour pouvoir metre le form dans le system tray
merci beaucoup
jordan
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
13 juil. 2004 à 12:33
Il suffira alors de traiter le message Clic droit :

Private Sub IconHook_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'# Si le message atteste d'un double click...
Select Case X / Screen.TwipsPerPixelX 
    Case  WM_LBUTTONDBLCLK :
           Me.Show
           RemoveIconFromTray
    Case WM_RBUTTONUP :
           PopupMenu Menu1
End Select
End Sub


pour la reduction dans la Tray, utilise l'evenement Resize :

Private Form1_Resize()
      If Me.WindowState = VbMinimized then
             Me.Hide
             AddIconToTray IconHook.hWnd, Me.Icon.Handle, "This is a test tip"
      End If
End Sub
0
jordangj Messages postés 63 Date d'inscription lundi 12 juillet 2004 Statut Membre Dernière intervention 8 mai 2007
13 juil. 2004 à 12:47
nikel pour le menu par contre pb avec le resize j'ai mi ca comme code par ce que j'aivai une ereur apres private

Private Sub Form1_Resize()
If Me.WindowState = vbMinimized Then
Me.Hide
AddIconToTray IconHook.hWnd, Me.Icon.Handle, "This is a test tip"
End If
End Sub


mon forme sapel bien Form1
et ca ne ce minimise pas dans la bare tray.

autre question peu on afficher ej l'icone dans la barre des tache meme quand la fenetre est restauré

merci
jordan
0
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
13 juil. 2004 à 13:13
ca doit être Form_Resize et non Form1_Resize....

pour afficher l'icone meme si l'appli est affichée, il suffit de pas la supprimer quand tu la restaure...
regardes le code que je t'ai fourni, et changes ce qui doit l'etre....
0
jordangj Messages postés 63 Date d'inscription lundi 12 juillet 2004 Statut Membre Dernière intervention 8 mai 2007
13 juil. 2004 à 13:29
ok merci c nikel
@+
0
jordangj Messages postés 63 Date d'inscription lundi 12 juillet 2004 Statut Membre Dernière intervention 8 mai 2007
13 juil. 2004 à 14:19
eu dsl encore une kestion comment au lieu de minimisé dans la tray avec la touche minimisé utilisisé la croix

et j'ai un probleme quand je le minimise il se met bien dans le system tray mais quand je veux le rouvrir ilne saffiche plus

merci
Jordan
0
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
13 juil. 2004 à 15:55
elementaire :

Private Sub IconHook_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'# Si le message atteste d'un double click...
Select Case X / Screen.TwipsPerPixelX 
Case WM_LBUTTONDBLCLK :
'# SINON, LE RESIZE REPLACERA L'ICONE DANS LA TRAY !!!!!
Me.WindowState = VbNormal
Me.Show
RemoveIconFromTray
Case WM_RBUTTONUP :
PopupMenu Menu1
End Select
End Sub


pour l'utilisation de la Croix : (procedure Resize inutile)

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = vbManual Then
        Cancel = True
        Me.Hide
        AddIconToTray IconHook.hWnd, Me.Icon.Handle, "This is a test tip"
    End If
End Sub
0
Rejoignez-nous