Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GameForm.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 //
4 // Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 // -----------------------------------------------------------------------------
24 // Original code from SlimDX project.
25 // Greetings to SlimDX Group. Original code published with the following license:
26 // -----------------------------------------------------------------------------
27 /*
28 * Copyright (c) 2007-2011 SlimDX Group
29 *
30 * Permission is hereby granted, free of charge, to any person obtaining a copy
31 * of this software and associated documentation files (the "Software"), to deal
32 * in the Software without restriction, including without limitation the rights
33 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
34 * copies of the Software, and to permit persons to whom the Software is
35 * furnished to do so, subject to the following conditions:
36 *
37 * The above copyright notice and this permission notice shall be included in
38 * all copies or substantial portions of the Software.
39 *
40 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
41 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
43 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
44 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
45 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
46 * THE SOFTWARE.
47 */
48 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP && SILICONSTUDIO_PARADOX_GRAPHICS_API_DIRECT3D
49 using System.Runtime.InteropServices;
50 using System;
51 using System.ComponentModel;
52 using System.Drawing;
53 using System.Windows.Forms;
54 
55 namespace SiliconStudio.Paradox.Games
56 {
57  /// <summary>
58  /// Default Rendering Form on windows desktop.
59  /// </summary>
60  public class GameForm : Form
61  {
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;
67 
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;
75  //private DisplayMonitor monitor;
76  private bool isUserResizing;
77  private bool isBackgroundFirstDraw;
78  private bool isSizeChangedWithoutResizeBegin;
79 
80  private bool isActive;
81 
82  /// <summary>
83  /// Initializes a new instance of the <see cref="GameForm"/> class.
84  /// </summary>
85  public GameForm()
86  : this("Paradox Game")
87  {
88  }
89 
90  /// <summary>
91  /// Initializes a new instance of the <see cref="GameForm"/> class.
92  /// </summary>
93  /// <param name="text">The text.</param>
94  public GameForm(String text)
95  {
96  Text = text;
97  BackColor = System.Drawing.Color.Black;
98  ClientSize = new System.Drawing.Size(800, 600);
99 
100  ResizeRedraw = true;
101  SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
102 
103  Icon = Resources.GameResources.Logo;
104 
105  previousWindowState = FormWindowState.Normal;
106  }
107 
108  /// <summary>
109  /// Occurs when [app activated].
110  /// </summary>
111  public event EventHandler<EventArgs> AppActivated;
112 
113  /// <summary>
114  /// Occurs when [app deactivated].
115  /// </summary>
116  public event EventHandler<EventArgs> AppDeactivated;
117 
118  /// <summary>
119  /// Occurs when [monitor changed].
120  /// </summary>
121  public event EventHandler<EventArgs> MonitorChanged;
122 
123  /// <summary>
124  /// Occurs when [pause rendering].
125  /// </summary>
126  public event EventHandler<EventArgs> PauseRendering;
127 
128  /// <summary>
129  /// Occurs when [resume rendering].
130  /// </summary>
131  public event EventHandler<EventArgs> ResumeRendering;
132 
133  /// <summary>
134  /// Occurs when [screensaver].
135  /// </summary>
136  public event EventHandler<CancelEventArgs> Screensaver;
137 
138  /// <summary>
139  /// Occurs when [system resume].
140  /// </summary>
141  public event EventHandler<EventArgs> SystemResume;
142 
143  /// <summary>
144  /// Occurs when [system suspend].
145  /// </summary>
146  public event EventHandler<EventArgs> SystemSuspend;
147 
148  /// <summary>
149  /// Occurs when [user resized].
150  /// </summary>
151  public event EventHandler<EventArgs> UserResized;
152 
153  /// <summary>
154  /// Gets or sets a value indicating whether this instance is processing keys. By default is <c>false</c>
155  /// </summary>
156  /// <value><c>true</c> if this instance is processing keys; otherwise, <c>false</c>.</value>
157  public bool IsProcessingKeys { get; set; }
158 
159  internal bool IsFullScreen { get; set; }
160 
161  /// <summary>
162  /// Raises the <see cref="E:System.Windows.Forms.Form.ResizeBegin"/> event.
163  /// </summary>
164  /// <param name="e">A <see cref="T:System.EventArgs"/> that contains the event data.</param>
165  protected override void OnResizeBegin(EventArgs e)
166  {
167  isUserResizing = true;
168 
169  base.OnResizeBegin(e);
170  cachedSize = Size;
171  OnPauseRendering(e);
172  }
173 
174  /// <summary>
175  /// Raises the <see cref="E:System.Windows.Forms.Form.ResizeEnd"/> event.
176  /// </summary>
177  /// <param name="e">A <see cref="T:System.EventArgs"/> that contains the event data.</param>
178  protected override void OnResizeEnd(EventArgs e)
179  {
180  base.OnResizeEnd(e);
181 
182  if (isUserResizing && cachedSize != Size)
183  {
184  OnUserResized(e);
185  // UpdateScreen();
186  }
187 
188  isUserResizing = false;
189  OnResumeRendering(e);
190  }
191 
192  /// <summary>
193  /// Raises the <see cref="E:System.Windows.Forms.Form.Load"/> event.
194  /// </summary>
195  /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param>
196  protected override void OnLoad(EventArgs e)
197  {
198  base.OnLoad(e);
199  // UpdateScreen();
200  }
201 
202  /// <summary>
203  /// Paints the background of the control.
204  /// </summary>
205  /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs"/> that contains the event data.</param>
206  protected override void OnPaintBackground(PaintEventArgs e)
207  {
208  if (!isBackgroundFirstDraw)
209  {
210  base.OnPaintBackground(e);
211  isBackgroundFirstDraw = true;
212  }
213  }
214 
215  /// <summary>
216  /// Raises the Pause Rendering event.
217  /// </summary>
218  /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
219  private void OnPauseRendering(EventArgs e)
220  {
221  if (PauseRendering != null)
222  PauseRendering(this, e);
223  }
224 
225  /// <summary>
226  /// Raises the Resume Rendering event.
227  /// </summary>
228  /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
229  private void OnResumeRendering(EventArgs e)
230  {
231  if (ResumeRendering != null)
232  ResumeRendering(this, e);
233  }
234 
235  /// <summary>
236  /// Raises the User resized event.
237  /// </summary>
238  /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
239  private void OnUserResized(EventArgs e)
240  {
241  if (UserResized != null)
242  UserResized(this, e);
243  }
244 
245  private void OnMonitorChanged(EventArgs e)
246  {
247  if (MonitorChanged != null)
248  MonitorChanged(this, e);
249  }
250 
251  /// <summary>
252  /// Raises the On App Activated event.
253  /// </summary>
254  /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
255  private void OnAppActivated(EventArgs e)
256  {
257  if (AppActivated != null)
258  AppActivated(this, e);
259  }
260 
261  /// <summary>
262  /// Raises the App Deactivated event
263  /// </summary>
264  /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
265  private void OnAppDeactivated(EventArgs e)
266  {
267  if (AppDeactivated != null)
268  AppDeactivated(this, e);
269  }
270 
271  /// <summary>
272  /// Raises the System Suspend event
273  /// </summary>
274  /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
275  private void OnSystemSuspend(EventArgs e)
276  {
277  if (SystemSuspend != null)
278  SystemSuspend(this, e);
279  }
280 
281  /// <summary>
282  /// Raises the System Resume event
283  /// </summary>
284  /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
285  private void OnSystemResume(EventArgs e)
286  {
287  if (SystemResume != null)
288  SystemResume(this, e);
289  }
290 
291  /// <summary>
292  /// Raises the <see cref="E:Screensaver"/> event.
293  /// </summary>
294  /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
295  private void OnScreensaver(CancelEventArgs e)
296  {
297  if (Screensaver != null)
298  Screensaver(this, e);
299  }
300 
301  protected override void OnClientSizeChanged(EventArgs e)
302  {
303  base.OnClientSizeChanged(e);
304  if (!isUserResizing && (isSizeChangedWithoutResizeBegin || cachedSize != Size))
305  {
306  isSizeChangedWithoutResizeBegin = false;
307  cachedSize = Size;
308  OnUserResized(EventArgs.Empty);
309  //UpdateScreen();
310  }
311  }
312 
313  /// <summary>
314  /// Override windows message loop handling.
315  /// </summary>
316  /// <param name="m">The Windows <see cref="T:System.Windows.Forms.Message"/> to process.</param>
317  protected override void WndProc(ref Message m)
318  {
319  long wparam = m.WParam.ToInt64();
320 
321  switch (m.Msg)
322  {
323  case Win32Native.WM_SIZE:
324  if (wparam == SIZE_MINIMIZED)
325  {
326  previousWindowState = FormWindowState.Minimized;
327  OnPauseRendering(EventArgs.Empty);
328  }
329  else
330  {
331  Rectangle rect;
332 
333  GetClientRect(m.HWnd, out rect);
334  if (rect.Bottom - rect.Top == 0)
335  {
336  // Rapidly clicking the task bar to minimize and restore a window
337  // can cause a WM_SIZE message with SIZE_RESTORED when
338  // the window has actually become minimized due to rapid change
339  // so just ignore this message
340  }
341  else if (wparam == SIZE_MAXIMIZED)
342  {
343  if (previousWindowState == FormWindowState.Minimized)
344  OnResumeRendering(EventArgs.Empty);
345 
346  previousWindowState = FormWindowState.Maximized;
347 
348  OnUserResized(EventArgs.Empty);
349  //UpdateScreen();
350  cachedSize = Size;
351  }
352  else if (wparam == SIZE_RESTORED)
353  {
354  if (previousWindowState == FormWindowState.Minimized)
355  OnResumeRendering(EventArgs.Empty);
356 
357  if (!isUserResizing && (Size != cachedSize || previousWindowState == FormWindowState.Maximized))
358  {
359  previousWindowState = FormWindowState.Normal;
360 
361  // Only update when cachedSize is != 0
362  if (cachedSize != Size.Empty)
363  {
364  isSizeChangedWithoutResizeBegin = true;
365  }
366  }
367 
368  previousWindowState = FormWindowState.Normal;
369  }
370  }
371  break;
372  case Win32Native.WM_ACTIVATEAPP:
373  if (wparam != 0)
374  {
375  OnAppActivated(EventArgs.Empty);
376  isActive = true;
377  }
378  else
379  {
380  OnAppDeactivated(EventArgs.Empty);
381  isActive = false;
382  }
383  break;
384  case Win32Native.WM_POWERBROADCAST:
385  if (wparam == PBT_APMQUERYSUSPEND)
386  {
387  OnSystemSuspend(EventArgs.Empty);
388  m.Result = new IntPtr(1);
389  return;
390  }
391  else if (wparam == PBT_APMRESUMESUSPEND)
392  {
393  OnSystemResume(EventArgs.Empty);
394  m.Result = new IntPtr(1);
395  return;
396  }
397  break;
398  case Win32Native.WM_MENUCHAR:
399  //m.Result = new IntPtr(MNC_CLOSE << 16); // IntPtr(MAKELRESULT(0, MNC_CLOSE));
400  return;
401  case Win32Native.WM_SYSCOMMAND:
402  wparam &= 0xFFF0;
403  if (wparam == SC_MONITORPOWER || wparam == SC_SCREENSAVE)
404  {
405  var e = new CancelEventArgs();
406  OnScreensaver(e);
407  if (e.Cancel)
408  {
409  m.Result = IntPtr.Zero;
410  return;
411  }
412  }
413  break;
414  }
415  base.WndProc(ref m);
416  }
417 
418  protected override void OnKeyUp(KeyEventArgs e)
419  {
420  if (IsSystemKeyToFilter(e))
421  {
422  e.SuppressKeyPress = true;
423  e.Handled = true;
424  return;
425  }
426 
427  base.OnKeyUp(e);
428  }
429 
430  protected override void OnKeyDown(KeyEventArgs e)
431  {
432  if (IsSystemKeyToFilter(e))
433  {
434  e.SuppressKeyPress = true;
435  e.Handled = true;
436  return;
437  }
438 
439  base.OnKeyDown(e);
440  }
441 
442  private static bool IsSystemKeyToFilter(KeyEventArgs e)
443  {
444  // Supress ALT and F10 keys from being processed
445  return ((e.KeyValue == 0x12 || e.KeyValue == 0x79 || e.Alt) && !(e.Alt && e.KeyValue == 0x73));
446  }
447 
448  [DllImport("user32.dll", EntryPoint = "GetClientRect")]
449  private static extern bool GetClientRect(IntPtr hWnd, out Rectangle lpRect);
450  }
451 }
452 #endif
System.Windows.Shapes.Rectangle Rectangle
Definition: ColorPicker.cs:16