XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   July 19: Series4 - 2 Pass Renderstates
My Book: Out Now!
      
       Go to section on this site

Additional info


Latest Forum posts

 WinForm not found
  Posted by: riemer
  When: 20/07/2008 at 14:30:03

 Bounding Boxes for Maya Data
  Posted by: riemer
  When: 20/07/2008 at 14:27:31

 QuadTree
  Posted by: riemer
  When: 20/07/2008 at 14:23:48

 Bounding Boxes for Maya Data
  Posted by: vijay
  When: 20/07/2008 at 12:25:40

 Error on compiling
  Posted by: libia
  When: 20/07/2008 at 02:36:16

 Terrain From RAW
  Posted by: reiko
  When: 19/07/2008 at 20:36:39

 WinForm not found
  Posted by: kapil.rajak
  When: 19/07/2008 at 16:24:52

 human skin colors
  Posted by: aguacate
  When: 19/07/2008 at 13:34:55

 Bounding Boxes for Maya Data
  Posted by: Archenon
  When: 19/07/2008 at 09:19:16

 Volunteer to write VB.net tutorial
  Posted by: riemer
  When: 19/07/2008 at 07:00:19


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 - 2008 MVP Award
    DirectX - XNA

    Contents

    News
    Home
    Forum
    XNA 2.0 Recipes Book (8)
    Downloads
    Extra Reading (3)
    Matrices: geometrical
    Matrix Mathematics
    Homogenous matrices
    Tutorials (136)
    XNA 2.0 using C# (65)
    Series 1: Terrain (13)
    Series 2:Flightsim (14)
    Series 3: HLSL (18)
    Series 4: Advanced terrain (17)
    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!