-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayeredWindow.cs
More file actions
193 lines (159 loc) · 5.5 KB
/
Copy pathLayeredWindow.cs
File metadata and controls
193 lines (159 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace Rounder.Windows;
public abstract class LayeredWindow : Form
{
private const int WsExLayered = 0x00080000;
private const int WsExTransparent = 0x00000020;
private const int WsExToolWindow = 0x00000080;
private const int WsExNoActivate = 0x08000000;
private const int HwndTopmost = -1;
private const byte AcSrcOver = 0x00;
private const byte AcSrcAlpha = 0x01;
private const int UlwAlpha = 0x00000002;
private const uint SwpNoSize = 0x0001;
private const uint SwpNoMove = 0x0002;
private const uint SwpNoActivate = 0x0010;
private const uint SwpShowWindow = 0x0040;
private const uint SwpNoOwnerZOrder = 0x0200;
private const uint SwpNoSendChanging = 0x0400;
private Rectangle layerBounds;
private Bitmap? surface;
protected LayeredWindow()
{
AutoScaleMode = AutoScaleMode.None;
FormBorderStyle = FormBorderStyle.None;
ShowInTaskbar = false;
StartPosition = FormStartPosition.Manual;
TopMost = true;
}
protected override bool ShowWithoutActivation => true;
protected int LayerWidth => layerBounds.Width;
protected int LayerHeight => layerBounds.Height;
protected override CreateParams CreateParams
{
get
{
var cp = base.CreateParams;
cp.ExStyle |= WsExLayered | WsExTransparent | WsExToolWindow | WsExNoActivate;
return cp;
}
}
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
ApplyNativeBounds();
}
protected void SetLayerBounds(Rectangle bounds)
{
layerBounds = bounds;
Bounds = bounds;
ApplyNativeBounds();
}
protected void RenderLayer()
{
if (!IsHandleCreated || IsDisposed || layerBounds.Width <= 0 || layerBounds.Height <= 0)
{
return;
}
if (surface is null || surface.Width != layerBounds.Width || surface.Height != layerBounds.Height)
{
surface?.Dispose();
surface = new Bitmap(layerBounds.Width, layerBounds.Height, PixelFormat.Format32bppPArgb);
}
DrawLayer(surface);
var screenDc = GetDC(IntPtr.Zero);
var memoryDc = CreateCompatibleDC(screenDc);
var hBitmap = surface.GetHbitmap(Color.FromArgb(0));
var oldBitmap = SelectObject(memoryDc, hBitmap);
try
{
var size = new SizeRef(layerBounds.Width, layerBounds.Height);
var source = new PointRef(0, 0);
var destination = new PointRef(layerBounds.Left, layerBounds.Top);
var blend = new BlendFunction
{
BlendOp = AcSrcOver,
BlendFlags = 0,
SourceConstantAlpha = 255,
AlphaFormat = AcSrcAlpha
};
UpdateLayeredWindow(Handle, screenDc, ref destination, ref size, memoryDc, ref source, 0, ref blend, UlwAlpha);
}
finally
{
SelectObject(memoryDc, oldBitmap);
DeleteObject(hBitmap);
DeleteDC(memoryDc);
ReleaseDC(IntPtr.Zero, screenDc);
}
}
protected abstract void DrawLayer(Bitmap surface);
protected override void Dispose(bool disposing)
{
if (disposing)
{
surface?.Dispose();
surface = null;
}
base.Dispose(disposing);
}
protected void KeepAboveTaskbar()
{
ApplyNativeBounds();
}
private void ApplyNativeBounds()
{
if (!IsHandleCreated || IsDisposed || layerBounds.Width <= 0 || layerBounds.Height <= 0)
{
return;
}
SetWindowPos(Handle, HwndTopmost, layerBounds.Left, layerBounds.Top, layerBounds.Width, layerBounds.Height, SwpNoActivate | SwpShowWindow | SwpNoOwnerZOrder | SwpNoSendChanging);
}
[StructLayout(LayoutKind.Sequential)]
private struct PointRef
{
public int X;
public int Y;
public PointRef(int x, int y)
{
X = x;
Y = y;
}
}
[StructLayout(LayoutKind.Sequential)]
private struct SizeRef
{
public int Cx;
public int Cy;
public SizeRef(int cx, int cy)
{
Cx = cx;
Cy = cy;
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct BlendFunction
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
}
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref PointRef pptDst, ref SizeRef psize, IntPtr hdcSrc, ref PointRef pptSrc, int crKey, ref BlendFunction pblend, int dwFlags);
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDc);
[DllImport("gdi32.dll")]
private static extern IntPtr CreateCompatibleDC(IntPtr hDc);
[DllImport("gdi32.dll")]
private static extern bool DeleteDC(IntPtr hDc);
[DllImport("gdi32.dll")]
private static extern IntPtr SelectObject(IntPtr hDc, IntPtr hObject);
[DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
}