Get Application's Window Handles

前端 未结 3 1406
野趣味
野趣味 2020-11-28 10:14

I\'m building an app that given another app mainWindowhandle it collects information about the window state. I have no problem collecting information about child windows, bu

3条回答
  •  情话喂你
    2020-11-28 10:36

    First, you'll have to get the windowhandle of the application's mainwindow.

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

    Then, you can use this handle to get all childwindows:

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
    
    private List GetChildWindows(IntPtr parent)  
    {  
        List result = new List();  
        GCHandle listHandle = GCHandle.Alloc(result);  
        try  
        {  
             EnumWindowProc childProc = new EnumWindowProc(EnumWindow);  
             EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));  
        }  
        finally  
        {  
             if (listHandle.IsAllocated)  
                   listHandle.Free();  
        }  
        return result;  
    }   
    

提交回复
热议问题