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

Commit 137a316

Browse files
v0.4.0: First release, input validation, icon added.
1 parent f228a7d commit 137a316

5 files changed

Lines changed: 87 additions & 14 deletions

File tree

QRes.exe

4.5 KB
Binary file not shown.

ScriptRes/MainWindow.xaml

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,64 @@
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:local="clr-namespace:ScriptRes"
77
mc:Ignorable="d"
8-
Title="MainWindow" Height="550" Width="350" ResizeMode="CanMinimize" UseLayoutRounding="False" Loaded="Window_Loaded">
8+
Title="ScriptRes by Andrii Ihnatiuk" Height="550" Width="350" Loaded="Window_Loaded" MinWidth="310" MinHeight="550" ScrollViewer.CanContentScroll="True" MaxHeight="550">
99
<Grid Margin="10">
1010
<Grid.RowDefinitions>
1111
<RowDefinition Height="100"></RowDefinition>
1212
<RowDefinition Height="80"></RowDefinition>
1313
<RowDefinition Height="1*"></RowDefinition>
1414
</Grid.RowDefinitions>
15-
<GroupBox Header="Display Settings" Grid.Row="0" UseLayoutRounding="False" Padding="5">
15+
<GroupBox Header="Display Settings" Grid.Row="0" Padding="5">
1616
<Grid>
1717
<Grid.RowDefinitions>
1818
<RowDefinition Height="1*"></RowDefinition>
1919
<RowDefinition Height="1*"></RowDefinition>
2020
</Grid.RowDefinitions>
2121
<Grid.ColumnDefinitions>
2222
<ColumnDefinition Width="1*"></ColumnDefinition>
23-
<ColumnDefinition Width="60"></ColumnDefinition>
24-
<ColumnDefinition Width="60"></ColumnDefinition>
23+
<ColumnDefinition Width="70"></ColumnDefinition>
24+
<ColumnDefinition Width="70"></ColumnDefinition>
2525
</Grid.ColumnDefinitions>
2626

2727
<TextBlock Text="Initial resolution:" Padding="0" HorizontalAlignment="Left" VerticalAlignment="Center"/>
28-
<TextBox Name="tBoxInX" Text="1920" HorizontalAlignment="Right" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" TextAlignment="Center" MaxLength="4" VerticalAlignment="Center" Width="50" Grid.Column="1"/>
29-
<TextBox Name="tBoxInY" Text="1080" HorizontalAlignment="Right" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" TextAlignment="Center" MaxLength="4" VerticalAlignment="Center" Width="50" Grid.Column="2"/>
28+
<Grid Grid.Column="1">
29+
<Grid.ColumnDefinitions>
30+
<ColumnDefinition Width="1*"/>
31+
<ColumnDefinition Width="50"/>
32+
</Grid.ColumnDefinitions>
33+
34+
<TextBlock Text="X:" LineHeight="18" VerticalAlignment="Center" TextAlignment="Right" Margin="0,0,4,0"/>
35+
<TextBox Name="tBoxInX" HorizontalAlignment="Right" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" TextAlignment="Center" MaxLength="4" VerticalAlignment="Center" Width="50" Grid.Column="1" Padding="1.02" PreviewTextInput="TBox_AllowNumbersOnly"/>
36+
</Grid>
37+
<Grid Grid.Column="2">
38+
<Grid.ColumnDefinitions>
39+
<ColumnDefinition Width="1*"/>
40+
<ColumnDefinition Width="50"/>
41+
</Grid.ColumnDefinitions>
42+
43+
<TextBlock Text="Y:" LineHeight="18" VerticalAlignment="Center" TextAlignment="Right" Margin="0,0,4,0"/>
44+
<TextBox Name="tBoxInY" HorizontalAlignment="Right" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" TextAlignment="Center" MaxLength="4" VerticalAlignment="Center" Width="50" Grid.Column="2" Padding="1.02" PreviewTextInput="TBox_AllowNumbersOnly"/>
45+
</Grid>
3046

