forked from seerge/g-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSDBase.cs
463 lines (438 loc) · 18 KB
/
OSDBase.cs
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace OSD
{
class OSDNativeForm : NativeWindow, IDisposable
{
private bool _disposed = false;
private byte _alpha = 250;
private Size _size = new Size(350, 50);
private Point _location = new Point(50, 50);
protected virtual void PerformPaint(PaintEventArgs e)
{
}
protected internal void Invalidate()
{
this.UpdateLayeredWindow();
}
private void UpdateLayeredWindow()
{
Bitmap bitmap1 = new Bitmap(this.Size.Width, this.Size.Height, PixelFormat.Format32bppArgb);
using (Graphics graphics1 = Graphics.FromImage(bitmap1))
{
Rectangle rectangle1;
SIZE size1;
POINT point1;
POINT point2;
BLENDFUNCTION blendfunction1;
rectangle1 = new Rectangle(0, 0, this.Size.Width, this.Size.Height);
PerformPaint(new PaintEventArgs(graphics1, rectangle1));
IntPtr ptr1 = User32.GetDC(IntPtr.Zero);
IntPtr ptr2 = Gdi32.CreateCompatibleDC(ptr1);
IntPtr ptr3 = bitmap1.GetHbitmap(Color.FromArgb(0));
IntPtr ptr4 = Gdi32.SelectObject(ptr2, ptr3);
size1.cx = this.Size.Width;
size1.cy = this.Size.Height;
point1.x = this.Location.X;
point1.x = this.Location.X;
point1.y = this.Location.Y;
point2.x = 0;
point2.y = 0;
blendfunction1 = new BLENDFUNCTION();
blendfunction1.BlendOp = 0;
blendfunction1.BlendFlags = 0;
blendfunction1.SourceConstantAlpha = this._alpha;
blendfunction1.AlphaFormat = 1;
User32.UpdateLayeredWindow(base.Handle, ptr1, ref point1, ref size1, ptr2, ref point2, 0, ref blendfunction1, 2); //2=ULW_ALPHA
Gdi32.SelectObject(ptr2, ptr4);
User32.ReleaseDC(IntPtr.Zero, ptr1);
Gdi32.DeleteObject(ptr3);
Gdi32.DeleteDC(ptr2);
}
}
public virtual void Show()
{
if (base.Handle == IntPtr.Zero) //if handle don't equal to zero - window was created and just hided
this.CreateWindowOnly();
User32.ShowWindow(base.Handle, User32.SW_SHOWNOACTIVATE);
}
public virtual void Hide()
{
if (base.Handle == IntPtr.Zero)
return;
User32.ShowWindow(base.Handle, User32.SW_HIDE);
this.DestroyHandle();
}
public virtual void Close()
{
this.Hide();
this.Dispose();
}
private void CreateWindowOnly()
{
CreateParams params1 = new CreateParams();
params1.Caption = "FloatingNativeWindow";
int nX = this._location.X;
int nY = this._location.Y;
Screen screen1 = Screen.FromHandle(base.Handle);
if ((nX + this._size.Width) > screen1.Bounds.Width)
{
nX = screen1.Bounds.Width - this._size.Width;
}
if ((nY + this._size.Height) > screen1.Bounds.Height)
{
nY = screen1.Bounds.Height - this._size.Height;
}
this._location = new Point(nX, nY);
Size size1 = this._size;
Point point1 = this._location;
params1.X = nX;
params1.Y = nY;
params1.Height = size1.Height;
params1.Width = size1.Width;
params1.Parent = IntPtr.Zero;
uint ui = User32.WS_POPUP;
params1.Style = (int)ui;
params1.ExStyle = User32.WS_EX_TOPMOST | User32.WS_EX_TOOLWINDOW | User32.WS_EX_LAYERED | User32.WS_EX_NOACTIVATE | User32.WS_EX_TRANSPARENT;
this.CreateHandle(params1);
this.UpdateLayeredWindow();
}
protected virtual void SetBoundsCore(int x, int y, int width, int height)
{
if (((this.X != x) || (this.Y != y)) || ((this.Width != width) || (this.Height != height)))
{
if (base.Handle != IntPtr.Zero)
{
int num1 = 20;
if ((this.X == x) && (this.Y == y))
{
num1 |= 2;
}
if ((this.Width == width) && (this.Height == height))
{
num1 |= 1;
}
User32.SetWindowPos(base.Handle, IntPtr.Zero, x, y, width, height, (uint)num1);
}
else
{
this.Location = new Point(x, y);
this.Size = new Size(width, height);
}
}
}
#region # Properties #
/// <summary>
/// Get or set position of top-left corner of floating native window in screen coordinates
/// </summary>
public virtual Point Location
{
get { return this._location; }
set
{
if (base.Handle != IntPtr.Zero)
{
this.SetBoundsCore(value.X, value.Y, this._size.Width, this._size.Height);
RECT rect = new RECT();
User32.GetWindowRect(base.Handle, ref rect);
this._location = new Point(rect.left, rect.top);
this.UpdateLayeredWindow();
}
else
{
this._location = value;
}
}
}
/// <summary>
/// Get or set size of client area of floating native window
/// </summary>
public virtual Size Size
{
get { return this._size; }
set
{
if (base.Handle != IntPtr.Zero)
{
this.SetBoundsCore(this._location.X, this._location.Y, value.Width, value.Height);
RECT rect = new RECT();
User32.GetWindowRect(base.Handle, ref rect);
this._size = new Size(rect.right - rect.left, rect.bottom - rect.top);
this.UpdateLayeredWindow();
}
else
{
this._size = value;
}
}
}
/// <summary>
/// Gets or sets the height of the floating native window
/// </summary>
public int Height
{
get { return this._size.Height; }
set
{
this._size = new Size(this._size.Width, value);
}
}
/// <summary>
/// Gets or sets the width of the floating native window
/// </summary>
public int Width
{
get { return this._size.Width; }
set
{
this._size = new Size(value, this._size.Height);
}
}
/// <summary>
/// Get or set x-coordinate of top-left corner of floating native window in screen coordinates
/// </summary>
public int X
{
get { return this._location.X; }
set
{
this.Location = new Point(value, this.Location.Y);
}
}
/// <summary>
/// Get or set y-coordinate of top-left corner of floating native window in screen coordinates
/// </summary>
public int Y
{
get { return this._location.Y; }
set
{
this.Location = new Point(this.Location.X, value);
}
}
/// <summary>
/// Get rectangle represented client area of floating native window in client coordinates(top-left corner always has coord. 0,0)
/// </summary>
public Rectangle Bound
{
get
{
return new Rectangle(new Point(0, 0), this._size);
}
}
/// <summary>
/// Get or set full opacity(255) or full transparency(0) or any intermediate state for floating native window transparency
/// </summary>
public byte Alpha
{
get { return this._alpha; }
set
{
if (this._alpha == value) return;
this._alpha = value;
this.UpdateLayeredWindow();
}
}
#endregion
#region IDisposable Members
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!this._disposed)
{
this.DestroyHandle();
this._disposed = true;
}
}
#endregion
}
#region # Win32 #
[StructLayout(LayoutKind.Sequential)]
internal struct POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
internal struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
internal struct SIZE
{
public int cx;
public int cy;
}
[StructLayout(LayoutKind.Sequential)]
internal struct TRACKMOUSEEVENTS
{
public uint cbSize;
public uint dwFlags;
public IntPtr hWnd;
public uint dwHoverTime;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MSG
{
public IntPtr hwnd;
public int message;
public IntPtr wParam;
public IntPtr lParam;
public int time;
public int pt_x;
public int pt_y;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct BLENDFUNCTION
{
public byte BlendOp;
public byte BlendFlags;
public byte SourceConstantAlpha;
public byte AlphaFormat;
}
internal class User32
{
public const uint WS_POPUP = 0x80000000;
public const int WS_EX_TOPMOST = 0x8;
public const int WS_EX_TOOLWINDOW = 0x80;
public const int WS_EX_LAYERED = 0x80000;
public const int WS_EX_TRANSPARENT = 0x20;
public const int WS_EX_NOACTIVATE = 0x08000000;
public const int SW_SHOWNOACTIVATE = 4;
public const int SW_HIDE = 0;
public const uint AW_HOR_POSITIVE = 0x1;
public const uint AW_HOR_NEGATIVE = 0x2;
public const uint AW_VER_POSITIVE = 0x4;
public const uint AW_VER_NEGATIVE = 0x8;
public const uint AW_CENTER = 0x10;
public const uint AW_HIDE = 0x10000;
public const uint AW_ACTIVATE = 0x20000;
public const uint AW_SLIDE = 0x40000;
public const uint AW_BLEND = 0x80000;
// Methods
private User32()
{
}
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool AnimateWindow(IntPtr hWnd, uint dwTime, uint dwFlags);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool ClientToScreen(IntPtr hWnd, ref POINT pt);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool DispatchMessage(ref MSG msg);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool DrawFocusRect(IntPtr hWnd, ref RECT rect);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr GetFocus();
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern ushort GetKeyState(int virtKey);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool GetMessage(ref MSG msg, int hWnd, uint wFilterMin, uint wFilterMax);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool GetClientRect(IntPtr hWnd, [In, Out] ref RECT rect);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr GetWindow(IntPtr hWnd, int cmd);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool HideCaret(IntPtr hWnd);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool InvalidateRect(IntPtr hWnd, ref RECT rect, bool erase);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr LoadCursor(IntPtr hInstance, uint cursor);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int MapWindowPoints(IntPtr hWndFrom, IntPtr hWndTo, [In, Out] ref RECT rect, int cPoints);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool PeekMessage(ref MSG msg, int hWnd, uint wFilterMin, uint wFilterMax, uint wFlag);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool PostMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool ReleaseCapture();
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool ScreenToClient(IntPtr hWnd, ref POINT pt);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern uint SendMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr SetCursor(IntPtr hCursor);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr SetFocus(IntPtr hWnd);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern int SetWindowLong(IntPtr hWnd, int nIndex, int newLong);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndAfter, int X, int Y, int Width, int Height, uint flags);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool redraw);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool ShowCaret(IntPtr hWnd);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool SetCapture(IntPtr hWnd);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern int ShowWindow(IntPtr hWnd, short cmdShow);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool SystemParametersInfo(uint uiAction, uint uiParam, ref int bRetValue, uint fWinINI);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool TrackMouseEvent(ref TRACKMOUSEEVENTS tme);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool TranslateMessage(ref MSG msg);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, IntPtr hdcSrc, ref POINT pprSrc, int crKey, ref BLENDFUNCTION pblend, int dwFlags);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool UpdateWindow(IntPtr hwnd);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool WaitMessage();
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool AdjustWindowRectEx(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle);
}
internal class Gdi32
{
// Methods
private Gdi32()
{
}
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
internal static extern int CombineRgn(IntPtr dest, IntPtr src1, IntPtr src2, int flags);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr CreateBrushIndirect(ref LOGBRUSH brush);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr CreateRectRgnIndirect(ref RECT rect);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
internal static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
internal static extern int GetClipBox(IntPtr hDC, ref RECT rectBox);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
internal static extern bool PatBlt(IntPtr hDC, int x, int y, int width, int height, uint flags);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
internal static extern int SelectClipRgn(IntPtr hDC, IntPtr hRgn);
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
}
[StructLayout(LayoutKind.Sequential)]
public struct LOGBRUSH
{
public uint lbStyle;
public uint lbColor;
public uint lbHatch;
}
#endregion
}