2009-09-08 12 views
0

私は以下のコードを使用して、タスクバーのウィンドウ名を検索してウィンドウを閉じます。 しかし、私の場合、私のウィンドウはタスクバーに表示されません。その場合、WM_Closeはウィンドウを閉じることができませんでした。 WM_Closeを使用してそれを行うには別の方法は何ですか? "のIntPtr hWndは=のPostMessage(IntPtr.Zero、WM_CLOSE、IntPtr.Zero、IntPtr.Zero);" 今、以下のコードを使用して...しかし、C#でWM_Closeを使用する

でエラーを取得

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) 
     { 
      //Free the resources of ShellBasics and terminate Daemon here. 
      IntPtr hWnd = FindWindowByCaption(IntPtr.Zero, "DAEMON TAB BAR"); 
      bool ret = CloseWindow(hWnd); 
     } 
    } 

    //WM_Close 
    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] 
    static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName); 

    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); 

    static uint WM_CLOSE = 0x10; 

    static bool CloseWindow(IntPtr hWnd) 
    { 
     SendMessage(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero); 
     return true; 
    } 

これを閉じるためにウィンドウ名を指定する場所はどこですか?


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

答えて

1

編集:申し訳ありませんが、あなたの質問を読み違えます。

代わりにFindWindow/FindWindowExを使用してください。

+0

@Preet、ご支援いただきありがとうございます。 void PostMessageSafe(HandleRef hWnd、uint msg、IntPtr wParam、IntPtr lParam) – Anuya

+0

@Preet、ここでウィンドウ名を渡す場所はどこですか? – Anuya

+0

@Preet、上に更新されたコードを参照してください、それはparamertersが一致しないといくつかのエラーを出すと、それを閉じるためにWindowsの名前inorderを提供する場所を教えてください。 – Anuya

関連する問題