Skip to content

Commit 85e070b

Browse files
committed
Upgraded to C# 9 and refactoring
1 parent 40ffb62 commit 85e070b

File tree

11 files changed

+189
-240
lines changed

11 files changed

+189
-240
lines changed

Build/Build.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<WindowTextExtractorDestFileWin64 Include="$(ApplicationPath)\WindowTextExtractor64.exe" />
2020
<WindowTextExtractorSourceConfgiFile Include="$(WindowTextExtractorProjectPath)\bin\x86\Release\WindowTextExtractor.exe.config" />
2121
<WindowTextExtractorDestConfgiFile Include="$(ApplicationPath)\WindowTextExtractor.exe.config" />
22-
<WindowTextExtractorSourceLibFiles Include="$(WindowTextExtractorProjectPath)\bin\x86\Release\*.dll" />
22+
<WindowTextExtractorSourceLibFiles Include="$(WindowTextExtractorProjectPath)\Libs\*dll;$(WindowTextExtractorProjectPath)\bin\x86\Release\zxing.dll;$(WindowTextExtractorProjectPath)\bin\x86\Release\zxing.presentation.dll" />
2323
<WindowTextExtractorDestLibFiles Include="$(ApplicationPath)\" />
2424
</ItemGroup>
2525

WindowTextExtractor.sln

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.28307.852
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.33529.622
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WindowTextExtractor", "WindowTextExtractor\WindowTextExtractor.csproj", "{784E5D61-B08C-47E4-936A-03C9F67B71D0}"
77
EndProject

WindowTextExtractor/Extensions/ProcessExtensions.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,15 @@ public static Process GetParentProcess(this Process process)
4040
}
4141
}
4242

43-
public static Priority GetProcessPriority(this Process process)
43+
public static Priority GetProcessPriority(this Process process) => Kernel32.GetPriorityClass(process.Handle) switch
4444
{
45-
var priorityClass = Kernel32.GetPriorityClass(process.Handle);
46-
switch (priorityClass)
47-
{
48-
case PriorityClass.REALTIME_PRIORITY_CLASS: return Priority.RealTime;
49-
case PriorityClass.HIGH_PRIORITY_CLASS: return Priority.High;
50-
case PriorityClass.ABOVE_NORMAL_PRIORITY_CLASS: return Priority.AboveNormal;
51-
case PriorityClass.NORMAL_PRIORITY_CLASS: return Priority.Normal;
52-
case PriorityClass.BELOW_NORMAL_PRIORITY_CLASS: return Priority.BelowNormal;
53-
case PriorityClass.IDLE_PRIORITY_CLASS: return Priority.Idle;
54-
default: return Priority.Normal;
55-
}
56-
}
45+
PriorityClass.REALTIME_PRIORITY_CLASS => Priority.RealTime,
46+
PriorityClass.HIGH_PRIORITY_CLASS => Priority.High,
47+
PriorityClass.ABOVE_NORMAL_PRIORITY_CLASS => Priority.AboveNormal,
48+
PriorityClass.NORMAL_PRIORITY_CLASS => Priority.Normal,
49+
PriorityClass.BELOW_NORMAL_PRIORITY_CLASS => Priority.BelowNormal,
50+
PriorityClass.IDLE_PRIORITY_CLASS => Priority.Idle,
51+
_ => Priority.Normal
52+
};
5753
}
5854
}

WindowTextExtractor/Forms/AboutForm.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ partial class AboutForm : Form
1212
public AboutForm()
1313
{
1414
InitializeComponent();
15-
Text = "About " + AssemblyUtils.AssemblyProductName;
16-
lblProductName.Text = string.Format("{0} v{1}", AssemblyUtils.AssemblyProductName, AssemblyUtils.AssemblyProductVersion);
17-
lblCopyright.Text = string.Format("{0}-{1} {2}", AssemblyUtils.AssemblyCopyright, DateTime.Now.Year, AssemblyUtils.AssemblyCompany);
15+
Text = $"About {AssemblyUtils.AssemblyProductName}";
16+
lblProductName.Text = $"{AssemblyUtils.AssemblyProductName} v{AssemblyUtils.AssemblyProductVersion}";
17+
lblCopyright.Text = $"{AssemblyUtils.AssemblyCopyright}-{DateTime.Now.Year} {AssemblyUtils.AssemblyCompany}";
1818
linkUrl.Text = URL;
1919
}
2020

WindowTextExtractor/Forms/MainForm.cs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ public partial class MainForm : Form, IMessageFilter
4646
private string _listText;
4747
private int _fps;
4848
private decimal _scale;
49-
private object _lockObject;
5049
private bool _refreshImage;
5150
private bool _imageTab;
5251
private bool _isRecording;
5352
private bool _captureCursor;
5453
private DateTime? _startRecordingTime;
55-
private VideoFileWriter _videoWriter;
5654
private Bitmap _image;
55+
private readonly object _lockObject;
56+
private readonly VideoFileWriter _videoWriter;
5757

5858
private AccurateTimer _captureWindowTimer;
5959
private AccurateTimer _updatePictureBoxTimer;
@@ -67,10 +67,11 @@ public MainForm()
6767

6868
AppDomain.CurrentDomain.UnhandledException += OnCurrentDomainUnhandledException;
6969
Application.ThreadException += OnThreadException;
70+
using var currentProcess = Process.GetCurrentProcess();
7071

