示例#1
0
        private KeyValuePair <WindowInfo, UnwantedTitle> GetFirstUnwantedWindow(Process p)
        {
            WindowInfo    wi = new WindowInfo(null, "", IntPtr.Zero);
            UnwantedTitle ut = new UnwantedTitle();

            foreach (ProcessThread thread in p.Threads)
            {
                WinApi.EnumThreadWindows(thread.Id,
                                         (windowHandle, lParam) =>
                {
                    if (!WinApi.IsWindowVisible(windowHandle))
                    {
                        return(true);
                    }

                    int length = WinApi.GetWindowTextLength(windowHandle);
                    if (length != 0)
                    {
                        StringBuilder builder = new StringBuilder(length);
                        WinApi.GetWindowText(windowHandle, builder, length + 1);

                        string windowTitle = builder.ToString().ToLower().Trim();

                        int foundIndex = _unwantedTitles.FindIndex(it => it.Title == windowTitle);

                        if (foundIndex >= 0)
                        {
                            wi = new WindowInfo(
                                owner: p,
                                title: windowTitle,
                                handle: windowHandle);

                            ut = _unwantedTitles[foundIndex];

                            return(false);
                        }
                    }

                    return(true);
                }, IntPtr.Zero);
            }

            return(new KeyValuePair <WindowInfo, UnwantedTitle>(wi, ut));
        }
示例#2
0
        private void LoadConfig(string configPath)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(configPath);

                var items = xmlDoc.DocumentElement.SelectNodes("/UnwantedTitles/Item");
                foreach (XmlNode it in items)
                {
                    var newItem = new UnwantedTitle();
                    newItem.Title                 = it.Attributes["Title"].Value;
                    newItem.KillAction            = it.Attributes["KillAction"].Value.ToLower() == "closeprocess" ? KillAction.kaCloseProcess : KillAction.kaCloseWindow;
                    newItem.InactivatePeriodLimit = int.Parse(it.Attributes["InactivatePeriodLimitSeconds"].Value) * 1000;
                    newItem.LifePeriodLimit       = int.Parse(it.Attributes["LifePeriodLimitSeconds"].Value) * 1000;
                    _unwantedTitles.Add(newItem);
                }
            }
            catch (Exception)
            {
                Console.WriteLine($"Ошибка при загрузке файла с настройками: '{configPath}'");
                throw;
            }
        }