Advertisement
CharcoStudios

Untitled

May 3rd, 2012
1,459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. program.cs:
  2.  
  3.   static class Program
  4.     {
  5.         [STAThread]
  6.         static void Main( string[] args )
  7.         {
  8.             Application.EnableVisualStyles( );
  9.             Application.SetCompatibleTextRenderingDefault( false );
  10.             XnaControlGame.CreateAndShow<MainDialog, VoxelEditorGame>( );
  11.         }
  12.     }
  13.  
  14. XnaControlGame.cs:
  15.  
  16.  public interface IXnaFormContainer
  17.     {
  18.         Control XnaControl { get; }
  19.         XnaControlGame Game { get; set; }
  20.     }
  21.  
  22.     public abstract class XnaControlGame : Microsoft.Xna.Framework.Game
  23.     {
  24.         public Control Parent { get; private set; }
  25.  
  26.         public static void CreateAndShow<T, Q>( )
  27.             where T : Form, IXnaFormContainer, new( )
  28.             where Q : XnaControlGame
  29.         {
  30.             using ( T form = new T( ) )
  31.             {
  32.                 form.Show( );
  33.  
  34.                 using ( Q game = ( Q ) Activator.CreateInstance( typeof( Q ), new object[] { form.XnaControl.Handle, form, form.XnaControl } ) )
  35.                 {
  36.                     form.Game = game;
  37.                     game.Parent = form.XnaControl;
  38.                     game.Run( );
  39.                 }
  40.             }
  41.         }
  42.  
  43.        
  44.         #region Private Vars to Build Embedded Xna Control
  45.  
  46.         IntPtr _XnaDrawingSurface;
  47.         GraphicsDeviceManager graphics;
  48.  
  49.         System.Windows.Forms.Form parentForm;
  50.         System.Windows.Forms.Control controlXna;
  51.  
  52.         System.Windows.Forms.Control gameForm;
  53.  
  54.         #endregion
  55.  
  56.         #region Constructor
  57.  
  58.         public XnaControlGame( IntPtr handle,
  59.             System.Windows.Forms.Form parentForm,
  60.             System.Windows.Forms.Control surfaceControl )
  61.         {
  62.             graphics = new GraphicsDeviceManager( this );
  63.             graphics.GraphicsProfile = GraphicsProfile.Reach;
  64.             Content.RootDirectory = "Content";
  65.  
  66.             this.parentForm = parentForm;
  67.             this.controlXna = surfaceControl;
  68.  
  69.             gameForm = System.Windows.Forms.Control.FromHandle( this.Window.Handle );
  70.             gameForm.VisibleChanged += new EventHandler( gameForm_VisibleChanged );
  71.             controlXna.SizeChanged += new EventHandler( pictureBox_SizeChanged );
  72.  
  73.             // preparing device settings handler.
  74.             _XnaDrawingSurface = handle;
  75.             Mouse.WindowHandle = handle;
  76.  
  77.             graphics.PreparingDeviceSettings += OnPreparingDeviceSettings;
  78.             graphics.PreferredBackBufferWidth = (controlXna.Width > 0) ? controlXna.Width : 50;
  79.             graphics.PreferredBackBufferHeight = (controlXna.Height > 0) ? controlXna.Height : 50;
  80.  
  81.             parentForm.FormClosed += delegate( object sender, System.Windows.Forms.FormClosedEventArgs e )
  82.             {
  83.                 this.Exit( );
  84.                 Application.Exit( );
  85.             };
  86.         }
  87.  
  88.         #endregion
  89.  
  90.         #region Events
  91.  
  92.         private void OnPreparingDeviceSettings( object sender, PreparingDeviceSettingsEventArgs e )
  93.         {
  94.             e.GraphicsDeviceInformation.PresentationParameters.DeviceWindowHandle = _XnaDrawingSurface;
  95.         }
  96.  
  97.         private void gameForm_VisibleChanged( object sender, EventArgs e )
  98.         {
  99.             if ( gameForm.Visible == true )
  100.                 gameForm.Visible = false;
  101.         }
  102.  
  103.         void pictureBox_SizeChanged( object sender, EventArgs e )
  104.         {
  105.             if ( parentForm.WindowState !=
  106.                 System.Windows.Forms.FormWindowState.Minimized )
  107.             {
  108.                 graphics.PreferredBackBufferWidth = controlXna.Width;
  109.                 graphics.PreferredBackBufferHeight = controlXna.Height;
  110.                 graphics.ApplyChanges( );
  111.                 OnSizeChanged( );
  112.             }
  113.         }
  114.  
  115.         protected virtual void OnSizeChanged( ) { }
  116.  
  117.         #endregion        
  118.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement