Skip to content

Commit afd1338

Browse files
committed
Issue #12 Added transparent and opaque items to the Action button
1 parent 8a7c019 commit afd1338

File tree

5 files changed

+119
-71
lines changed

5 files changed

+119
-71
lines changed

WindowTextExtractor/Forms/MainForm.Designer.cs

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

WindowTextExtractor/Forms/MainForm.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,14 @@ private void ActionButtonStripItemClicked(object sender, ToolStripItemClickedEve
638638
User32.ShowWindow(windowHandle, ShowWindowCommands.SW_SHOW);
639639
break;
640640

641+
case "miTransparent":
642+
WindowUtils.SetTransparency(windowHandle, 100);
643+
break;
644+
645+
case "miOpaque":
646+
WindowUtils.SetTransparency(windowHandle, 0);
647+
break;
648+
641649
case "miMinimize":
642650
User32.PostMessage(windowHandle, Constants.WM_SYSCOMMAND, Constants.SC_MINIMIZE, 0);
643651
break;

WindowTextExtractor/Native/Constants.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ static class Constants
2929
public const int DWL_DLGPROC = 4;
3030
public const int DWL_USER = 8;
3131

32+
// LayeredWindowAttributes
33+
public const int LWA_ALPHA = 0x00000002;
34+
35+
// WindowStyle
36+
public const long WS_EX_LAYERED = 0x00080000L;
37+
3238
public const int SC_MINIMIZE = 0xF020;
3339
public const int SC_MAXIMIZE = 0xF030;
3440
public const int SC_RESTORE = 0xF120;
@@ -40,7 +46,6 @@ static class Constants
4046
public const int CURSOR_SHOWING = 0x00000001;
4147

4248
public const int DI_COMPAT = 0x0004;
43-
4449
public const int DI_NORMAL = 0x0003;
4550
}
4651
}

WindowTextExtractor/Native/User32.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ static class User32
6060
public static extern IntPtr GetParent(IntPtr hWnd);
6161

6262
[DllImport("user32.dll")]
63-
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
63+
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, long dwNewLong);
6464

6565
[DllImport("user32.dll")]
66-
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
66+
public static extern long GetWindowLong(IntPtr hWnd, int nIndex);
6767

6868
[DllImport("user32.dll")]
6969
public static extern int GetClassLong(IntPtr hWnd, int nIndex);
@@ -117,5 +117,8 @@ static class User32
117117

118118
[DllImport("user32.dll")]
119119
public static extern bool RedrawWindow(IntPtr hWnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, RedrawWindowFlags flags);
120+
121+
[DllImport("user32.dll")]
122+
public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, Byte bAlpha, uint dwFlags);
120123
}
121124
}

WindowTextExtractor/Utils/WindowUtils.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,20 @@ public static Bitmap CaptureWindow(IntPtr handle, bool captureCursor = false)
310310
}
311311
}
312312

313+
public static void SetTransparency(IntPtr handle, int percent)
314+
{
315+
var opacity = (byte)Math.Round(255 * (100 - percent) / 100f, MidpointRounding.AwayFromZero);
316+
SetOpacity(handle, opacity);
317+
}
318+
319+
320+
public static void SetOpacity(IntPtr handle, byte opacity)
321+
{
322+
var exStyle = User32.GetWindowLong(handle, Constants.GWL_EXSTYLE);
323+
User32.SetWindowLong(handle, Constants.GWL_EXSTYLE, exStyle | Constants.WS_EX_LAYERED);
324+
User32.SetLayeredWindowAttributes(handle, 0, opacity, Constants.LWA_ALPHA);
325+
}
326+
313327
private static string GetWindowText(IntPtr handle)
314328
{
315329
var builder = new StringBuilder(1024);

0 commit comments

Comments
 (0)