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 );
}
}