Les Snippets

Connexion

Fermer une application identifiée par son titre

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 21/03/2006 17:28:12 et initié par PCPT [Liste]
Date de mise à jour : 27/03/2006 17:53:38
Vue : 21819
Catégorie(s) : API
Langages dispo pour ce code :
- VB6, VBA
- C# 1.x, C# 2.x
- C# 1.x, C# 2.x
- VB 2005, VB.NET 1.x
- Delphi 5
- VB 2005, VB 2008, VB.NET 1.x



Langage : VB6 , VBA
Date ajout : 21/03/2006
Posté par PCPT [Liste]
Option Explicit 
  

' Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _          (ByVal lpClassName As StringByVal lpWindowName As StringAs Long  ' Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _          (ByVal hwnd As LongByVal wMsg As LongByVal wParam As Long, lParam As Any) As Long  ' Private Const WM_CLOSE      As Long = &H10  Private Const HTCAPTION     As Long = 2&  ' Private Const MON_TITRE     As String = "Sans titre - Bloc-notes"  ' ' Private Sub Command1_Click()      Dim lHwnd As Long      lHwnd = FindWindow(vbNullString, MON_TITRE)      If lHwnd = 0 Then          MsgBox "Titre non-trouvé!!!"      Else          Call SendMessage(lHwnd, WM_CLOSE, HTCAPTION, ByVal 0&)      End If  End Sub

Langage : C# 1.x , C# 2.x
Date ajout : 22/03/2006
Posté par Lutinore [Liste]

class MyForm : Form

{
    // ..
    [ DllImport( "User32.dll", SetLastError = true ) ] 
    private static extern IntPtr FindWindow( string className, string windowName );
    [ DllImport( "User32.dll" ) ] 
    private static extern IntPtr SendMessage( IntPtr hWnd, uint msg, UIntPtr wParam, IntPtr lParam );

    private const int WM_CLOSE = 0x0010; 
    private const int HTCAPTION = 2;
    private const string WINDOW_NAME = "Sans titre - Bloc-notes";
    
    private void CloseWindow( ) 
    {
        IntPtr hWnd = FindWindow( null, WINDOW_NAME );
        if ( hWnd == IntPtr.Zero ) 
            MessageBox.Show( this, "La fenêtre est introuvable." ); 
        else

            SendMessage( hWnd, WM_CLOSE, ( UIntPtr )HTCAPTION, IntPtr.Zero ); 
    }
}


Langage : C# 1.x , C# 2.x
Date ajout : 24/03/2006
Posté par sebmafate [Liste]
public void CloseApplicationByCaption(string caption) {
   foreach (Process p in Process.GetProcesses()) {
      if (p.MainWindowTitle.Equals(caption)) {
         p.Close();
      }
   } 
}

Remarque :
Solution alternative n'utilisant pas les APIs.
N'oubliez pas d'ajouter la directive :
using System.Diagnostics;
Langage : VB.NET 1.x , VB 2005
Date ajout : 24/03/2006
Posté par sebmafate [Liste]

Public Sub CloseApplicationByCaption(ByVal caption As String)
   For Each p As Process In Process.GetProcesses()
      If (p.MainWindowTitle.Equals(caption)) Then
         p.Close()
      End If
   Next
End Sub

Remarque :
Solution alternative n'utilisant pas les APIs.
N'oubliez pas d'ajouter la directive :
Imports System.Diagnostics
Langage : Delphi 5
Date ajout : 27/03/2006
Posté par ni69 [Liste]
DateMAJ : 27/03/2006
// Mode de fermeture par l'envoi d'un message Windows à l'application
SendMessage(FindWindow(nil, 'Titre de l'Application'), WM_CLOSE, 0, 0);
Langage : VB.NET 1.x , VB 2005 , VB 2008
Date ajout : 20/11/2008
Posté par PCPT [Liste]
    <System.Runtime.InteropServices.DllImportAttribute("user32.dll", SetLastError:=True)> Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
    End Function
    Function CloseWindowFromHandle(ByVal hWnd As IntPtr) As Boolean
        Const WM_CLOSE As Int32 = &H10
        If hWnd = IntPtr.Zero Then
            Return False
        Else
            Return (SendMessage(hWnd, WM_CLOSE, 00= 0)
        End If
    End Function
    <System.Runtime.InteropServices.DllImportAttribute("user32.dll", SetLastError:=True)> Shared Function FindWindow(ByVal lpClassName As StringByVal lpWindowName As StringAs IntPtr
    End Function
    Function GetHandleFromCaption(ByVal sCaption As StringAs IntPtr
        Return FindWindow(Microsoft.VisualBasic.vbNullString, sCaption)
    End Function
Remarque :
CloseWindowFromHandle(GetHandleFromCaption("Sans titre - Bloc-notes"))

Snippets en rapport avec : Application, Fermer



Codes sources en rapport avec : Application, Fermer

{Visual Basic, VB6, VB.NET, VB 2005} FERMER UN PROGRAMME, UNE APPLICATION, UNE FENÊTRE... D'APRÈS UNE PARTIE DE SON NOM
Permet en quelques lignes de fermer un programme, une application, une fenêtre... en passant en para...

{C / C++ / C++.NET} JEU DES CARTES
Jeu des cartes c'est un jeu qui consiste à trouver les paires des cartes préalablement cachées . il...

{C / C++ / C++.NET} CONTACTS MANAGER
Contacts Manager est une mini application qui vous permette de sauvegarder vos contactes avec les li...

{Visual Basic, VB6, VB.NET, VB 2005} CRYPTAGE ET DECRYPTAGE
Voici une petit application qui permet de crypter et décrypter des massages,en un clin d’?il. ...

{PHP} MY.BOOKMARKS
My.Boomarks My.Bookmarks est une application PHP5 permettant la sauvegarde de vos sites favoris s...

{C# / C#.NET} VERIFIER VOTRE CODE RIO (RELEVE IDENTIFIANT OPERATEUR) POUR LA PORTABILITÉ DE VOTRE NUMÉRO DE PORTABLE
Le code RIO est le code que vous demandez à votre opérateur actuel si vous voulez conserver votre nu...

{C# / C#.NET} LA LISTE DES TÂCHES COMME DANS L'ONGLET APPLICATION DU GESTIONNAIRE DES TÂCHES
Ce code permet d'obtenir la liste de ce que Windows considère comme une tâche et qu'il affiche dans ...

{Visual Basic, VB6, VB.NET, VB 2005} LA LISTE DES TÂCHES COMME DANS L'ONGLET APPLICATION DU GESTIONNAIRE DES TÂCHES
Ce code permet d'obtenir la liste de ce que Windows considère comme une tâche et qu'il affiche dans ...

{Visual Basic, VB6, VB.NET, VB 2005} VERSION DE DÉMO DE NOS PROGRAMMES
Beaucoup d'entre vous n'ont pas compris l'utilité des recherches de Mayzz avec sa source: http://ww...

{C / C++ / C++.NET} CARNET D'ADRESSE MODE CONSOLE
Ce programme est un carnet d'adresse en mode console qui écrit dans un .txt les informations suivant...