3147
<TextBlock Text="Preferred resolution:" Padding="0" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="1"/>
32-
<TextBox Name="tBoxOutX" Text="1280" HorizontalAlignment="Right" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" TextAlignment="Center" MaxLength="4" VerticalAlignment="Center" Width="50" Grid.Column="1" Grid.Row="1"/>
33-
<TextBox Name="tBoxOutY" Text="720" HorizontalAlignment="Right" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" TextAlignment="Center" MaxLength="4" VerticalAlignment="Center" Width="50" Grid.Column="2" Grid.Row="1"/>
48+
<Grid Grid.Column="1" Grid.Row="1">
49+
<Grid.ColumnDefinitions>
50+
<ColumnDefinition Width="1*"/>
51+
<ColumnDefinition Width="50"/>
52+
</Grid.ColumnDefinitions>
53+
54+
<TextBlock Text="X:" LineHeight="18" VerticalAlignment="Center" TextAlignment="Right" Margin="0,0,4,0"/>
55+
<TextBox Name="tBoxOutX" HorizontalAlignment="Right" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" TextAlignment="Center" MaxLength="4" VerticalAlignment="Center" Width="50" Grid.Column="1" Grid.Row="1" Padding="1.02" PreviewTextInput="TBox_AllowNumbersOnly"/>
56+
</Grid>
57+
<Grid Grid.Column="2" Grid.Row="1">
58+
<Grid.ColumnDefinitions>
59+
<ColumnDefinition Width="1*"/>
60+
<ColumnDefinition Width="50"/>
61+
</Grid.ColumnDefinitions>
62+
63+
<TextBlock Text="Y:" LineHeight="18" VerticalAlignment="Center" TextAlignment="Right" Margin="0,0,4,0"/>
64+
<TextBox Name="tBoxOutY" HorizontalAlignment="Right" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" TextAlignment="Center" MaxLength="4" VerticalAlignment="Center" Width="50" Grid.Column="2" Grid.Row="1" Padding="1.02" PreviewTextInput="TBox_AllowNumbersOnly"/>
65+
</Grid>
3466
</Grid>
3567
</GroupBox>
3668

@@ -89,7 +121,7 @@
89121
<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"/>
90122

91123
<TextBlock Grid.Row="4" Text="Shortcut name" Height="16" VerticalAlignment="Bottom" Margin="0,20,0,4"></TextBlock>
92-
<TextBox Name="tBoxShortcutName" IsEnabled="False" Grid.Row="5" Grid.ColumnSpan="2" Padding="6.02" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" VerticalAlignment="Center" Foreground="DimGray"/>
124+
<TextBox Name="tBoxShortcutName" IsEnabled="False" Grid.Row="5" Grid.ColumnSpan="2" Padding="6.02" TextWrapping="NoWrap" AcceptsReturn="False" AcceptsTab="False" VerticalAlignment="Center" Foreground="DimGray" PreviewTextInput="TBoxShortcutName_ValidateShortcutName" MaxLength="50" />
93125

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

ScriptRes/MainWindow.xaml.cs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.IO;
6+
using System.Text.RegularExpressions;
67
using System.Windows;
78
using System.Windows.Controls;
89
using System.Windows.Media.Imaging;
@@ -26,6 +27,9 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
2627
chBoxQresPath.IsChecked = true;
2728
chBoxQresPath.Checked += CheckBox_Checked;
2829
chBoxQresPath.Unchecked += CheckBox_Checked;
30+
31+
tBoxInX.Text = SystemParameters.PrimaryScreenWidth.ToString();
32+
tBoxInY.Text = SystemParameters.PrimaryScreenHeight.ToString();
2933
}
3034

