XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   2D Series finished!
My Book: Out Now!
      
       Go to section on this site

Additional info


Latest Forum posts

 Smooth Terrain Heights
  Posted by: Anonymous
  When: 14/03/2010 at 06:28:12

 Collision performance problems
  Posted by: evlhamburger
  When: 13/03/2010 at 18:21:30

 Cannot compile FX file
  Posted by: Anonymous
  When: 13/03/2010 at 17:56:56

 Smooth Terrain Heights
  Posted by: Anonymous
  When: 13/03/2010 at 15:42:48

 Collision performance problems
  Posted by: Dastrus
  When: 13/03/2010 at 11:31:04

 Mirror used as security cam...
  Posted by: Archenon
  When: 13/03/2010 at 05:10:04

 Smooth Terrain Heights
  Posted by: Anonymous
  When: 13/03/2010 at 04:57:33

 Mirror used as security cam...
  Posted by: Archenon
  When: 13/03/2010 at 04:36:45

 Smooth Terrain Heights
  Posted by: Anonymous
  When: 13/03/2010 at 01:16:28

 typo!
  Posted by: Anonymous
  When: 12/03/2010 at 17:23:29


Ads

Displaying a MessageBox in XNA

For XBOX 360 compatibility reasons, XNA’s Game Studio Express doesn’t allow us to display a simple MessageBox. However, sometimes this MessageBox can come really useful. Of course, several ways can be found to display a MessageBox using the default Windows API. I will show you a very easy way to display a MessageBox, but don’t expect this to work on the XBOX, as this of course doesn’t run with the Windows API.

-- Don't mind my ramblings on this page too much. The forum thread found at the bottom of this page contains a much cleaner approach. --







We simply need 1 line that links to the Windows API dll, and another one that specifies the function in this dll that we want to import into our project. Add these lines to the very top of your Game1 class (if you don’t know where that is, the full code of the Game1.cs file is always presented at the bottom of each chapter):

 [DllImport("user32.dll", CharSet = CharSet.Auto)]
 public static extern uint MessageBox(IntPtr hWnd, String text, String caption, uint type);

That’s all there is to it! Now we can use the MessageBox anywhere in our program. For example, let’s put this line at the end of the Initialize() method:

 MessageBox(new IntPtr(0), "MesssageBox Text", "MessageBox title", 0);

The first argument can be a valid pointer to a window. Next we specify the text and caption of the MessageBox, and the type MessageBox. This last argument indicates what kind of buttons we want to be available in the MessageBox.

Running this code, you should see this at startup:



Which is nice, but what if we would like to show a number in the messagebox? Easy, we can use this line:

 float number = MathHelper.Pi/10;
 MessageBox(new IntPtr(0), string.Format("Result: {0}", number) , "MessageBox title", 0);

Which gives this result:



For multiple numbers in one line, this becomes:

 float number1 = MathHelper.Pi/10;
 float number2 = 10.0f / 3.0f;
 int number3 = 512;
 MessageBox(new IntPtr(0), string.Format("Result1: {0}; Result2: {1}; Result3: {2}", number1, number2, number3), "MessageBox title", 0);

Which gives this result:




DirectX Tutorial 11 - MessageBox in XNA

If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !



Click here to go to the forum on this chapter!

Or click on one of the topics on this chapter to go there:
  • MessageBox
          A MessageBox can only be used when working in Wind...



    The complete code:

     using System;
     using System.Runtime.InteropServices;
     using System.Collections.Generic;
     using Microsoft.Xna.Framework;
     using Microsoft.Xna.Framework.Audio;
     using Microsoft.Xna.Framework.Content;
     using Microsoft.Xna.Framework.Graphics;
     using Microsoft.Xna.Framework.Input;
     using Microsoft.Xna.Framework.Storage;
     
     namespace XNAMessageBox
     {
         public class Game1 : Microsoft.Xna.Framework.Game
         {
             [DllImport("user32.dll", CharSet = CharSet.Auto)]
             public static extern uint MessageBox(IntPtr hWnd, String text, String caption, uint type);
     
             GraphicsDeviceManager graphics;
             ContentManager content;
             GraphicsDevice device;
     
             public Game1()
             {
                 graphics = new GraphicsDeviceManager(this);
                 content = new ContentManager(Services);            
             }
     
             protected override void Initialize()
             {
                 base.Initialize();
     
                 float number1 = MathHelper.Pi/10;
                 float number2 = 10.0f / 3.0f;
                 int number3 = 512;
                 MessageBox(new IntPtr(0), string.Format("Result1: {0}; Result2: {1}; Result3: {2}", number1, number2, number3), "MessageBox title", 0);
             }
     
             private void SetUpXNADevice()
             {
                 device = graphics.GraphicsDevice;
     
                 graphics.PreferredBackBufferWidth = 500;
                 graphics.PreferredBackBufferHeight = 500;
                 graphics.IsFullScreen = false;
                 graphics.ApplyChanges();
                 Window.Title = "Riemer's XNA Tutorials -- MessageBox";
             }
     
             protected override void LoadGraphicsContent(bool loadAllContent)
             {
                 if (loadAllContent)
                 {
                     SetUpXNADevice();
                 }
             }
     
             protected override void UnloadGraphicsContent(bool unloadAllContent)
             {
                 if (unloadAllContent == true)
                 {
                     content.Unload();
                 }
             }
     
             protected override void Update(GameTime gameTime)
             {
                 if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                     this.Exit();
                 base.Update(gameTime);
             }
             protected override void Draw(GameTime gameTime)
             {
                 device.Clear(Color.CornflowerBlue);
                 base.Draw(gameTime);
             }
         }
     }


    Google
     
    Webwww.riemers.net


    If you appreciate the amount of time I spend creating and updating
    these pages, feel free to donate -- any amount is welcome !



    - Website design & XNA + DirectX code : Riemer Grootjans -
    ©2003 - 2008 Riemer Grootjans
  • Translations

    This site in English
    This site in Korean
    This site in Czech

    Microsoft MVP Award



    2007 - 2009 MVP Award
    DirectX - XNA

    Contents

    News
    Home
    Forum
    XNA 2.0 Recipes Book (8)
    XNA 3.0 Recipes Book (8)
    Downloads
    Extra Reading (3)
    Matrices: geometrical
    Matrix Mathematics
    Homogenous matrices
    Community Projects (1)
    Tutorials (160)
    XNA 3.0 using C# (89)
    2D Series: Shooters! (22)
    3D Series 1: Terrain (13)
    3D Series 2: Flightsim (14)
    3D Series 3: HLSL (18)
    3D Series 4: Adv. terrain (19)
    Short Tuts (3)
    Run XNA on older pcs
    MessageBox in XNA
    Normal generation
    DirectX using C# (54)
    DirectX using C++ (15)
    DirectX using VB (2)
    -- Expand all --


    Thank you!

    Support this site --
    any amount is welcome !

    Stay up-to-date

    I don't have the time to keep a News section, so stay informed about the updates by clicking on this RSS file!