7172
_lockObject = new object();
7273
_isButtonTargetMouseDown = false;
73-
_processId = Process.GetCurrentProcess().Id;
74+
_processId = currentProcess.Id;
7475
_messageId = User32.RegisterWindowMessage("WINDOW_TEXT_EXTRACTOR_HOOK");
7576
_64BitFilePath = string.Empty;
7677
_informationFileName = string.Empty;
@@ -132,8 +133,7 @@ protected override void OnLoad(EventArgs e)
132133
_64BitFilePath = Path.Combine(directoryName, fileName);
133134
if (!File.Exists(_64BitFilePath))
134135
{
135-
var message = string.Format("{0} is not found.", fileName);
136-
MessageBox.Show(message, AssemblyUtils.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
136+
MessageBox.Show($"{fileName} is not found.", AssemblyUtils.AssemblyTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
137137
Close();
138138
return;
139139
}
@@ -345,7 +345,7 @@ private void menuItemSaveEnvironmentAs_Click(object sender, EventArgs e)
345345
var builder = new StringBuilder(1024);
346346
foreach (var variableKey in variables.Keys)
347347
{
348-
builder.AppendFormat($"{variableKey.PadRight(paddingSize)}: {variables[variableKey]}{Environment.NewLine}");
348+
builder.AppendLine($"{variableKey,-paddingSize}: {variables[variableKey]}");
349349
}
350350
content = builder.ToString();
351351
}
@@ -357,10 +357,13 @@ private void menuItemSaveEnvironmentAs_Click(object sender, EventArgs e)
357357

358358
private void menuItemFont_Click(object sender, EventArgs e)
359359
{
360-
var dialog = new FontDialog();
361-
dialog.ShowHelp = false;
362-
dialog.ShowColor = false;
363-
dialog.Font = txtContent.Font;
360+
var dialog = new FontDialog
361+
{
362+
ShowHelp = false,
363+
ShowColor = false,
364+
Font = txtContent.Font
365+
};
366+
364367
if (dialog.ShowDialog() != DialogResult.Cancel)
365368
{
366369
txtContent.Font = dialog.Font;
@@ -644,7 +647,7 @@ public bool PreFilterMessage(ref Message m)
644647
return false;
645648
}
646649

647-
var process = Process.GetProcessById(element.Current.ProcessId);
650+
using var process = Process.GetProcessById(element.Current.ProcessId);
648651
if (element.Current.IsPassword)
649652
{
650653
if (process.ProcessName.ToLower() == "iexplore")
@@ -667,7 +670,7 @@ public bool PreFilterMessage(ref Message m)
667670
Process.Start(new ProcessStartInfo
668671
{
669672
FileName = _64BitFilePath,
670-
Arguments = string.Format("{0} {1} {2}", Handle.ToInt32(), element.Current.NativeWindowHandle, _messageId)
673+
Arguments = $"{Handle.ToInt32()} {element.Current.NativeWindowHandle} {_messageId}"
671674
});
672675
}
673676
else
@@ -701,11 +704,9 @@ public bool PreFilterMessage(ref Message m)
701704
}
702705
else
703706
{
704-
using (var image = WindowUtils.CaptureWindow(windowHandle, captureCursor))
705-
{
706-
var newImage = ImageUtils.ResizeImage(image, (int)(image.Width * scale), (int)(image.Height * scale));
707-
FillImage(newImage);
708-
}
707+
using var image = WindowUtils.CaptureWindow(windowHandle, captureCursor);
708+
var newImage = ImageUtils.ResizeImage(image, (int)(image.Width * scale), (int)(image.Height * scale));
709+
FillImage(newImage);
709710
}
710711
var windowInformation = WindowUtils.GetWindowInformation(windowHandle, cursorPosition);
711712
FillInformation(windowInformation);
@@ -776,10 +777,8 @@ private void CaptureWindowCallback()
776777
}
777778
else
778779
{
779-
using (var sourceImage = WindowUtils.CaptureWindow(windowHandle, captureCursor))
780-
{
781-
newImage = ImageUtils.ResizeImage(sourceImage, (int)(sourceImage.Width * scale), (int)(sourceImage.Height * scale));
782-
}
780+
using var sourceImage = WindowUtils.CaptureWindow(windowHandle, captureCursor);
781+
newImage = ImageUtils.ResizeImage(sourceImage, (int)(sourceImage.Width * scale), (int)(sourceImage.Height * scale));
783782
}
784783
}
785784
}
@@ -1028,8 +1027,7 @@ private void BindLanguages()
10281027

10291028
private void OnCurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
10301029
{
1031-
var ex = e.ExceptionObject as Exception;
1032-
ex = ex ?? new Exception("OnCurrentDomainUnhandledException");
1030+
var ex = e.ExceptionObject as Exception ?? new Exception("OnCurrentDomainUnhandledException");
10331031
OnThreadException(sender, new ThreadExceptionEventArgs(ex));
10341032
}
10351033

WindowTextExtractor/Utils/AssemblyUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static string AssemblyProductVersion
4141
get
4242
{
4343
var version = Assembly.GetExecutingAssembly().GetName().Version;
44-
return string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build);
44+
return $"{version.Major}.{version.Minor}.{version.Build}";
4545
}
4646
}
4747

WindowTextExtractor/Utils/FileUtils.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ static class FileUtils
88
{
99
public static void Save(string fileName, XDocument document)
1010
{
11-
using (TextWriter writer = new Utf8StringWriter())
12-
{
13-
document.Save(writer, SaveOptions.None);
14-
File.WriteAllText(fileName, writer.ToString());
15-
}
11+
using var writer = new Utf8StringWriter();
12+
document.Save(writer, SaveOptions.None);
13+
File.WriteAllText(fileName, writer.ToString());
1614
}
1715

1816
private class Utf8StringWriter : StringWriter

0 commit comments

Comments
 (0)