Skip to content

Commit

Permalink
Added legacy drawing for the watermark text and removed enforce regis…
Browse files Browse the repository at this point in the history
…try create on install
  • Loading branch information
amgdy committed Apr 16, 2024
1 parent fdf1e6a commit fcb70fc
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
89 changes: 87 additions & 2 deletions src/Magdys.ScreenPrivacyWatermark.App/Forms/WatermarkForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private void WatermarkForm_FormClosing(object sender, FormClosingEventArgs e)
case CloseReason.MdiFormClosing:
case CloseReason.UserClosing:
case CloseReason.TaskManagerClosing:
//case CloseReason.ApplicationExitCall:
//case CloseReason.ApplicationExitCall:
e.Cancel = true;
break;
}
Expand Down Expand Up @@ -111,7 +111,14 @@ private void WatermarkForm_Paint(object sender, PaintEventArgs e)
{
_logger.LogTrace(_loggerExecutingText, nameof(WatermarkForm_Paint), Text);

DrawWatermarkText(e.Graphics);
if (_watermarkFormatSetting.Value.UseLegacyRendering)
{
DrawWatermarkTextLegacy(e.Graphics);
}
else
{
DrawWatermarkText(e.Graphics);
}

_logger.LogTrace(_loggerExecutedText, nameof(WatermarkForm_Paint), Text);
}
Expand Down Expand Up @@ -185,6 +192,84 @@ private void DrawWatermarkText(Graphics graphics)
_logger.LogTrace(_loggerExecutedText, nameof(DrawWatermarkText), Text);
}

private string GetFormattedWatermarkText()
{
_logger.LogTrace(_loggerExecutingText, nameof(GetFormattedWatermarkText), Text);

var watermarkText = _watermarkFormOptions.WatermarkText;

var spacer = new string(' ', _watermarkFormatSetting.Value.UseDynamicsSpacing ? new Random().Next(6, 15) : 10);

var watermarkBaseString = $"{watermarkText} {spacer}";

_logger.LogDebug("Watermark text: {_watermarkText}", watermarkBaseString);

var formatedText = new StringBuilder();
for (int i = 0; i < 100; i++)
{
formatedText.Append(watermarkBaseString);
}

_logger.LogTrace(_loggerExecutedText, nameof(GetFormattedWatermarkText), Text);

return formatedText.ToString();
}

private void DrawWatermarkTextLegacy(Graphics graphics)
{
_logger.LogTrace(_loggerExecutingText, nameof(DrawWatermarkText), Text);

ArgumentNullException.ThrowIfNull(graphics);

string watermarkText = GetFormattedWatermarkText();

graphics.TextRenderingHint = _watermarkFormatSetting.Value.TextRender;
graphics.SmoothingMode = SmoothingMode.Default;
graphics.InterpolationMode = InterpolationMode.Default;

using var font = new Font(_watermarkFormatSetting.Value.FontName, _watermarkFormatSetting.Value.FontSize, FontStyle.Regular, GraphicsUnit.Point);
using var brush = new SolidBrush(Color.FromArgb(255, _watermarkFormatSetting.Value.Color));
using var stringFormat = new StringFormat(StringFormatFlags.NoClip);


graphics.ResetTransform();
if (_watermarkFormatSetting.Value.UseDiagonalLines)
{
graphics.RotateTransform(-45);
}

double screenDiagonal = Math.Round(Math.Sqrt(Math.Pow(_watermarkFormOptions.Screen.WorkingArea.Width, 2) + Math.Pow(_watermarkFormOptions.Screen.WorkingArea.Height, 2)));

int repeatCounts = _watermarkFormatSetting.Value.LinesCount;

int x = _watermarkFormatSetting.Value.UseDiagonalLines ? (int)screenDiagonal : Bounds.Height;

int spacer = x / repeatCounts;

int firstPoint = _watermarkFormatSetting.Value.UseDynamicsSpacing ? new Random().Next(-2, 2) : 0;

for (int i = firstPoint; i < repeatCounts + 2 + firstPoint; i++)
{
var point = new Point(-spacer * i, spacer * i);

if (_watermarkFormatSetting.Value.OutlineColor == null)
{
graphics.DrawString(watermarkText, font, brush, point, stringFormat);
}
else
{
using GraphicsPath graphicPath = new GraphicsPath();
using Pen pen = new Pen(_watermarkFormatSetting.Value.OutlineColor.Value, _watermarkFormatSetting.Value.OutlineWidth);
graphicPath.AddString(watermarkText, font.FontFamily, (int)font.Style, font.Size * 1.5f, point, stringFormat);

graphics.DrawPath(pen, graphicPath);
graphics.FillPath(brush, graphicPath);
}
}

_logger.LogTrace(_loggerExecutedText, nameof(DrawWatermarkText), Text);
}

private void TimeProcessPolicyCheck_Tick(object sender, EventArgs e)
{
#if !(DEBUG)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ public bool UseDiagonalLines
#endif
} = true;

public bool UseLegacyRendering { get; set; } = true;

}

2 changes: 1 addition & 1 deletion src/Magdys.ScreenPrivacyWatermark.Setup/Package.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<Feature Id="Main" Title="Magdy's Screen Privacy Watermark">
<Files Include="!(bindpath.AppDir)**"></Files>
<Component Directory="INSTALLFOLDER" Id="ApplicationRegistryEntries" Guid="b486d8c0-3f80-41ed-ad31-c4d4d93d7f67">
<RegistryKey Root="HKLM" Key="SOFTWARE\MAGDYS\ScreenPrivacyWatermark" ForceCreateOnInstall="yes" ForceDeleteOnUninstall="no">
<RegistryKey Root="HKLM" Key="SOFTWARE\MAGDYS\ScreenPrivacyWatermark" ForceCreateOnInstall="no" ForceDeleteOnUninstall="no">
<RegistryValue Name="AACCS" Type="string" Value="" />
<RegistryValue Name="LogLevel" Type="string" Value="Info" />
</RegistryKey>
Expand Down

0 comments on commit fcb70fc

Please sign in to comment.