简体   繁体   中英

Get handle of child window to postmessage in C#

im looking how to get handle of child window and postmessage there.

My code:

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        //This send postmessage to window "192.168.0.94 - Remote Desktop Connection"//
        IntPtr hwnd10 = FindWindow(null, "192.168.0.94 - Remote Desktop Connection");
        Point location = new Point(636, 324);
        PostMessage(hwnd10, WM_MOUSEMOVE, 0, MAKELPARAM(location.X, location.Y));
        PostMessage(hwnd10, WM_LBUTTONDOWN, 0x1, MAKELPARAM(location.X, location.Y));
        PostMessage(hwnd10, WM_LBUTTONUP, 0, MAKELPARAM(location.X, location.Y));

        // But now how can i do the same to child window "Input Capture Window"?

I do some research and i found [pageenumchildwindows][1] but don't know exacly how can i use it in my example.

Screenshoot to see what window exacly am i looking for :

Here is another implementation, using EnumWindows and EnumChildWindows .

This answer is not meant to be marked, but shows a different approach to the same problem.

It searches for a depth of 3, which can be increased in the code or even can be got as a constructor or method parameter.

It works.

public class RecursiveWindowSearcher
{
    public delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
    [DllImport("user32")]
    public extern static int EnumWindows(EnumWindowsProc lpEnumFunc, int lParam);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

    private IntPtr windowParent;
    private string windowName;
    private IntPtr windowPointer;

    public RecursiveWindowSearcher(string windowName, IntPtr windowParent)
    {
        this.windowName = windowName;
        this.windowParent = windowParent;
    }

    public bool WinApiCallback(IntPtr hwnd, IntPtr lParam)
    {
        Console.WriteLine("Window: {0}", hwnd);

        StringBuilder windowNameFar = new StringBuilder(256);
        GetWindowText(hwnd, windowNameFar, 256);

        if (windowNameFar.ToString() == windowName)
        {
            windowPointer = hwnd;
            return false;
        }   

        Console.WriteLine("Name: {0}", windowNameFar);

        if(indent == 6)
        {
            return false;
        }
        indent += 2;
        EnumChildWindows(hwnd, WinApiCallback, IntPtr.Zero);
        indent -= 2;

        return true;
    }

    public IntPtr Find()
    {
        this.windowPointer = IntPtr.Zero;
        if (windowParent == IntPtr.Zero)
        {
            EnumWindows(WinApiCallback, 0);
        }
        else
        {
            EnumChildWindows(windowParent, WinApiCallback, IntPtr.Zero);
        }
        return windowPointer;
    }
}

And the usage is simple enough:

The second constructor parameter is a pointer to an existing window to search starting from. If you have this handle, you can pass it to narrow the search scope to a specific window and its children.

static void Main()
{
    IntPtr windowFound = new RecursiveWindowSearcher("Skype for Business ", IntPtr.Zero).Find();
    Console.ReadLine();
}

OK i found solution thanks to Oguz Ozgul. I did it by FindWindowEx like :

 IntPtr child = FindWindowEx(hwnd10, IntPtr.Zero, "TscShellAxHostClass", null);
        //check if window is caught
        if(child!=IntPtr.Zero)
        {
            Console.WriteLine("Findow TscShellAxHostClass found!!!");

            child = FindWindowEx(child, IntPtr.Zero, "ATL:00007FFC92EAF400", null);
            if (child != IntPtr.Zero)
            {
                Console.WriteLine("Findow ATL:00007FFC92EAF400 found!!!");

                child = FindWindowEx(child, IntPtr.Zero, "UIMainClass", null);
                if (child != IntPtr.Zero)
                {

                    Console.WriteLine("Findow UIMainClass found!!!");
                    child = FindWindowEx(child, IntPtr.Zero, "UIContainerClass", null);
                    if (child != IntPtr.Zero)
                    {

                        Console.WriteLine("Findow UIContainerClass found!!!");
                        child = FindWindowEx(child, IntPtr.Zero, "IHWindowClass", null);
                        if (child != IntPtr.Zero)
                        {
                            Console.WriteLine("Findow IHWindowClass found!!!");
                        }
                    }
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM