J'essaie d'utiliser le code ci-dessous pour fermer la fenêtre.Problème lors de l'utilisation de WM_Close dans C#
Mais obtenir erreur dans
"IntPtr hWnd = PostMessage (IntPtr.Zero, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);" Où indiquer le nom de la fenêtre afin de fermer cela?
Et il y a aussi un problème dans les paramètres que je passe.
void DaemonTerminamtionHook_KeyPressed(object sender, KeyPressedEventArgs e)
{
DaemonResult = MessageBox.Show("Are you sure, you want to Terminate Daemon?", "Terminate Daemon", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
if (DaemonResult == DialogResult.Yes)
{
IntPtr hWnd = PostMessage(IntPtr.Zero, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
bool ret = CloseWindow(hWnd);
}
}
static uint WM_CLOSE = 0x10;
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
static bool CloseWindow(IntPtr hWnd)
{
bool returnValue = PostMessage(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
if (!returnValue)
throw new Win32Exception(Marshal.GetLastWin32Error());
return true;
}
Après modification du code, mais toujours pas de chance. depuis que je suis nouveau à windows messaging stuff.
void DaemonTerminamtionHook_KeyPressed(object sender, KeyPressedEventArgs e)
{
DaemonResult = MessageBox.Show("Are you sure, you want to Terminate Daemon?", "Terminate Daemon", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
if (DaemonResult == DialogResult.Yes)
{
IntPtr hWnd = FindWindow(null, "DAEMON TAB BAR");
bool ret = CloseWindow(hWnd);
}
}
static bool CloseWindow(IntPtr hWnd)
{
//How to call it here
return true;
}
Quelle erreur obtenez-vous? –
Les paramètres ne correspondent pas à la méthode. – Anuya
Votre code initial dans CloseWindow semble OK. Vous pouvez vérifier si la valeur de retour de FindWindow est IntPtr.Zero. Si c'est le cas, la fenêtre que vous essayez de fermer est introuvable. Sinon, appelez simplement votre CloseWindow d'origine avec hWnd comme paramètre. –