예제 #1
0
        public static List <IntPtr> GetChildWindows(IntPtr hwnd)
        {
            var childWindows = new List <IntPtr>();

            try
            {
                WinAPI.WindowEnumProc addChildWindow =
                    (IntPtr childHwnd, IntPtr lparam) => {
                    childWindows.Add(childHwnd);
                    return(1);
                };
                WinAPI.EnumChildWindows(hwnd, addChildWindow, IntPtr.Zero);
            }
            catch (Exception ex)
            {
                ex.log("in GetChildWindows");
            }
            return(childWindows);
        }
    public Excel.Application[] GetExcelInstances()
    {
        _XLInstances              = new List <Excel.Application>();
        _ExcelProcessIDs          = new HashSet <int>();
        _ExcelTopLevelWindowHwnds = new List <int>();

        foreach (Process p in Process.GetProcessesByName("EXCEL"))
        {
            _ExcelProcessIDs.Add(p.Id);                                                            //find all process ids related to Excel
        }
        int hwnd = 0;
        var cb   = new WinAPI.WindowEnumProc(GetAllExcelTopLevelWindowHwnds);

        WinAPI.EnumWindows(cb, ref hwnd);

        foreach (var hwnd2 in _ExcelTopLevelWindowHwnds)
        {
            var excelHwnd = 0;
            var cb2       = new WinAPI.WindowEnumProc(GetExcelWorkbooksFromExcelWindowHandles);
            WinAPI.EnumChildWindows(hwnd2, cb2, ref excelHwnd);
        }

        return(_XLInstances.ToArray());
    }