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 : 15205
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

{Javascript / DHTML} VIRTUAL IPHONE (V.2)
Après plusieurs mois d'attente et une reprise à zéro du "programme" voici le début de la nouvelle ve...

{Delphi} NETTOYEUR AUTOMATIQUE DE VOS DISQUES
C'est une application qui vous permet de néttoyer vos disques automatiquement avec option de pré-rég...

{Delphi} GESTION DES "CRASHS D'APPLICATION"
Salut, voici un composant pour permettre de gérer aisément et d'une façon ergonomique les accidents...

{C / C++ / C++.NET} DLLMAKER V2.0
Un programme Qt qui crée des fichiers prêts à être utilisés dans la création de dll. Ces fichiers s...

{Visual Basic, VB6, VB.NET, VB 2005} RETANGLE DE PETZOL
Le programme affiche un rectangle de Petzol. Il a fallu capter un programme en C et un autre en wpf...

{PHP} BLACKLAGOON APPS
Cette application a été conçu dans le but d'aider dans la gestion de son alliance du jeu World of St...

{Flash} SCRATCH_GAMES_CREATOR
Le but de cette application était que n'importe qui puisse réaliser son jeu à gratter (même ceux qui...

{C# / C#.NET} LANCEUR POUR WORLD OF WARCRAFT
World of Warcraft (Wow) est un MMORPG. Cette application permet à Wow de se connecter à différents ...

{Visual Basic, VB6, VB.NET, VB 2005} STANDARISATION D'APPLICATIONS
Cet utilitaire permet d'exécuter les applications que vous utilisez le plus souvent directement depu...

{Flash} DESSIN APPLICATION AS3
une base pour une application de dessin code action scripte 3 avec redimensionnement, palette & ta...