0

I want to close a child window of a running application programmatically. I am using the following code:

    const UInt32 WM_CLOSE = 0x0010;

    [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
    public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

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

    public static bool CloseWindowbyTitle(string titleStr)
    {
        bool result = false;
        IntPtr windowPtr = FindWindow(null, titleStr);
        if (windowPtr == IntPtr.Zero)
            return result;
        SendMessage(windowPtr, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
        result = true;

        return result;
    }

It is working fine. But when the child window is in edit mode, it asks to the user to save edited data and if the user clicks on 'Yes' or 'No' then only child window gets closed.

But I want to close the child window forcefully, so that without asking anything it should get closed. But I don't know how to achieve this.

I can't use Process class to kill child windows as no separate process is getting created for that child window.

Thanks in advance.

Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219
BhushanK
  • 1,205
  • 6
  • 23
  • 39
  • 1
    You'd have to call DestroyWindow() but that is not legal from another process. You can't fix this if you can't get it out of "edit mode". – Hans Passant Aug 28 '14 at 09:15
  • 1
    @HansPassant: What technique uses TestComplete or other tools for GUI testing? Because, using test complete, you can get access to controls, and manage them out of the process. – Daniel Peñalba Aug 28 '14 at 09:50
  • 1
    Pretty hacky, but could you watch for the prompt window too, and simulate a button press on "No"? – GazTheDestroyer Aug 28 '14 at 09:51
  • @GazTheDestroyer : means i have simulate the button click on the prompt dialog ? – BhushanK Aug 28 '14 at 09:59

0 Answers0