//Get the window title from process public static string GetWindowTitleFromProcess(Process targetProcess) { string ProcessTitle = "Unknown"; try { ProcessTitle = targetProcess.MainWindowTitle; if (string.IsNullOrWhiteSpace(ProcessTitle)) { ProcessTitle = GetWindowTitleFromWindowHandle(targetProcess.MainWindowHandle); } if (string.IsNullOrWhiteSpace(ProcessTitle) || ProcessTitle == "Unknown") { ProcessTitle = targetProcess.ProcessName; } if (!string.IsNullOrWhiteSpace(ProcessTitle)) { ProcessTitle = AVFunctions.StringRemoveStart(ProcessTitle, " "); ProcessTitle = AVFunctions.StringRemoveEnd(ProcessTitle, " "); } else { ProcessTitle = "Unknown"; } } catch { } return(ProcessTitle); }
//Convert file to a string public static string FileToString(string[] stringSource) { try { //Load application bitmap image foreach (string loadFile in stringSource) { try { //Validate the load path if (string.IsNullOrWhiteSpace(loadFile)) { continue; } //Adjust the load path string loadFileLower = loadFile.ToLower(); loadFileLower = AVFunctions.StringRemoveStart(loadFileLower, " "); loadFileLower = AVFunctions.StringRemoveEnd(loadFileLower, " "); //Debug.WriteLine("Loading text: " + loadFileLower); //Read the text file if (File.Exists(loadFileLower)) { return(File.ReadAllText(loadFileLower)); } } catch { } } } catch { } return(string.Empty); }
//Get the window title from window handle public static string GetWindowTitleFromWindowHandle(IntPtr targetWindowHandle) { string ProcessTitle = "Unknown"; try { int WindowTextBuilderLength = GetWindowTextLength(targetWindowHandle) + 1; StringBuilder WindowTextBuilder = new StringBuilder(WindowTextBuilderLength); GetWindowText(targetWindowHandle, WindowTextBuilder, WindowTextBuilder.Capacity); string BuilderString = WindowTextBuilder.ToString(); if (!string.IsNullOrWhiteSpace(BuilderString)) { ProcessTitle = BuilderString; ProcessTitle = AVFunctions.StringRemoveStart(ProcessTitle, " "); ProcessTitle = AVFunctions.StringRemoveEnd(ProcessTitle, " "); } else { ProcessTitle = "Unknown"; } } catch { } return(ProcessTitle); }
//Convert file to a BitmapImage public static BitmapImage FileToBitmapImage(string[] sourceImages, ImageSourceFolders[] sourceFolders, string sourceBackup, IntPtr windowHandle, int pixelWidth, int iconIndex) { try { //Prepare application bitmap image BitmapImage imageToBitmapImage = BeginBitmapImage(pixelWidth); MemoryStream imageMemoryStream = new MemoryStream(); //Load application bitmap image foreach (string loadFile in sourceImages) { try { //Validate the load path if (string.IsNullOrWhiteSpace(loadFile)) { continue; } //Adjust the load path string loadFileLower = loadFile.ToLower(); loadFileLower = AVFunctions.StringRemoveStart(loadFileLower, " "); loadFileLower = AVFunctions.StringRemoveEnd(loadFileLower, " "); if (!loadFileLower.Contains("/") && !loadFileLower.Contains("\\")) { loadFileLower = string.Join(string.Empty, loadFileLower.Split(Path.GetInvalidFileNameChars())); } //Debug.WriteLine("Loading image: " + loadFileLower); if (loadFileLower.StartsWith("pack://")) { imageToBitmapImage.UriSource = new Uri(loadFileLower, UriKind.RelativeOrAbsolute); } else if (File.Exists(loadFileLower) && loadFileLower.EndsWith(".ico")) { imageToBitmapImage.StreamSource = GetIconMemoryStreamFromIcoFile(loadFileLower, ref imageMemoryStream); } else if (File.Exists(loadFileLower) && !loadFileLower.EndsWith(".exe") && !loadFileLower.EndsWith(".dll") && !loadFileLower.EndsWith(".bin") && !loadFileLower.EndsWith(".tmp") && !loadFileLower.EndsWith(".bat")) { imageToBitmapImage.UriSource = new Uri(loadFileLower, UriKind.RelativeOrAbsolute); } else if (File.Exists(loadFileLower) && (loadFileLower.EndsWith(".exe") || loadFileLower.EndsWith(".dll") || loadFileLower.EndsWith(".bin") || loadFileLower.EndsWith(".tmp"))) { imageToBitmapImage.StreamSource = GetIconMemoryStreamFromExeFile(loadFileLower, iconIndex, ref imageMemoryStream); } else if (sourceFolders != null) { foreach (ImageSourceFolders sourceFolder in sourceFolders) { try { if (Directory.Exists(sourceFolder.SourcePath)) { string[] pngImages = Directory.GetFiles(sourceFolder.SourcePath, "*.png", sourceFolder.SearchOption); string[] jpgImages = Directory.GetFiles(sourceFolder.SourcePath, "*.jpg", sourceFolder.SearchOption); IEnumerable <string> foundImages = pngImages.Concat(jpgImages); string foundImage = foundImages.FirstOrDefault(x => Path.GetFileNameWithoutExtension(x).ToLower() == loadFileLower); if (!string.IsNullOrWhiteSpace(foundImage)) { imageToBitmapImage.UriSource = new Uri(foundImage, UriKind.RelativeOrAbsolute); break; } } } catch { } } } //Return application bitmap image if (imageToBitmapImage.UriSource != null) { return(EndBitmapImage(imageToBitmapImage, ref imageMemoryStream)); } else if (imageToBitmapImage.StreamSource != null && imageToBitmapImage.StreamSource.Length > 75) { return(EndBitmapImage(imageToBitmapImage, ref imageMemoryStream)); } } catch (Exception ex) { Debug.WriteLine("Failed loading image: " + loadFile + "/" + ex.Message); } } //Image source not found, loading backup or window icon if (windowHandle == IntPtr.Zero) { imageToBitmapImage.UriSource = new Uri(sourceBackup, UriKind.RelativeOrAbsolute); } else { MemoryStream windowImage = GetIconMemoryStreamFromWindow(windowHandle, ref imageMemoryStream); if (windowImage != null) { imageToBitmapImage.StreamSource = windowImage; } else { imageToBitmapImage.UriSource = new Uri(sourceBackup, UriKind.RelativeOrAbsolute); } } //Return application bitmap image return(EndBitmapImage(imageToBitmapImage, ref imageMemoryStream)); } catch { } return(null); }