Skip to content
This repository has been archived by the owner on Jul 27, 2024. It is now read-only.

Commit

Permalink
v0.3.0: User can select custom icon file, set name for the shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-ihnatiuk committed May 20, 2023
1 parent f0a5086 commit f228a7d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 63 deletions.
14 changes: 4 additions & 10 deletions ScriptRes/IconUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Icon>();

if (VerifyMultiIconExtension(filePath))
if (VerifyFileWithIcons(filePath))
{
// Get the amount of icons stored
uint iconsCount = ExtractIconEx(filePath, -1, null, null, 1);
Expand Down Expand Up @@ -92,17 +92,11 @@ public static Icon[] ExtractAllIcons(string filePath, uint MaxIcons = 10)
}
}
}
else
{
//Icon? icon = Icon.ExtractAssociatedIcon(filePath);
//icons = icon == null ? Array.Empty<Icon>() : new Icon[1] { icon };
icons = Array.Empty<Icon>();
}

return icons;
}

private static bool VerifyMultiIconExtension(string filePath)
private static bool VerifyFileWithIcons(string filePath)
{
return Path.GetExtension(filePath) switch
{
Expand Down
19 changes: 14 additions & 5 deletions ScriptRes/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,17 @@

<Grid Grid.Row="2" Margin="10">
<Grid.RowDefinitions>
<!--Executable selection-->
<RowDefinition Height="20"/>
<RowDefinition Height="30"/>
<!--Icon selection-->
<RowDefinition Height="40"/>
<RowDefinition Height="60"/>
<!--Name selection-->
<RowDefinition Height="40"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="30"/>
<!--Create button-->
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
Expand All @@ -71,7 +77,7 @@
<ListBox Name="listBoxIcons" BorderThickness="0" Margin="0,0,10,0" Grid.Row="3" Grid.ColumnSpan="1" Height="60" HorizontalAlignment="Stretch" VerticalAlignment="Top" d:ItemsSource="{d:SampleData ItemCount=5}" ScrollViewer.VerticalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Height="35" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<StackPanel Orientation="Horizontal" Height="34" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
Expand All @@ -80,9 +86,12 @@
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Grid.Row="3" Grid.Column="1" Visibility="Hidden" Height="30" Background="White" Content="Custom" VerticalAlignment="Center"/>
<Button Name="btnCustomIcon" Click="BtnBrowse_Click" Grid.Row="3" Grid.Column="1" Margin="0,2" Visibility="Hidden" Height="30" Background="White" Content="Custom" VerticalAlignment="Top"/>

<TextBlock Grid.Row="4" Text="Shortcut name" Height="16" VerticalAlignment="Bottom" Margin="0,20,0,4"></TextBlock>
<TextBox Name="tBoxShortcutName" IsEnabled="False" Grid.Row="5" Grid.ColumnSpan="2" Padding="6.02" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" VerticalAlignment="Center" Foreground="DimGray"/>

<Button Click="BtnCreate_Click" Grid.Row="4" Grid.ColumnSpan="2" Content="Create a shortcut" Height="30" VerticalAlignment="Bottom"></Button>
<Button Click="BtnCreate_Click" Grid.Row="6" Grid.ColumnSpan="2" Content="Create a shortcut" Height="30" VerticalAlignment="Bottom"></Button>
</Grid>
</Grid>

Expand All @@ -101,7 +110,7 @@
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="MaxHeight" To="35" />
<DoubleAnimation Duration="0:0:0.2" Storyboard.TargetProperty="MaxHeight" To="34" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
Expand Down
120 changes: 72 additions & 48 deletions ScriptRes/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace ScriptRes
/// </summary>
public partial class MainWindow : Window
{
private string _iconsSource = string.Empty;

public MainWindow()
{
InitializeComponent();
Expand All @@ -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<IconListItem>();
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<IconListItem>();
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";
}
}

Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}

Expand Down

0 comments on commit f228a7d

Please sign in to comment.