diff --git a/ScriptRes/IconUtil.cs b/ScriptRes/IconUtil.cs index 0cb6959..233dd91 100644 --- a/ScriptRes/IconUtil.cs +++ b/ScriptRes/IconUtil.cs @@ -59,12 +59,12 @@ public static Icon[] ExtractAllIcons(string filePath, uint MaxIcons = 10) { if (File.Exists(filePath) == false) { - throw new Exception($"The file: {filePath} doesn't exist or moved."); + throw new FileNotFoundException($"The file: {filePath} doesn't exist or moved."); } - Icon[] icons; + Icon[] icons = Array.Empty(); - if (VerifyMultiIconExtension(filePath)) + if (VerifyFileWithIcons(filePath)) { // Get the amount of icons stored uint iconsCount = ExtractIconEx(filePath, -1, null, null, 1); @@ -92,17 +92,11 @@ public static Icon[] ExtractAllIcons(string filePath, uint MaxIcons = 10) } } } - else - { - //Icon? icon = Icon.ExtractAssociatedIcon(filePath); - //icons = icon == null ? Array.Empty() : new Icon[1] { icon }; - icons = Array.Empty(); - } return icons; } - private static bool VerifyMultiIconExtension(string filePath) + private static bool VerifyFileWithIcons(string filePath) { return Path.GetExtension(filePath) switch { diff --git a/ScriptRes/MainWindow.xaml b/ScriptRes/MainWindow.xaml index 68ab84b..697ab4b 100644 --- a/ScriptRes/MainWindow.xaml +++ b/ScriptRes/MainWindow.xaml @@ -51,11 +51,17 @@ + + + + + - + + @@ -71,7 +77,7 @@ - + @@ -80,9 +86,12 @@ - + @@ -101,7 +110,7 @@ - + diff --git a/ScriptRes/MainWindow.xaml.cs b/ScriptRes/MainWindow.xaml.cs index de7ff0b..b5c1770 100644 --- a/ScriptRes/MainWindow.xaml.cs +++ b/ScriptRes/MainWindow.xaml.cs @@ -14,6 +14,8 @@ namespace ScriptRes /// public partial class MainWindow : Window { + private string _iconsSource = string.Empty; + public MainWindow() { InitializeComponent(); @@ -26,43 +28,64 @@ private void Window_Loaded(object sender, RoutedEventArgs e) chBoxQresPath.Unchecked += CheckBox_Checked; } - // Handler for buttons which select file locations + // Handler for buttons to select file locations private void BtnBrowse_Click(object sender, RoutedEventArgs e) { if (sender is not Button btn) return; - OpenFileDialog openFileDialog = new OpenFileDialog { Filter = "All Files (*.*)|*.*" }; - - if (btn == btnChangeQresPath && openFileDialog.ShowDialog() == true) - { - tBoxQresPath.Text = openFileDialog.FileName; - } - else if (btn == btnBrowseExec && openFileDialog.ShowDialog() == true) + if (btn == btnChangeQresPath) { - // In case the file was deleted or moved after selection - if (File.Exists(openFileDialog.FileName) == false) + OpenFileDialog openFileDialog = new OpenFileDialog { Filter = "Executable files (*.exe)|*.exe" }; + if (openFileDialog.ShowDialog() == true) { - MessageBox.Show("Provided program executable doesn't exist or not a file!", "Error", MessageBoxButton.OK, MessageBoxImage.Warning); - return; + tBoxQresPath.Text = openFileDialog.FileName; } - tBoxExecPath.Text = openFileDialog.FileName; - - // Get all icons associated with a file - var extractedIcons = IconUtil.ExtractAllIcons(openFileDialog.FileName); - // Display all icons in ListBox - - var itemsSource = new List(); - for (int i = 0; i < extractedIcons.Length; i++) + } + else if (btn == btnBrowseExec || btn == btnCustomIcon) + { + string filter = btn == btnBrowseExec ? "All files (*.*)|*.*" : "Files with icons (*.ico;*.exe;*.dll)|*.ico;*.exe;*.dll"; + OpenFileDialog openFileDialog = new OpenFileDialog { Filter = filter }; + if (openFileDialog.ShowDialog() == true) { - var imageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon( - extractedIcons[i].Handle, - Int32Rect.Empty, - BitmapSizeOptions.FromEmptyOptions()); - - itemsSource.Add(new IconListItem(i, imageSource)); + _iconsSource = openFileDialog.FileName; + if (btn == btnBrowseExec) + { + tBoxExecPath.Text = openFileDialog.FileName; + } + // Get all icons associated with a file + System.Drawing.Icon[] extractedIcons; + try + { + extractedIcons = IconUtil.ExtractAllIcons(_iconsSource); + } + catch (FileNotFoundException exc) + { + MessageBox.Show($"An error occured while extracting icons from the file.\nDetails: {exc.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error); + return; + } + + // Display all icons in ListBox + var itemsSource = new List(); + for (int i = 0; i < extractedIcons.Length; i++) + { + var imageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon( + extractedIcons[i].Handle, + Int32Rect.Empty, + BitmapSizeOptions.FromEmptyOptions()); + + itemsSource.Add(new IconListItem(i, imageSource)); + } + listBoxIcons.ItemsSource = itemsSource; + listBoxIcons.SelectedIndex = 0; // first element is auto-selected + + tBoxExtractedIcons.Text = itemsSource.Count > 0 ? "Select the icon" : "Couldn't extract icons"; + tBoxShortcutName.IsEnabled = true; + if (btn == btnBrowseExec) + { + tBoxShortcutName.Text = Path.GetFileNameWithoutExtension(_iconsSource); + } + btnCustomIcon.Visibility = Visibility.Visible; } - listBoxIcons.ItemsSource = itemsSource; - tBoxExtractedIcons.Text = itemsSource.Count > 0 ? "Select the icon" : "Couldn't extract icons"; } } @@ -105,40 +128,41 @@ private void BtnCreate_Click(object sender, RoutedEventArgs e) string execName = Path.GetFileName(execPath); string execNameNoExt = Path.GetFileNameWithoutExtension(execPath); - string script = $"@echo off\r\n\r\n" + - "set exitCode=0\r\n" + + string script = $"@echo off\r\n" + + "chcp 65001\r\n\r\n" + + "set exitCode=0\r\n\r\n" + $"echo Changing the resolution to {outX}x{outY} ...\r\n" + $"\"{qresPath}\" /x:{outX} /y:{outY} | findstr /i \"Error:\" > nul\r\n" + "if %errorlevel% equ 0 (\r\n" + " set exitCode=-1\r\n" + - " goto error\r\n" + - ")\r\n" + + " echo ERROR: Something went wrong, it is likely that the resolution is not supported.\r\n" + + ")\r\n\r\n" + "echo.\r\n" + $"echo Launching the program \"{execName}\"\r\n" + + $"start /b /wait \"\" \"{execPath}\"\r\n\r\n" + "echo.\r\n" + - $"\"{execPath}\"\r\n\r\n" + - $":loop\r\ntasklist | findstr /i \"{execName}\" > nul\r\n" + - "if not %errorlevel% equ 0 (\r\n" + - " echo Changing resolution back to normal\r\n" + - $" \"{qresPath}\" /x:{inX} /y:{inY} > nul\r\n" + - " goto end\r\n) \r\n\r\n" + - "timeout /t 2 > nul 2>&1\r\n" + - "goto loop\r\n\r\n" + - ":error\r\n" + - "echo ERROR: Something went wrong, it is likely that the resolution is not supported.\r\n\r\n" + - ":end\r\n" + + "echo Changing resolution back to normal\r\n" + + $"\"{qresPath}\" /x:{inX} /y:{inY} | findstr /i \"Error:\" > nul\r\n" + + "if %errorlevel% equ 0 (\r\n" + + " set exitCode=-1\r\n" + + " echo ERROR: Something went wrong, it is likely that the resolution is not supported.\r\n" + + ")\r\n\r\n" + "echo.\r\n" + "echo Finish\r\n" + "timeout /t 2 > nul 2>&1\r\n" + "exit /b %exitCode%"; try { - string batchLocation = Path.Combine(desktopPath, $"{execNameNoExt}.bat"); + string scriptsLocation = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Scripts"); + Directory.CreateDirectory(scriptsLocation); + + string batchLocation = Path.Combine(scriptsLocation, $"{execNameNoExt}.bat"); File.WriteAllText(batchLocation, script); - string saveLocation = Environment.CurrentDirectory; + + string shortcutName = string.IsNullOrWhiteSpace(tBoxShortcutName.Text) ? execNameNoExt : tBoxShortcutName.Text; - CreateShortcut(desktopPath, batchLocation, execPath, execNameNoExt); - MessageBox.Show($"The shortcut was created: \n{desktopPath}\\{execNameNoExt}.lnk", "Success", MessageBoxButton.OK, MessageBoxImage.Information); + CreateShortcut(desktopPath, batchLocation, _iconsSource, shortcutName); + MessageBox.Show($"The shortcut was created: \n{desktopPath}\\{shortcutName}.lnk", "Success", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { @@ -151,7 +175,7 @@ private void CheckBox_Checked(object sender, EventArgs e) if (sender is not CheckBox box) return; tBoxQresPath.IsEnabled = !box.IsChecked ?? false; - tBoxQresPath.Text = "./Qres.exe"; + tBoxQresPath.Text = "./QRes.exe"; btnChangeQresPath.IsEnabled = !box.IsChecked ?? false; }