Skip to content

Commit 8cc1ad2

Browse files
committed
Issues #36 Realtime transparency preview
1 parent a8dbd3e commit 8cc1ad2

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

SmartContextMenu/Forms/TransparencyForm.Designer.cs

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SmartContextMenu/Forms/TransparencyForm.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Windows.Forms;
3+
using SmartContextMenu.Utils;
34

45
namespace SmartContextMenu.Forms
56
{
@@ -19,8 +20,14 @@ private void InitializeControls(LanguageManager manager, Window window)
1920
Text = manager.GetString("trans_form");
2021
numericTransparency.Value = window.Transparency;
2122
DialogResult = DialogResult.Cancel;
23+
numericTransparency.TextChanged += NumericTransparencyValueChanged;
24+
ChangeTransparency();
2225
}
2326

27+
private void NumericTransparencyValueChanged(object sender, EventArgs e) => ChangeTransparency();
28+
29+
private void NumericTransparencyKeyDown(object sender, KeyEventArgs e) => ChangeTransparency();
30+
2431
private void ButtonApplyClick(object sender, EventArgs e)
2532
{
2633
WindowTransparency = (int)numericTransparency.Value;
@@ -40,5 +47,11 @@ private void FormKeyDown(object sender, KeyEventArgs e)
4047
Close();
4148
}
4249
}
50+
51+
private void ChangeTransparency()
52+
{
53+
var opacity = WindowUtils.TransparencyToAlphaOpacity((int)numericTransparency.Value);
54+
WindowUtils.SetOpacity(Handle, opacity);
55+
}
4356
}
4457
}

SmartContextMenu/Utils/WindowUtils.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,5 +566,9 @@ public static Rect GetSystemMargins(IntPtr hWnd)
566566
}
567567

568568
public static Func<int, double> TransparencyToOpacity = t => 1 - (t / 100.0);
569+
570+
public static Func<int, byte> TransparencyToAlphaOpacity = t => (byte)Math.Round(255 * (100 - t) / 100f, MidpointRounding.AwayFromZero);
571+
572+
public static Func<byte, int> AlphaOpacityToTransparency = o => 100 - (int)Math.Round(100 * o / 255f, MidpointRounding.AwayFromZero);
569573
}
570574
}

SmartContextMenu/Window.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ public int Transparency
101101
var isLayeredWindow = (style & WS_EX_LAYERED) == WS_EX_LAYERED;
102102
if (!isLayeredWindow) return 0;
103103
GetLayeredWindowAttributes(Handle, out _, out var alpha, out _);
104-
var transparency = 100 - (int)Math.Round(100 * alpha / 255f, MidpointRounding.AwayFromZero);
105-
return transparency;
104+
return WindowUtils.AlphaOpacityToTransparency(alpha);
106105
}
107106
}
108107

@@ -314,7 +313,7 @@ public void Resume()
314313

315314
public void SetTransparency(int percent)
316315
{
317-
var opacity = (byte)Math.Round(255 * (100 - percent) / 100f, MidpointRounding.AwayFromZero);
316+
var opacity = WindowUtils.TransparencyToAlphaOpacity(percent);
318317
WindowUtils.SetOpacity(Handle, opacity);
319318
}
320319

0 commit comments

Comments
 (0)