forked from flamewave000/discord-overlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectXHost.cs
332 lines (299 loc) · 11.2 KB
/
DirectXHost.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
using System;
using System.Windows.Forms;
using SharpDX.Windows;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Threading.Tasks;
namespace DirectXHost
{
public class DirectXHost : IDisposable
{
[DllImport("user32")]
private static extern int PrintWindow(IntPtr hwnd, IntPtr hdcBlt, UInt32 nFlags);
private Form _overlayForm;
private PictureBox _overlayTarget;
private RenderForm _dxForm;
private GraphicsD3D11 _graphics;
private bool UserResized { get; set; }
private Size ClientSize { get; set; }
public bool IsDisposed { get; private set; }
#region Events
public event EventHandler HostWindowClosed;
public event EventHandler HostWindowCreated;
protected void OnHostWindowCreated()
{
if (HostWindowCreated != null)
{
HostWindowCreated(this._dxForm, new EventArgs());
}
}
protected void OnHostWindowClosed()
{
if (HostWindowClosed != null)
{
HostWindowClosed(this._dxForm, new EventArgs());
}
}
#endregion
public void Exit()
{
throw new NotImplementedException();
}
public async Task Run()
{
try
{
await Settings.Load();
InitializeRenderForm();
_graphics = new GraphicsD3D11();
_graphics.Initialise(_dxForm, true);
_dxForm.UserResized += (sender, args) =>
{
var renderForm = sender as RenderForm;
ClientSize = new Size(renderForm.ClientSize.Width, renderForm.ClientSize.Height);
UserResized = true;
};
_overlayForm.Show();
RenderLoop.Run(_dxForm, RenderCallback);
}
catch (ArgumentException ex)
{
MessageBox.Show(string.Format("Exception running DirectX host window.\r\n{0}", ex.Message), "DirectX Host", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Exception running DirectX host window.\r\n{0}", ex.Message), "DirectX Host", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// Set the bitmap object to the size of the screen
private Bitmap _bmpScreenshot;
Bitmap bmpScreenshot
{
get => _bmpScreenshot;
set
{
_bmpScreenshot = value;
_overlayTarget.Image = value;
updateImageBounds();
}
}
Graphics gfxScreenshot;
private void RenderCallback()
{
Update();
Draw();
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
IntPtr dc = gfxScreenshot.GetHdc();
PrintWindow(_dxForm.Handle, dc, 0);
gfxScreenshot.ReleaseHdc();
gfxScreenshot.Dispose();
_overlayTarget.Invalidate();
}
class OverlayForm : Form
{
public OverlayForm() : base()
{
DoubleBuffered = true;
}
}
bool _drag = false;
Point _wstart, _mstart;
private void updateImageBounds()
{
_overlayTarget.Size = _dxForm.Size;
int BorderWidth = (_dxForm.Width - _dxForm.ClientSize.Width) / 2;
int TitlebarHeight = (_dxForm.Height - _dxForm.ClientSize.Height) - BorderWidth;
_overlayTarget.Location = new Point(-BorderWidth, -TitlebarHeight);
_overlayTarget.Width = _dxForm.Width - BorderWidth;
_overlayTarget.Height = _dxForm.Height - BorderWidth;
_overlayTarget.Invalidate();
}
private void InitializeRenderForm()
{
_dxForm = new RenderForm(Constants.WindowTitle);
_dxForm.HandleCreated += WindowHandleCreated;
_dxForm.HandleDestroyed += WindowHandleDestroyed;
_dxForm.MinimumSize = new Size(Constants.StartWidth, Constants.StartHeight);
_dxForm.ClientSize = Settings.savePositions ? Settings.containerRect.Size : _dxForm.MinimumSize;
_dxForm.StartPosition = Settings.savePositions ? FormStartPosition.Manual : FormStartPosition.CenterScreen;
if (Settings.savePositions) _dxForm.Location = Settings.containerRect.Point;
_dxForm.FormBorderStyle = FormBorderStyle.SizableToolWindow;
_dxForm.GotFocus += (s, e) => ShouldShowOverlayFrame(true);
_dxForm.LostFocus += (s, e) => ShouldShowOverlayFrame(false);
_dxForm.TopMost = false;
_dxForm.HelpRequested += _dxForm_HelpRequested;
_dxForm.Menu = GetMenu();
_dxForm.UserResized += (sender, args) => { Settings.containerRect.Size = _dxForm.ClientSize; Settings.Save(); };
_dxForm.LocationChanged += (sender, args) => { Settings.containerRect.Point = _dxForm.Location; Settings.Save(); };
_overlayForm = new OverlayForm();
_overlayForm.Text = _overlayForm.Name = "Discord Overlay";
_overlayForm.MinimumSize = new Size(100, 50);
_overlayForm.ClientSize = Settings.savePositions ? Settings.overlayRect.Size : new Size(Constants.OverlayStartWidth, Constants.OverlayStartHeight);
_overlayForm.StartPosition = Settings.savePositions ? FormStartPosition.Manual : FormStartPosition.CenterScreen;
if (Settings.savePositions) _overlayForm.Location = Settings.overlayRect.Point;
_overlayForm.BackColor = Settings.transparencyKey;
_overlayForm.TransparencyKey = Settings.transparencyKey;
_overlayForm.TopMost = true;
_overlayForm.ShowIcon = false;
_overlayForm.MinimizeBox = true;
_overlayForm.MaximizeBox = true;
_overlayForm.BackgroundImageLayout = ImageLayout.None;
_overlayForm.FormBorderStyle = FormBorderStyle.Sizable;
_overlayForm.FormClosing += (s, e) => _dxForm.Close();
_overlayForm.GotFocus += (s, e) => ShouldShowOverlayFrame(true);
_overlayForm.LostFocus += (s, e) => ShouldShowOverlayFrame(false);
_overlayTarget = new PictureBox();
_overlayTarget.SizeMode = PictureBoxSizeMode.Normal;
_overlayTarget.BackColor = Color.Red;
_overlayTarget.Anchor = AnchorStyles.Top | AnchorStyles.Left;
_overlayForm.Controls.Add(_overlayTarget);
_overlayForm.ResizeEnd += (sender, args) => { Settings.overlayRect.Size = _overlayForm.ClientSize; Settings.Save(); };
_overlayForm.LocationChanged += (sender, args) => { Settings.overlayRect.Point = _overlayForm.Location; Settings.Save(); };
// Set the bitmap object to the size of the screen
bmpScreenshot = new Bitmap(_dxForm.Width, _dxForm.Height, PixelFormat.Format32bppArgb);
_dxForm.MouseDown += (s, e) =>
{
_drag = e.Button == MouseButtons.Left;
_mstart = _dxForm.PointToScreen(e.Location);
_wstart = _dxForm.Location;
};
_dxForm.MouseUp += (s, e) => _drag = e.Button == MouseButtons.Left ? false : _drag;
_dxForm.MouseMove += (s, e) =>
{
if (!_drag) return;
var newPoint = e.Location;
var delta = new Point(newPoint.X - _mstart.X, newPoint.Y - _mstart.Y);
_dxForm.Location = _dxForm.PointToScreen(new Point(_wstart.X + delta.X, _wstart.Y + delta.Y));
};
_overlayForm.MouseDown += (s, e) =>
{
_drag = e.Button == MouseButtons.Left;
_mstart = _overlayForm.PointToScreen(e.Location);
_wstart = _overlayForm.Location;
};
_overlayForm.MouseUp += (s, e) => _drag = e.Button == MouseButtons.Left ? false : _drag;
_overlayForm.MouseMove += (s, e) =>
{
if (!_drag) return;
var newPoint = e.Location;
var delta = new Point(newPoint.X - _mstart.X, newPoint.Y - _mstart.Y);
_overlayForm.Location = _overlayForm.PointToScreen(new Point(_wstart.X + delta.X, _wstart.Y + delta.Y));
};
}
private void _dxForm_HelpRequested(object sender, EventArgs eventArgs)
{
MessageBox.Show(_dxForm, @"Overlay Clickable: Enables/Disables ability to interact with the Overlay Window.
Save Window Positions: Saves the Overlay and Container sizes and screen positions.
Transparency Colour: The colour used as a transparency key for hiding the background. Windows does not support an Alpha channel, so it has to use a defined colour to control clipping.
If you have issues with the window positions/sizes, delete the 'props.bin' file that is generated in the program's folder. This will reset the program settings.
", "Help", MessageBoxButtons.OK, MessageBoxIcon.Question);
}
private int ColorToBgr(Color color) => (color.B << 16) | (color.G << 8) | color.R;
private MainMenu GetMenu() => new MainMenu(new MenuItem[]
{
new MenuItem("?", _dxForm_HelpRequested),
new MenuItem($"{(Settings.overlayClickable ? '☑' : '☐')} Overlay Clickable", (s,e) => {
Settings.overlayClickable = !Settings.overlayClickable;
_dxForm.Menu = GetMenu();
ShouldShowOverlayFrame(Settings.overlayClickable);
Settings.Save();
}),
new MenuItem($"{(Settings.savePositions ? '☑' : '☐')} Save Window Positions", (s,e) => {
Settings.savePositions = !Settings.savePositions;
_dxForm.Menu = GetMenu();
Settings.Save();
}),
new MenuItem("Transparency Colour", (s,e) =>
{
var dialog = new ColorDialog();
dialog.Color = Settings.transparencyKey;
dialog.SolidColorOnly = true;
dialog.AnyColor = true;
dialog.FullOpen = true;
dialog.CustomColors = new int[] { ColorToBgr(Constants.DefaultTransparencyKey), ColorToBgr(Settings.transparencyKey) };
if(dialog.ShowDialog(_dxForm) == DialogResult.OK)
{
Settings.transparencyKey = dialog.Color;
Settings.Save();
ShouldShowOverlayFrame(true);
}
}),
new MenuItem(" Version 2.0")
});
private void ShouldShowOverlayFrame(bool show)
{
if (show && Settings.overlayClickable)
{
_overlayForm.AllowTransparency = false;
_overlayForm.FormBorderStyle = FormBorderStyle.Sizable;
}
else
{
try
{
_overlayForm.AllowTransparency = true;
_overlayForm.FormBorderStyle = FormBorderStyle.None;
_overlayForm.BackColor = Settings.transparencyKey;
_overlayForm.TransparencyKey = Settings.transparencyKey;
}
catch (Exception) { }
}
}
private void WindowHandleDestroyed(object sender, EventArgs e)
{
Dispose();
IsDisposed = true;
OnHostWindowClosed();
}
private void WindowHandleCreated(object sender, EventArgs e)
{
OnHostWindowCreated();
}
public void Update()
{
if (!UserResized) return;
_graphics.ResizeGraphics(ClientSize.Width, ClientSize.Height);
UserResized = false;
bmpScreenshot?.Dispose();
if (ClientSize.Width <= 0 || ClientSize.Height <= 0) return;
//// Set the bitmap object to the size of the screen
bmpScreenshot = new Bitmap(_dxForm.Width, _dxForm.Height, PixelFormat.Format32bppArgb);
}
public void Draw()
{
_graphics.ClearRenderTargetView();
_graphics.PresentSwapChain();
}
#region IDisposable Support
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
_dxForm.Dispose();
}
_graphics.Dispose();
IsDisposed = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
~DirectXHost()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(false);
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
GC.SuppressFinalize(this);
}
#endregion
}
}