2009-09-08 3 views
0

나는 아래 코드를 사용하여 작업 표시 줄의 창 이름을 검색하여 창을 닫습니다. 하지만 한 가지 경우, 내 창은 작업 표시 줄에 나타나지 않습니다. 이 경우 WM_Close은 창을 닫을 수 없습니다. 뭐 다른 방법을 사용하여 WM_Close ???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; 
    } 

이제 아래의 코드를 사용하여 ...하지만

에 오류가

"IntPtr입니다 HWND = PostMessage를 (IntPtr.Zero, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);"

어디에서 닫으려면 창 이름을 제공합니까 ???


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