diff --git a/MsmhToolsClass/MsmhToolsClass/DnsTool/DNSCryptConfigEditor.cs b/MsmhToolsClass/MsmhToolsClass/DnsTool/DNSCryptConfigEditor.cs index 57f0409..548cac0 100644 --- a/MsmhToolsClass/MsmhToolsClass/DnsTool/DNSCryptConfigEditor.cs +++ b/MsmhToolsClass/MsmhToolsClass/DnsTool/DNSCryptConfigEditor.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Net; using System.Text; @@ -225,19 +226,26 @@ public void ChangePersonalServer(string[] sdns) public async Task WriteAsync() { - if (!FileDirectory.IsFileLocked(ConfigPath)) + try { - File.WriteAllText(ConfigPath, string.Empty); - for (int n = 0; n < ConfigList.Count; n++) + if (!FileDirectory.IsFileLocked(ConfigPath)) { - string line = ConfigList[n]; + File.WriteAllText(ConfigPath, string.Empty); + for (int n = 0; n < ConfigList.Count; n++) + { + string line = ConfigList[n]; - if (n == ConfigList.Count - 1) - await FileDirectory.AppendTextAsync(ConfigPath, line, new UTF8Encoding(false)); - else - await FileDirectory.AppendTextLineAsync(ConfigPath, line, new UTF8Encoding(false)); + if (n == ConfigList.Count - 1) + await FileDirectory.AppendTextAsync(ConfigPath, line, new UTF8Encoding(false)); + else + await FileDirectory.AppendTextLineAsync(ConfigPath, line, new UTF8Encoding(false)); + } + //File.WriteAllLines(ConfigPath, ConfigList); } - //File.WriteAllLines(ConfigPath, ConfigList); + } + catch (Exception ex) + { + Debug.WriteLine("DNSCryptConfigEditor WriteAsync: " + ex.Message); } } } \ No newline at end of file diff --git a/MsmhToolsClass/MsmhToolsClass/ExtensionsMethods.cs b/MsmhToolsClass/MsmhToolsClass/ExtensionsMethods.cs index 76a7fc4..3ede345 100644 --- a/MsmhToolsClass/MsmhToolsClass/ExtensionsMethods.cs +++ b/MsmhToolsClass/MsmhToolsClass/ExtensionsMethods.cs @@ -1,5 +1,4 @@ -using System; -using System.Reflection; +using System.Reflection; using System.Text; using System.Xml; using System.Text.RegularExpressions; diff --git a/MsmhToolsClass/MsmhToolsClass/ProcessManager.cs b/MsmhToolsClass/MsmhToolsClass/ProcessManager.cs index 7b3ee28..c736ce6 100644 --- a/MsmhToolsClass/MsmhToolsClass/ProcessManager.cs +++ b/MsmhToolsClass/MsmhToolsClass/ProcessManager.cs @@ -57,9 +57,13 @@ await Task.Run(async () => if (OperatingSystem.IsWindows()) { using PerformanceCounter performanceCounter = new("Process", "% Processor Time", processName, true); - performanceCounter.NextValue(); // Returns 0 - await Task.Delay(delay); // Needs time to calculate - result = performanceCounter.NextValue() / Environment.ProcessorCount; + performanceCounter.NextSample(); // Returns 0 + await Task.Delay(delay / 2); // Needs time to calculate + CounterSample first = performanceCounter.NextSample(); + await Task.Delay(delay / 2); // Needs time to calculate + CounterSample second = performanceCounter.NextSample(); + float final = CounterSample.Calculate(first, second); + result = final / Environment.ProcessorCount; } } catch (Exception ex) @@ -356,30 +360,44 @@ public static int ExecuteOnly(string processName, string? args = null, bool hide //----------------------------------------------------------------------------------- public static bool FindProcessByName(string processName) { - int result; - Process[] processes = Process.GetProcessesByName(processName); - result = processes.Length; + int result = 0; try { - for (int n = 0; n < processes.Length; n++) - processes[n].Dispose(); + Process[] processes = Process.GetProcessesByName(processName); + result = processes.Length; + try + { + for (int n = 0; n < processes.Length; n++) + processes[n].Dispose(); + } + catch (Exception) { } + } + catch (Exception ex) + { + Debug.WriteLine("FindProcessByName: " + ex.Message); } - catch (Exception) { } return result > 0; } //----------------------------------------------------------------------------------- public static bool FindProcessByPID(int pid) { bool result = false; - Process[] processes = Process.GetProcesses(); - for (int n = 0; n < processes.Length; n++) + try { - if (processes[n].Id == pid) + Process[] processes = Process.GetProcesses(); + for (int n = 0; n < processes.Length; n++) { - result = true; - break; + if (processes[n].Id == pid) + { + result = true; + break; + } + processes[n].Dispose(); } - processes[n].Dispose(); + } + catch (Exception ex) + { + Debug.WriteLine("FindProcessByPID: " + ex.Message); } return result; } diff --git a/MsmhToolsWinFormsClass/CustomControls/CustomButton.cs b/MsmhToolsWinFormsClass/CustomControls/CustomButton.cs index 468e39d..e73c026 100644 --- a/MsmhToolsWinFormsClass/CustomControls/CustomButton.cs +++ b/MsmhToolsWinFormsClass/CustomControls/CustomButton.cs @@ -1,6 +1,7 @@ using MsmhToolsClass; using System; using System.ComponentModel; +using System.Diagnostics; using System.Drawing.Drawing2D; using System.Windows.Forms.Design; /* @@ -228,200 +229,206 @@ private void CustomButton_EnabledChanged(object? sender, EventArgs e) private void CustomButton_Paint(object? sender, PaintEventArgs e) { - if (ApplicationIdle == false) - return; + if (ApplicationIdle == false) return; - if (sender is Button button) + try { - Color backColor = GetBackColor(button); - Color foreColor = GetForeColor(); - Color borderColor = GetBorderColor(); - - // Paint Background - if (Parent != null) + if (sender is Button button) { - e.Graphics.Clear(Parent.BackColor); - if (Parent.BackColor == Color.Transparent) - if (Parent is TabPage tabPage) - { - if (tabPage.Parent is CustomTabControl customTabControl) - e.Graphics.Clear(customTabControl.BackColor); - else if (tabPage.Parent is TabControl tabControl) - e.Graphics.Clear(tabControl.BackColor); - } - } - else - e.Graphics.Clear(backColor); + Color backColor = GetBackColor(button); + Color foreColor = GetForeColor(); + Color borderColor = GetBorderColor(); - Rectangle rect = new(0, 0, button.ClientSize.Width - 1, button.ClientSize.Height - 1); + // Paint Background + if (Parent != null) + { + e.Graphics.Clear(Parent.BackColor); + if (Parent.BackColor == Color.Transparent) + if (Parent is TabPage tabPage) + { + if (tabPage.Parent is CustomTabControl customTabControl) + e.Graphics.Clear(customTabControl.BackColor); + else if (tabPage.Parent is TabControl tabControl) + e.Graphics.Clear(tabControl.BackColor); + } + } + else + e.Graphics.Clear(backColor); - // Paint Button Background - using SolidBrush sbBG = new(backColor); - e.Graphics.FillRoundedRectangle(sbBG, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); + Rectangle rect = new(0, 0, button.ClientSize.Width - 1, button.ClientSize.Height - 1); - // Paint Hover Button Background (// MousePosition Or Cursor.Position) (Faster than rect.Contains(button.PointToClient(MousePosition))) - if (button.PointToScreen(Point.Empty).X <= MousePosition.X - && MousePosition.X <= (button.PointToScreen(Point.Empty).X + rect.Width) - && button.PointToScreen(Point.Empty).Y <= MousePosition.Y - && MousePosition.Y <= (button.PointToScreen(Point.Empty).Y + rect.Height)) - { - if (ButtonMouseHover) + // Paint Button Background + using SolidBrush sbBG = new(backColor); + e.Graphics.FillRoundedRectangle(sbBG, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); + + // Paint Hover Button Background (// MousePosition Or Cursor.Position) (Faster than rect.Contains(button.PointToClient(MousePosition))) + if (button.PointToScreen(Point.Empty).X <= MousePosition.X + && MousePosition.X <= (button.PointToScreen(Point.Empty).X + rect.Width) + && button.PointToScreen(Point.Empty).Y <= MousePosition.Y + && MousePosition.Y <= (button.PointToScreen(Point.Empty).Y + rect.Height)) { - if (ButtonMouseDown) + if (ButtonMouseHover) { - Color mouseDownBackColor; - if (backColor.DarkOrLight() == "Dark") - mouseDownBackColor = backColor.ChangeBrightness(0.2f); + if (ButtonMouseDown) + { + Color mouseDownBackColor; + if (backColor.DarkOrLight() == "Dark") + mouseDownBackColor = backColor.ChangeBrightness(0.2f); + else + mouseDownBackColor = backColor.ChangeBrightness(-0.2f); + + using SolidBrush sbDBG = new(mouseDownBackColor); + e.Graphics.FillRoundedRectangle(sbDBG, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); + ButtonMouseDown = false; // Fix a minor bug. + } else - mouseDownBackColor = backColor.ChangeBrightness(-0.2f); + { + Color mouseHoverBackColor; + if (backColor.DarkOrLight() == "Dark") + mouseHoverBackColor = BackColor.ChangeBrightness(0.1f); + else + mouseHoverBackColor = BackColor.ChangeBrightness(-0.1f); + + using SolidBrush sbHBG = new(mouseHoverBackColor); + e.Graphics.FillRoundedRectangle(sbHBG, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); + } + } + } + + if (button.Enabled && button.Focused) + { + rect.Inflate(-2, -2); + using Pen pen = new(SelectionColor) { DashStyle = DashStyle.Dash }; + //if (RoundedCorners == 0) + // e.Graphics.DrawRectangle(pen, rect); + //else + e.Graphics.DrawRoundedRectangle(pen, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); + rect.Inflate(+2, +2); + } - using SolidBrush sbDBG = new(mouseDownBackColor); - e.Graphics.FillRoundedRectangle(sbDBG, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); - ButtonMouseDown = false; // Fix a minor bug. + // Paint Image + if (Image != null) + { + Rectangle rectImage = new(0, 0, Image.Width, Image.Height); + int pad = 2; + int top = rect.Y + pad; + int bottom = rect.Y + (rect.Height - rectImage.Height) - pad; + int left = rect.X + pad; + int right = rect.X + (rect.Width - rectImage.Width) - pad; + int centerX = rect.X + ((rect.Width - rectImage.Width) / 2); + int centerY = rect.Y + ((rect.Height - rectImage.Height) / 2); + if (RightToLeft == RightToLeft.No) + { + if (ImageAlign == ContentAlignment.BottomCenter) + rectImage.Location = new(centerX, bottom); + else if (ImageAlign == ContentAlignment.BottomLeft) + rectImage.Location = new(left, bottom); + else if (ImageAlign == ContentAlignment.BottomRight) + rectImage.Location = new(right, bottom); + else if (ImageAlign == ContentAlignment.MiddleCenter) + rectImage.Location = new(centerX, centerY); + else if (ImageAlign == ContentAlignment.MiddleLeft) + rectImage.Location = new(left, centerY); + else if (ImageAlign == ContentAlignment.MiddleRight) + rectImage.Location = new(right, centerY); + else if (ImageAlign == ContentAlignment.TopCenter) + rectImage.Location = new(centerX, top); + else if (ImageAlign == ContentAlignment.TopLeft) + rectImage.Location = new(left, top); + else if (ImageAlign == ContentAlignment.TopRight) + rectImage.Location = new(right, top); + else + rectImage.Location = new(centerX, centerY); } else { - Color mouseHoverBackColor; - if (backColor.DarkOrLight() == "Dark") - mouseHoverBackColor = BackColor.ChangeBrightness(0.1f); + if (ImageAlign == ContentAlignment.BottomCenter) + rectImage.Location = new(centerX, bottom); + else if (ImageAlign == ContentAlignment.BottomLeft) + rectImage.Location = new(right, bottom); + else if (ImageAlign == ContentAlignment.BottomRight) + rectImage.Location = new(left, bottom); + else if (ImageAlign == ContentAlignment.MiddleCenter) + rectImage.Location = new(centerX, centerY); + else if (ImageAlign == ContentAlignment.MiddleLeft) + rectImage.Location = new(right, centerY); + else if (ImageAlign == ContentAlignment.MiddleRight) + rectImage.Location = new(left, centerY); + else if (ImageAlign == ContentAlignment.TopCenter) + rectImage.Location = new(centerX, top); + else if (ImageAlign == ContentAlignment.TopLeft) + rectImage.Location = new(right, top); + else if (ImageAlign == ContentAlignment.TopRight) + rectImage.Location = new(left, top); else - mouseHoverBackColor = BackColor.ChangeBrightness(-0.1f); - - using SolidBrush sbHBG = new(mouseHoverBackColor); - e.Graphics.FillRoundedRectangle(sbHBG, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); + rectImage.Location = new(centerX, centerY); } + + e.Graphics.DrawImage(Image, rectImage); } - } - if (button.Enabled && button.Focused) - { - rect.Inflate(-2, -2); - using Pen pen = new(SelectionColor) { DashStyle = DashStyle.Dash }; - //if (RoundedCorners == 0) - // e.Graphics.DrawRectangle(pen, rect); - //else - e.Graphics.DrawRoundedRectangle(pen, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); - rect.Inflate(+2, +2); - } + // Paint Button Text + TextFormatFlags flags; - // Paint Image - if (Image != null) - { - Rectangle rectImage = new(0, 0, Image.Width, Image.Height); - int pad = 2; - int top = rect.Y + pad; - int bottom = rect.Y + (rect.Height - rectImage.Height) - pad; - int left = rect.X + pad; - int right = rect.X + (rect.Width - rectImage.Width) - pad; - int centerX = rect.X + ((rect.Width - rectImage.Width) / 2); - int centerY = rect.Y + ((rect.Height - rectImage.Height) / 2); if (RightToLeft == RightToLeft.No) { - if (ImageAlign == ContentAlignment.BottomCenter) - rectImage.Location = new(centerX, bottom); - else if (ImageAlign == ContentAlignment.BottomLeft) - rectImage.Location = new(left, bottom); - else if (ImageAlign == ContentAlignment.BottomRight) - rectImage.Location = new(right, bottom); - else if (ImageAlign == ContentAlignment.MiddleCenter) - rectImage.Location = new(centerX, centerY); - else if (ImageAlign == ContentAlignment.MiddleLeft) - rectImage.Location = new(left, centerY); - else if (ImageAlign == ContentAlignment.MiddleRight) - rectImage.Location = new(right, centerY); - else if (ImageAlign == ContentAlignment.TopCenter) - rectImage.Location = new(centerX, top); - else if (ImageAlign == ContentAlignment.TopLeft) - rectImage.Location = new(left, top); - else if (ImageAlign == ContentAlignment.TopRight) - rectImage.Location = new(right, top); + if (TextAlign == ContentAlignment.BottomCenter) + flags = TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter; + else if (TextAlign == ContentAlignment.BottomLeft) + flags = TextFormatFlags.Bottom | TextFormatFlags.Left; + else if (TextAlign == ContentAlignment.BottomRight) + flags = TextFormatFlags.Bottom | TextFormatFlags.Right; + else if (TextAlign == ContentAlignment.MiddleCenter) + flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter; + else if (TextAlign == ContentAlignment.MiddleLeft) + flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Left; + else if (TextAlign == ContentAlignment.MiddleRight) + flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Right; + else if (TextAlign == ContentAlignment.TopCenter) + flags = TextFormatFlags.Top | TextFormatFlags.HorizontalCenter; + else if (TextAlign == ContentAlignment.TopLeft) + flags = TextFormatFlags.Top | TextFormatFlags.Left; + else if (TextAlign == ContentAlignment.TopRight) + flags = TextFormatFlags.Top | TextFormatFlags.Right; else - rectImage.Location = new(centerX, centerY); + flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter; } else { - if (ImageAlign == ContentAlignment.BottomCenter) - rectImage.Location = new(centerX, bottom); - else if (ImageAlign == ContentAlignment.BottomLeft) - rectImage.Location = new(right, bottom); - else if (ImageAlign == ContentAlignment.BottomRight) - rectImage.Location = new(left, bottom); - else if (ImageAlign == ContentAlignment.MiddleCenter) - rectImage.Location = new(centerX, centerY); - else if (ImageAlign == ContentAlignment.MiddleLeft) - rectImage.Location = new(right, centerY); - else if (ImageAlign == ContentAlignment.MiddleRight) - rectImage.Location = new(left, centerY); - else if (ImageAlign == ContentAlignment.TopCenter) - rectImage.Location = new(centerX, top); - else if (ImageAlign == ContentAlignment.TopLeft) - rectImage.Location = new(right, top); - else if (ImageAlign == ContentAlignment.TopRight) - rectImage.Location = new(left, top); + if (TextAlign == ContentAlignment.BottomCenter) + flags = TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter; + else if (TextAlign == ContentAlignment.BottomLeft) + flags = TextFormatFlags.Bottom | TextFormatFlags.Right; + else if (TextAlign == ContentAlignment.BottomRight) + flags = TextFormatFlags.Bottom | TextFormatFlags.Left; + else if (TextAlign == ContentAlignment.MiddleCenter) + flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter; + else if (TextAlign == ContentAlignment.MiddleLeft) + flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Right; + else if (TextAlign == ContentAlignment.MiddleRight) + flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Left; + else if (TextAlign == ContentAlignment.TopCenter) + flags = TextFormatFlags.Top | TextFormatFlags.HorizontalCenter; + else if (TextAlign == ContentAlignment.TopLeft) + flags = TextFormatFlags.Top | TextFormatFlags.Right; + else if (TextAlign == ContentAlignment.TopRight) + flags = TextFormatFlags.Top | TextFormatFlags.Left; else - rectImage.Location = new(centerX, centerY); - } + flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter; - e.Graphics.DrawImage(Image, rectImage); - } + flags |= TextFormatFlags.RightToLeft; + } - // Paint Button Text - TextFormatFlags flags; + TextRenderer.DrawText(e.Graphics, button.Text, button.Font, rect, foreColor, flags); - if (RightToLeft == RightToLeft.No) - { - if (TextAlign == ContentAlignment.BottomCenter) - flags = TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter; - else if (TextAlign == ContentAlignment.BottomLeft) - flags = TextFormatFlags.Bottom | TextFormatFlags.Left; - else if (TextAlign == ContentAlignment.BottomRight) - flags = TextFormatFlags.Bottom | TextFormatFlags.Right; - else if (TextAlign == ContentAlignment.MiddleCenter) - flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter; - else if (TextAlign == ContentAlignment.MiddleLeft) - flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Left; - else if (TextAlign == ContentAlignment.MiddleRight) - flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Right; - else if (TextAlign == ContentAlignment.TopCenter) - flags = TextFormatFlags.Top | TextFormatFlags.HorizontalCenter; - else if (TextAlign == ContentAlignment.TopLeft) - flags = TextFormatFlags.Top | TextFormatFlags.Left; - else if (TextAlign == ContentAlignment.TopRight) - flags = TextFormatFlags.Top | TextFormatFlags.Right; - else - flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter; + // Paint Button Border + using Pen penB = new(borderColor); + e.Graphics.DrawRoundedRectangle(penB, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); } - else - { - if (TextAlign == ContentAlignment.BottomCenter) - flags = TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter; - else if (TextAlign == ContentAlignment.BottomLeft) - flags = TextFormatFlags.Bottom | TextFormatFlags.Right; - else if (TextAlign == ContentAlignment.BottomRight) - flags = TextFormatFlags.Bottom | TextFormatFlags.Left; - else if (TextAlign == ContentAlignment.MiddleCenter) - flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter; - else if (TextAlign == ContentAlignment.MiddleLeft) - flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Right; - else if (TextAlign == ContentAlignment.MiddleRight) - flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Left; - else if (TextAlign == ContentAlignment.TopCenter) - flags = TextFormatFlags.Top | TextFormatFlags.HorizontalCenter; - else if (TextAlign == ContentAlignment.TopLeft) - flags = TextFormatFlags.Top | TextFormatFlags.Right; - else if (TextAlign == ContentAlignment.TopRight) - flags = TextFormatFlags.Top | TextFormatFlags.Left; - else - flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter; - - flags |= TextFormatFlags.RightToLeft; - } - - TextRenderer.DrawText(e.Graphics, button.Text, button.Font, rect, foreColor, flags); - - // Paint Button Border - using Pen penB = new(borderColor); - e.Graphics.DrawRoundedRectangle(penB, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); + } + catch (Exception ex) + { + Debug.WriteLine("CustomButton Paint: " + ex.Message); } } diff --git a/MsmhToolsWinFormsClass/CustomControls/CustomCheckBox.cs b/MsmhToolsWinFormsClass/CustomControls/CustomCheckBox.cs index c864254..86a8490 100644 --- a/MsmhToolsWinFormsClass/CustomControls/CustomCheckBox.cs +++ b/MsmhToolsWinFormsClass/CustomControls/CustomCheckBox.cs @@ -1,5 +1,4 @@ using MsmhToolsClass; -using System; using System.ComponentModel; using System.Drawing.Design; using System.Drawing.Drawing2D; @@ -205,27 +204,37 @@ private void CustomCheckBox_Paint(object? sender, PaintEventArgs e) checkBox.FlatAppearance.BorderSize = 0; checkBox.AutoSize = false; checkBox.UseVisualStyleBackColor = false; + SizeF sizeF = checkBox.CreateGraphics().MeasureString(checkBox.Text, checkBox.Font); SizeF rectSizeF = sizeF; - if (checkBox.Text.Contains(Environment.NewLine)) - rectSizeF = checkBox.CreateGraphics().MeasureString(checkBox.Text.Split(Environment.NewLine)[0], checkBox.Font); - int rectSize = Convert.ToInt32(rectSizeF.Height - 2); - checkBox.Height = Convert.ToInt32(sizeF.Height); - checkBox.Width = Convert.ToInt32(sizeF.Width + rectSize + 5); + int rectSize = 10; + try + { + if (checkBox.Text.Contains(Environment.NewLine)) + rectSizeF = checkBox.CreateGraphics().MeasureString(checkBox.Text.Split(Environment.NewLine)[0], checkBox.Font); + rectSize = Convert.ToInt32(Math.Round(rectSizeF.Height) - 2); + checkBox.Height = Convert.ToInt32(Math.Round(sizeF.Height)); + checkBox.Width = Convert.ToInt32(Math.Round(sizeF.Width) + rectSize); + } + catch (Exception) + { + // do nothing + } + int x; float textX; - - if (checkBox.RightToLeft == RightToLeft.No) + + if (checkBox.RightToLeft != RightToLeft.Yes) { checkBox.TextAlign = ContentAlignment.MiddleLeft; - x = 1; - textX = (float)(rectSize * 1.3); + x = 0; + textX = rectSize; } else { checkBox.TextAlign = ContentAlignment.MiddleRight; - x = checkBox.Width - rectSize - 2; - textX = checkBox.Width - sizeF.Width - (float)(rectSize * 1.2); + x = checkBox.Width - rectSize; + textX = 0; } int y = (checkBox.ClientRectangle.Y + 1 + (checkBox.ClientRectangle.Height - rectSize)) / 2; diff --git a/MsmhToolsWinFormsClass/CustomControls/CustomLabel.cs b/MsmhToolsWinFormsClass/CustomControls/CustomLabel.cs index 8cf1be8..1f3d1eb 100644 --- a/MsmhToolsWinFormsClass/CustomControls/CustomLabel.cs +++ b/MsmhToolsWinFormsClass/CustomControls/CustomLabel.cs @@ -174,9 +174,19 @@ private void CustomLabel_Paint(object? sender, PaintEventArgs e) else e.Graphics.Clear(backColor); - if (AutoSize) - Size = e.Graphics.MeasureString(Text, Font).ToSize(); + { + try + { + SizeF sizeF = e.Graphics.MeasureString(Text, Font); + Width = Convert.ToInt32(Math.Round(sizeF.Width)); + Height = Convert.ToInt32(Math.Round(sizeF.Height)); + } + catch (Exception) + { + // do nothing + } + } Rectangle rect = new(0, 0, ClientRectangle.Width - 1, ClientRectangle.Height - 1); diff --git a/MsmhToolsWinFormsClass/CustomControls/CustomProgressBar.cs b/MsmhToolsWinFormsClass/CustomControls/CustomProgressBar.cs index 7fd32cf..3bfea7a 100644 --- a/MsmhToolsWinFormsClass/CustomControls/CustomProgressBar.cs +++ b/MsmhToolsWinFormsClass/CustomControls/CustomProgressBar.cs @@ -171,142 +171,153 @@ private void CustomProgressBar_RightToLeftChanged(object? sender, EventArgs e) protected override void OnPaint(PaintEventArgs e) { - if (!ApplicationIdle) return; - - Color backColor = GetBackColor(); - Color foreColor = GetForeColor(); - Color borderColor = GetBorderColor(); - Color chunksColor = GetChunksColor(); - Color chunksColorGradient; - - if (chunksColor.DarkOrLight() == "Dark") - chunksColorGradient = chunksColor.ChangeBrightness(0.5f); - else - chunksColorGradient = chunksColor.ChangeBrightness(-0.5f); - - //Rectangle rect = ClientRectangle; - Rectangle rect = new(0, 0, ClientRectangle.Width - 1, ClientRectangle.Height - 1); - Graphics g = e.Graphics; - // Draw horizontal bar (Background and Border) With Default System Color: - //ProgressBarRenderer.DrawHorizontalBar(g, rect); - - // Draw horizontal bar (Background and Border) With Custom Color: - // Fill Background - using SolidBrush bgBrush = new(backColor); - g.FillRoundedRectangle(bgBrush, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); - - // Draw Border - using Pen penB = new(borderColor); - g.DrawRoundedRectangle(penB, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); - - // Min - if (Value == Minimum) + try { - if (!StopWatch.IsRunning) StopWatch.Start(); - StopWatch.Restart(); - return; // Performance - } + if (!ApplicationIdle) return; - // Padding - if (Value > 0) - { - // Draw Chunks By Default Color (Green): - //Rectangle clip = new(rect.X, rect.Y, (int)Math.Round((float)Value / Maximum * rect.Width), rect.Height); - //ProgressBarRenderer.DrawHorizontalChunks(g, clip); + Color backColor = GetBackColor(); + Color foreColor = GetForeColor(); + Color borderColor = GetBorderColor(); + Color chunksColor = GetChunksColor(); + Color chunksColorGradient; + + if (chunksColor.DarkOrLight() == "Dark") + chunksColorGradient = chunksColor.ChangeBrightness(0.5f); + else + chunksColorGradient = chunksColor.ChangeBrightness(-0.5f); + + //Rectangle rect = ClientRectangle; + Rectangle rect = new(0, 0, ClientRectangle.Width - 1, ClientRectangle.Height - 1); + Graphics g = e.Graphics; + // Draw horizontal bar (Background and Border) With Default System Color: + //ProgressBarRenderer.DrawHorizontalBar(g, rect); - // Draw Chunks By Custom Color: - // The Following Is The Width Of The Bar. This Will Vary With Each Value. - int fillWidth = rect.Width * Value / (Maximum - Minimum); + // Draw horizontal bar (Background and Border) With Custom Color: + // Fill Background + using SolidBrush bgBrush = new(backColor); + g.FillRoundedRectangle(bgBrush, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); - // GDI+ Doesn't Like Rectangles 0px Wide or Height - if (fillWidth == 0) + // Draw Border + using Pen penB = new(borderColor); + g.DrawRoundedRectangle(penB, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); + + // Min + if (Value == Minimum) { - // Draw Only Border And Exit + if (!StopWatch.IsRunning) StopWatch.Start(); + StopWatch.Restart(); + return; // Performance + } + + // Compute Percent + int percent = (int)(Value / (double)Maximum * 100); + + // Padding + if (Value > 0) + { + // Draw Chunks By Default Color (Green): + //Rectangle clip = new(rect.X, rect.Y, (int)Math.Round((float)Value / Maximum * rect.Width), rect.Height); + //ProgressBarRenderer.DrawHorizontalChunks(g, clip); + + // Draw Chunks By Custom Color: + // The Following Is The Width Of The Bar. This Will Vary With Each Value. + int fillWidth = rect.Width * Value / (Maximum - Minimum); + + // GDI+ Doesn't Like Rectangles 0px Wide or Height + if (fillWidth == 0) + { + // Draw Only Border And Exit + g.DrawRoundedRectangle(penB, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); + return; + } + + // Rectangles For Upper And Lower Half Of Bar + int y = percent < 2 ? 1 : 0; + Rectangle topRect = new(0, y, fillWidth, (rect.Height / 2) + 1 - y); // +1 to avoid "having a dark line in the middle of the bar" + Rectangle buttomRect = new(0, (rect.Height / 2) - y, fillWidth, (rect.Height / 2) - y); + + // Paint Upper Half + int right = Value < RoundedCorners ? Value : RoundedCorners; + using LinearGradientBrush gbUH = new(new Point(topRect.X, topRect.Y), new Point(topRect.X, topRect.Height), chunksColorGradient, chunksColor); + g.FillRoundedRectangle(gbUH, topRect, RoundedCorners, right, 0, 0); + + // Paint Lower Half + // -1 to avoid "out of memory exception" + using LinearGradientBrush gbLH = new(new Point(buttomRect.X, buttomRect.Y - 1), new Point(buttomRect.X, buttomRect.Height), chunksColor, chunksColorGradient); + g.FillRoundedRectangle(gbLH, buttomRect, 0, 0, right, RoundedCorners); + + // Paint Border g.DrawRoundedRectangle(penB, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); - return; } - // Rectangles For Upper And Lower Half Of Bar - int y = Value < 2 ? 1 : 0; - Rectangle topRect = new(0, y, fillWidth, (rect.Height / 2) + 1 - y); // +1 to avoid "having a dark line in the middle of the bar" - Rectangle buttomRect = new(0, (rect.Height / 2) - y, fillWidth, (rect.Height / 2) - y); - - // Paint Upper Half - int right = Value < RoundedCorners ? Value : RoundedCorners; - using LinearGradientBrush gbUH = new(new Point(topRect.X, topRect.Y), new Point(topRect.X, topRect.Height), chunksColorGradient, chunksColor); - g.FillRoundedRectangle(gbUH, topRect, RoundedCorners, right, 0, 0); - - // Paint Lower Half - // -1 to avoid "out of memory exception" - using LinearGradientBrush gbLH = new(new Point(buttomRect.X, buttomRect.Y - 1), new Point(buttomRect.X, buttomRect.Height), chunksColor, chunksColorGradient); - g.FillRoundedRectangle(gbLH, buttomRect, 0, 0, right, RoundedCorners); - - // Paint Border - g.DrawRoundedRectangle(penB, rect, RoundedCorners, RoundedCorners, RoundedCorners, RoundedCorners); - } - // Compute Percent - int percent = (int)(Value / (double)Maximum * 100); - string textPercent; - if (Value > 0) - textPercent = percent.ToString() + '%'; - else - { - // If Value Is Zero Don't Write Anything - textPercent = string.Empty; - if (!DesignMode) - CustomText = string.Empty; - } + // Percentage + string textPercent; + if (Value > Minimum) + textPercent = percent.ToString() + '%'; + else if (Value == Maximum) + textPercent = "100%"; + else + { + // If Value Is Zero Don't Write Anything + textPercent = string.Empty; + if (!DesignMode) CustomText = string.Empty; + } - // Brush For Writing CustomText And Persentage On Progressbar - using SolidBrush brush = new(foreColor); + // Brush For Writing CustomText And Persentage On Progressbar + using SolidBrush brush = new(foreColor); - // Percent - SizeF lenPercent = g.MeasureString(textPercent, Font); - Point locationPercentCenter = new(Convert.ToInt32((Width / 2) - lenPercent.Width / 2), Convert.ToInt32((Height / 2) - lenPercent.Height / 2)); - g.DrawString(textPercent, Font, brush, locationPercentCenter); + // Percent + SizeF lenPercent = g.MeasureString(textPercent, Font); + Point locationPercentCenter = new(Convert.ToInt32((Width / 2) - lenPercent.Width / 2), Convert.ToInt32((Height / 2) - lenPercent.Height / 2)); + g.DrawString(textPercent, Font, brush, locationPercentCenter); - // Custom Text - if (!string.IsNullOrEmpty(CustomText)) - { - SizeF lenCustomText = g.MeasureString(CustomText, Font); - if (RightToLeft == RightToLeft.No) + // Custom Text + if (!string.IsNullOrEmpty(CustomText)) { - Point locationCustomTextLeft = new(5, Convert.ToInt32((Height / 2) - lenCustomText.Height / 2)); - g.DrawString(CustomText, Font, brush, locationCustomTextLeft); + SizeF lenCustomText = g.MeasureString(CustomText, Font); + if (RightToLeft == RightToLeft.No) + { + Point locationCustomTextLeft = new(5, Convert.ToInt32((Height / 2) - lenCustomText.Height / 2)); + g.DrawString(CustomText, Font, brush, locationCustomTextLeft); + } + else + { + Point locationCustomTextRight = new(Convert.ToInt32(Width - lenCustomText.Width - 5), Convert.ToInt32((Height / 2) - lenCustomText.Height / 2)); + g.DrawString(CustomText, Font, brush, locationCustomTextRight); + } } - else + + // Compute Elapsed Time + ElapsedTimeString = timer(out TimeSpan _); + + // Max + if (StopTimer || Value == Maximum) { - Point locationCustomTextRight = new(Convert.ToInt32(Width - lenCustomText.Width - 5), Convert.ToInt32((Height / 2) - lenCustomText.Height / 2)); - g.DrawString(CustomText, Font, brush, locationCustomTextRight); + if (StopWatch.IsRunning) StopWatch.Stop(); } - } - // Compute Elapsed Time - ElapsedTimeString = timer(out TimeSpan _); - - // Max - if (StopTimer || Value == Maximum) - { - if (StopWatch.IsRunning) StopWatch.Stop(); - } + string timer(out TimeSpan timeSpan) + { + TimeSpan eTime = StopWatch.Elapsed; + eTime = timeSpan = TimeSpan.FromMilliseconds(Math.Round(eTime.TotalMilliseconds, 1)); + return $"{eTime:hh\\:mm\\:ss\\.f}"; + } - string timer(out TimeSpan timeSpan) - { - TimeSpan eTime = StopWatch.Elapsed; - eTime = timeSpan = TimeSpan.FromMilliseconds(Math.Round(eTime.TotalMilliseconds, 1)); - return $"{eTime:hh\\:mm\\:ss\\.f}"; - } - - SizeF lenElapsedTime = g.MeasureString(ElapsedTimeString, Font); - if (RightToLeft == RightToLeft.No) - { - Point locationElapsedTimeRight = new(Convert.ToInt32(Width - lenElapsedTime.Width - 5), Convert.ToInt32((Height / 2) - lenElapsedTime.Height / 2)); - g.DrawString(ElapsedTimeString, Font, brush, locationElapsedTimeRight); + SizeF lenElapsedTime = g.MeasureString(ElapsedTimeString, Font); + if (RightToLeft == RightToLeft.No) + { + Point locationElapsedTimeRight = new(Convert.ToInt32(Width - lenElapsedTime.Width - 5), Convert.ToInt32((Height / 2) - lenElapsedTime.Height / 2)); + g.DrawString(ElapsedTimeString, Font, brush, locationElapsedTimeRight); + } + else + { + Point locationElapsedTimeLeft = new(5, Convert.ToInt32((Height / 2) - lenElapsedTime.Height / 2)); + g.DrawString(ElapsedTimeString, Font, brush, locationElapsedTimeLeft); + } } - else + catch (Exception ex) { - Point locationElapsedTimeLeft = new(5, Convert.ToInt32((Height / 2) - lenElapsedTime.Height / 2)); - g.DrawString(ElapsedTimeString, Font, brush, locationElapsedTimeLeft); + Debug.WriteLine("CustomProgressBar OnPaint: " + ex.Message); } } diff --git a/MsmhToolsWinFormsClass/CustomControls/CustomRadioButton.cs b/MsmhToolsWinFormsClass/CustomControls/CustomRadioButton.cs index b1f4854..f8f1455 100644 --- a/MsmhToolsWinFormsClass/CustomControls/CustomRadioButton.cs +++ b/MsmhToolsWinFormsClass/CustomControls/CustomRadioButton.cs @@ -212,17 +212,17 @@ private void CustomRadioButton_Paint(object? sender, PaintEventArgs e) int x; float textX; - if (rb.RightToLeft == RightToLeft.No) + if (rb.RightToLeft != RightToLeft.Yes) { rb.TextAlign = ContentAlignment.MiddleLeft; - x = 1; - textX = (float)(rectSize * 1.3); + x = 0; + textX = rectSize; } else { rb.TextAlign = ContentAlignment.MiddleRight; - x = rb.Width - rectSize - 2; - textX = rb.Width - sizeF.Width - (float)(rectSize * 1.2); + x = rb.Width - rectSize; + textX = 0; } int y = (rb.ClientRectangle.Y + (rb.ClientRectangle.Height - rectSize)) / 2; diff --git a/MsmhToolsWinFormsClass/CustomControls/CustomTextBox.cs b/MsmhToolsWinFormsClass/CustomControls/CustomTextBox.cs index fd83965..db6b7fa 100644 --- a/MsmhToolsWinFormsClass/CustomControls/CustomTextBox.cs +++ b/MsmhToolsWinFormsClass/CustomControls/CustomTextBox.cs @@ -351,8 +351,12 @@ public CustomTextBox() : base() BackColorChanged += CustomTextBox_BackColorChanged; ForeColorChanged += CustomTextBox_ForeColorChanged; textBox.Click += TextBox_Click; + textBox.MouseDown += TextBox_MouseDown; textBox.MouseEnter += TextBox_MouseEnter; + textBox.MouseHover += TextBox_MouseHover; textBox.MouseLeave += TextBox_MouseLeave; + textBox.MouseMove += TextBox_MouseMove; + textBox.MouseUp += TextBox_MouseUp; textBox.KeyPress += TextBox_KeyPress; textBox.Enter += TextBox_Enter; textBox.Leave += TextBox_Leave; @@ -498,6 +502,7 @@ private void TextBox_TextAlignChanged(object? sender, EventArgs e) public new event EventHandler? TextChanged; private void TextBox_TextChanged(object? sender, EventArgs e) { + //OnTextChanged(e); TextChanged?.Invoke(sender, e); } @@ -506,6 +511,7 @@ private void TextBox_TextChanged(object? sender, EventArgs e) public new event EventHandler? LostFocus; private void TextBox_LostFocus(object? sender, EventArgs e) { + //OnLostFocus(e); LostFocus?.Invoke(sender, e); } @@ -514,6 +520,7 @@ private void TextBox_LostFocus(object? sender, EventArgs e) public new event EventHandler? GotFocus; private void TextBox_GotFocus(object? sender, EventArgs e) { + //OnGotFocus(e); GotFocus?.Invoke(sender, e); } @@ -534,16 +541,36 @@ private void TextBox_Click(object? sender, EventArgs e) OnClick(e); } + private void TextBox_MouseDown(object? sender, MouseEventArgs e) + { + OnMouseDown(e); + } + private void TextBox_MouseEnter(object? sender, EventArgs e) { OnMouseEnter(e); } + private void TextBox_MouseHover(object? sender, EventArgs e) + { + OnMouseHover(e); + } + private void TextBox_MouseLeave(object? sender, EventArgs e) { OnMouseLeave(e); } + private void TextBox_MouseMove(object? sender, MouseEventArgs e) + { + OnMouseMove(e); + } + + private void TextBox_MouseUp(object? sender, MouseEventArgs e) + { + OnMouseUp(e); + } + private void TextBox_KeyPress(object? sender, KeyPressEventArgs e) { OnKeyPress(e); @@ -552,17 +579,20 @@ private void TextBox_KeyPress(object? sender, KeyPressEventArgs e) private void TextBox_Enter(object? sender, EventArgs e) { isFocused = true; + OnEnter(e); Invalidate(); } private void TextBox_Leave(object? sender, EventArgs e) { isFocused = false; + OnLeave(e); Invalidate(); } private void TextBox_Invalidated(object? sender, InvalidateEventArgs e) { + OnInvalidated(e); if (BackColor.DarkOrLight() == "Dark") Methods.SetDarkControl(textBox); textBox.Enabled = Enabled; diff --git a/SDCProxyServer/ExecuteCommands.cs b/SDCProxyServer/ExecuteCommands.cs index 88b85a8..9cc616d 100644 --- a/SDCProxyServer/ExecuteCommands.cs +++ b/SDCProxyServer/ExecuteCommands.cs @@ -73,6 +73,21 @@ await Task.Run(async () => Help.GetHelpPrograms(); } + // Parent Process + else if (input.ToLower().StartsWith(Key.ParentProcess.Name.ToLower())) + { + // ParentProcess -PID=m + + string key = Key.ParentProcess.PID; + bool isValueOk = ConsoleTools.GetValueByKey(input, key, true, false, out string value); + if (!isValueOk) return; + isValueOk = ConsoleTools.GetInt(key, value, true, 0, int.MaxValue, out int pid); + if (!isValueOk) return; + + ParentPID = pid; + ShowParentProcessMsg(); + } + // Settings else if (input.ToLower().StartsWith(Key.Setting.Name.ToLower())) { diff --git a/SDCProxyServer/Help.cs b/SDCProxyServer/Help.cs index 07a9926..e1ddde8 100644 --- a/SDCProxyServer/Help.cs +++ b/SDCProxyServer/Help.cs @@ -58,14 +58,29 @@ public static void GetHelp() help = $" Stop Proxy Server."; WriteToStdout(help); + // Parent Process ID + help = $"\n{Key.ParentProcess.Name}