48 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP && SILICONSTUDIO_PARADOX_GRAPHICS_API_DIRECT3D
49 using System.Runtime.InteropServices;
51 using System.ComponentModel;
53 using System.Windows.Forms;
55 namespace SiliconStudio.
Paradox.Games
60 public class GameForm :
Form
62 private const int SIZE_RESTORED = 0;
63 private const int SIZE_MINIMIZED = 1;
64 private const int SIZE_MAXIMIZED = 2;
65 private const int SIZE_MAXSHOW = 3;
66 private const int SIZE_MAXHIDE = 4;
68 private const uint PBT_APMRESUMESUSPEND = 7;
69 private const uint PBT_APMQUERYSUSPEND = 0;
70 private const int SC_MONITORPOWER = 0xF170;
71 private const int SC_SCREENSAVE = 0xF140;
72 private const int MNC_CLOSE = 1;
73 private System.Drawing.Size cachedSize;
74 private FormWindowState previousWindowState;
76 private bool isUserResizing;
77 private bool isBackgroundFirstDraw;
78 private bool isSizeChangedWithoutResizeBegin;
80 private bool isActive;
86 : this(
"Paradox Game")
94 public GameForm(String text)
97 BackColor = System.Drawing.Color.Black;
98 ClientSize =
new System.Drawing.Size(800, 600);
101 SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint,
true);
103 Icon = Resources.GameResources.Logo;
105 previousWindowState = FormWindowState.Normal;
111 public event EventHandler<EventArgs> AppActivated;
116 public event EventHandler<EventArgs> AppDeactivated;
121 public event EventHandler<EventArgs> MonitorChanged;
126 public event EventHandler<EventArgs> PauseRendering;
131 public event EventHandler<EventArgs> ResumeRendering;
136 public event EventHandler<CancelEventArgs> Screensaver;
141 public event EventHandler<EventArgs> SystemResume;
146 public event EventHandler<EventArgs> SystemSuspend;
151 public event EventHandler<EventArgs> UserResized;
157 public bool IsProcessingKeys {
get; set; }
159 internal bool IsFullScreen {
get; set; }
165 protected override void OnResizeBegin(
EventArgs e)
167 isUserResizing =
true;
169 base.OnResizeBegin(e);
178 protected override void OnResizeEnd(
EventArgs e)
182 if (isUserResizing && cachedSize != Size)
188 isUserResizing =
false;
189 OnResumeRendering(e);
196 protected override void OnLoad(
EventArgs e)
206 protected override void OnPaintBackground(PaintEventArgs e)
208 if (!isBackgroundFirstDraw)
210 base.OnPaintBackground(e);
211 isBackgroundFirstDraw =
true;
219 private void OnPauseRendering(
EventArgs e)
221 if (PauseRendering != null)
222 PauseRendering(
this, e);
229 private void OnResumeRendering(
EventArgs e)
231 if (ResumeRendering != null)
232 ResumeRendering(
this, e);
241 if (UserResized != null)
242 UserResized(
this, e);
245 private void OnMonitorChanged(
EventArgs e)
247 if (MonitorChanged != null)
248 MonitorChanged(
this, e);
257 if (AppActivated != null)
258 AppActivated(
this, e);
265 private void OnAppDeactivated(
EventArgs e)
267 if (AppDeactivated != null)
268 AppDeactivated(
this, e);
275 private void OnSystemSuspend(
EventArgs e)
277 if (SystemSuspend != null)
278 SystemSuspend(
this, e);
287 if (SystemResume != null)
288 SystemResume(
this, e);
295 private void OnScreensaver(CancelEventArgs e)
297 if (Screensaver != null)
298 Screensaver(
this, e);
301 protected override void OnClientSizeChanged(
EventArgs e)
303 base.OnClientSizeChanged(e);
304 if (!isUserResizing && (isSizeChangedWithoutResizeBegin || cachedSize != Size))
306 isSizeChangedWithoutResizeBegin =
false;
317 protected override void WndProc(ref Message m)
319 long wparam = m.WParam.ToInt64();
323 case Win32Native.WM_SIZE:
324 if (wparam == SIZE_MINIMIZED)
326 previousWindowState = FormWindowState.Minimized;
333 GetClientRect(m.HWnd, out rect);
334 if (rect.Bottom - rect.Top == 0)
341 else if (wparam == SIZE_MAXIMIZED)
343 if (previousWindowState == FormWindowState.Minimized)
346 previousWindowState = FormWindowState.Maximized;
352 else if (wparam == SIZE_RESTORED)
354 if (previousWindowState == FormWindowState.Minimized)
357 if (!isUserResizing && (Size != cachedSize || previousWindowState == FormWindowState.Maximized))
359 previousWindowState = FormWindowState.Normal;
362 if (cachedSize != Size.Empty)
364 isSizeChangedWithoutResizeBegin =
true;
368 previousWindowState = FormWindowState.Normal;
372 case Win32Native.WM_ACTIVATEAPP:
384 case Win32Native.WM_POWERBROADCAST:
385 if (wparam == PBT_APMQUERYSUSPEND)
388 m.Result =
new IntPtr(1);
391 else if (wparam == PBT_APMRESUMESUSPEND)
394 m.Result =
new IntPtr(1);
398 case Win32Native.WM_MENUCHAR:
401 case Win32Native.WM_SYSCOMMAND:
403 if (wparam == SC_MONITORPOWER || wparam == SC_SCREENSAVE)
405 var e =
new CancelEventArgs();
409 m.Result = IntPtr.Zero;
418 protected override void OnKeyUp(KeyEventArgs e)
420 if (IsSystemKeyToFilter(e))
422 e.SuppressKeyPress =
true;
430 protected override void OnKeyDown(KeyEventArgs e)
432 if (IsSystemKeyToFilter(e))
434 e.SuppressKeyPress =
true;
442 private static bool IsSystemKeyToFilter(KeyEventArgs e)
445 return ((e.KeyValue == 0x12 || e.KeyValue == 0x79 || e.Alt) && !(e.Alt && e.KeyValue == 0x73));
448 [DllImport(
"user32.dll", EntryPoint =
"GetClientRect")]
449 private static extern bool GetClientRect(IntPtr hWnd, out
Rectangle lpRect);
System.Windows.Shapes.Rectangle Rectangle