views:

326

answers:

2

Hi,

I want to send a pressKey event to a certain application which is not the active application IN Windows so I have to use the sendMessage/postMessage api calls.

However, I need to know the exact child window that is active IN the application and send the pressKey message to it...

I was using GetTopWindow and GetWindow(GW_CHILD) api calls to get the top child window of the main window, and do it again with the obtained child window to get the top grandchildWindow, and keep doing it until I found a childwindow with no more childwindows. This works great for some applications but in some cases it doesn't. Sometimes the parent window is the active window, not one of its childwindows, so getting the parent's top child window will not work cause I will be sending a message to the wrong window.

The only way I found of doing this (getting the handler of the actual active window) was using the GuiThreadInfo api call but it only works if the target application is the active one IN Windows. As I mentioned in the beginning, it isn't so the handler comes null.

I can bring the application to the top using setForegroundWindow api call but I DON'T want to do this. I also tried the AttachThreadInput and GetFocus api calls, but again, they only work if the target application is the active application IN windows.

Any ideas? Thanks

+1  A: 

I assume from the things that you have tried that you know how to get a handle to your main window, but if you don't just leave a comment and I will post a snippet for that.

I combined a few things that I found on the net to figure this out, but the main one is this one. I don't have a great app to test this with, but it works in a simple case. One exception is that I think if you use tool windows in your application it will not find that as it is coded because I think the GetLastActivePopup method doesn't include them (not sure about that, and didn't test that case).

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);

[DllImport("user32.dll")]
static extern IntPtr GetLastActivePopup(IntPtr hWnd);

[DllImport("user32.dll", ExactSpelling = true)]
static extern IntPtr GetAncestor(IntPtr hwnd, uint gaFlags);

const uint GA_PARENT = 1;
const uint GA_ROOT = 2;
const uint GA_ROOTOWNER = 3;

    public static IntPtr GetAppActiveWindow(IntPtr hwnd)
    {
        IntPtr activeAppWindow = IntPtr.Zero;

        if (hwnd != IntPtr.Zero)
        {
            //Get the root owner window (make sure we are at the app window
            //if you already have a handle to the main window shouldn't have 
            //to do this but I put it in just in case
            hwnd = GetAncestor(hwnd, GA_ROOTOWNER);

            while ((activeAppWindow = 
                      GetLastActivePopup(hwnd)) != activeAppWindow)
            {
                if (IsWindowVisible(activeAppWindow))
                    break;
                hwnd = activeAppWindow;
            }
        }

        return activeAppWindow;
    }
Brian ONeil
A: 

If you know the Window title and the Window class name, take a look at FindWindow() and FindWindowEx() and see if those meet your needs.

FindWindow(): http://msdn.microsoft.com/en-us/library/ms633499.aspx
FindWindowEx(): http://msdn.microsoft.com/en-us/library/ms633500(VS.85).aspx

Jay Michaud