3135
// Handler for buttons to select file locations
@@ -58,12 +62,12 @@ private void BtnBrowse_Click(object sender, RoutedEventArgs e)
5862
{
5963
extractedIcons = IconUtil.ExtractAllIcons(_iconsSource);
6064
}
61-
catch (FileNotFoundException exc)
65+
catch (FileNotFoundException exc)
6266
{
6367
MessageBox.Show($"An error occured while extracting icons from the file.\nDetails: {exc.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
6468
return;
6569
}
66-
70+
6771
// Display all icons in ListBox
6872
var itemsSource = new List<IconListItem>();
6973
for (int i = 0; i < extractedIcons.Length; i++)
@@ -101,6 +105,7 @@ private void BtnCreate_Click(object sender, RoutedEventArgs e)
101105

102106
if (string.IsNullOrEmpty(inX) || string.IsNullOrEmpty(inY) || string.IsNullOrEmpty(outX) || string.IsNullOrEmpty(outY) || string.IsNullOrEmpty(qresPath))
103107
{
108+
MessageBox.Show("Not all fields are filled in!", "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
104109
return;
105110
}
106111

@@ -160,7 +165,7 @@ private void BtnCreate_Click(object sender, RoutedEventArgs e)
160165
File.WriteAllText(batchLocation, script);
161166

162167
string shortcutName = string.IsNullOrWhiteSpace(tBoxShortcutName.Text) ? execNameNoExt : tBoxShortcutName.Text;
163-
168+
164169
CreateShortcut(desktopPath, batchLocation, _iconsSource, shortcutName);
165170
MessageBox.Show($"The shortcut was created: \n{desktopPath}\\{shortcutName}.lnk", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
166171
}
@@ -170,7 +175,7 @@ private void BtnCreate_Click(object sender, RoutedEventArgs e)
170175
}
171176
}
172177

173-
private void CheckBox_Checked(object sender, EventArgs e)
178+
private void CheckBox_Checked(object sender, EventArgs e)
174179
{
175180
if (sender is not CheckBox box) return;
176181

@@ -198,5 +203,36 @@ private void CreateShortcut(string shortcutPath, string targetPath, string? icon
198203
}
199204
shortcut.Save();
200205
}
206+
207+
// PreviewTextInput: allow only numbers in the textbox
208+
private void TBox_AllowNumbersOnly(object sender, System.Windows.Input.TextCompositionEventArgs e)
209+
{
210+
if (sender is not TextBox tBox) return;
211+
// If selection starts from the first character - forbid to enter 0
212+
if (tBox.SelectionStart == 0)
213+
{
214+
e.Handled = !NumbersMatch().IsMatch(e.Text);
215+
}
216+
else
217+
{
218+
e.Handled = !NumbersMatch().IsMatch(tBox.Text + e.Text);
219+
}
220+
}
221+
222+
// PreviewTextInput: forbid special characters in shortcut name
223+
private void TBoxShortcutName_ValidateShortcutName(object sender, System.Windows.Input.TextCompositionEventArgs e)
224+
{
225+
if (sender is not TextBox) return;
226+
e.Handled = !InvalidShortcutCharactersMatch().IsMatch(e.Text);
227+
}
228+
229+
// Compile time regular expressions
230+
231+
[GeneratedRegex(@"^[1-9]\d*$")]
232+
private static partial Regex NumbersMatch();
233+
234+
[GeneratedRegex(@"^[^\\/:\*\?""<>|]$")]
235+
private static partial Regex InvalidShortcutCharactersMatch();
236+
201237
}
202-
}
238+
}

ScriptRes/ScriptRes.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<TargetFramework>net7.0-windows</TargetFramework>
66
<Nullable>enable</Nullable>
77
<UseWPF>true</UseWPF>
8+
<ApplicationIcon>icons8-resolution-96.ico</ApplicationIcon>
89
</PropertyGroup>
910

1011
<ItemGroup>
@@ -19,6 +20,10 @@
1920
</COMReference>
2021
</ItemGroup>
2122

23+
<ItemGroup>
24+
<Content Include="icons8-resolution-96.ico" />
25+
</ItemGroup>
26+
2227
<ItemGroup>
2328
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
2429
</ItemGroup>

ScriptRes/icons8-resolution-96.ico

37.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)