XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   
My XNA Book
      
       Go to section on this site

Additional info


Latest Forum posts

 Tutorial 3 for Windows Phone 7
  Posted by: Anonymous
  When: 20/05/2013 at 02:30:13

 No download link for 2d series: shooter
  Posted by: zaboleq
  When: 07/05/2013 at 15:46:28

 Collision Class?
  Posted by: Anonymous
  When: 05/05/2013 at 19:03:59

 stack overflow
  Posted by: cityguy
  When: 07/04/2013 at 01:58:38

 Meshes looks strange.
  Posted by: ab_saratov
  When: 01/04/2013 at 04:31:08

 Lamppost Not loaded
  Posted by: Anonymous
  When: 22/03/2013 at 06:43:52

 Collision Class?
  Posted by: Da_Boom
  When: 21/03/2013 at 01:23:09

 Math boggles me
  Posted by: cityguy
  When: 17/03/2013 at 03:44:48

 Collision Class?
  Posted by: Da_Boom
  When: 16/03/2013 at 03:44:42

 Tree update
  Posted by: Anonymous
  When: 15/03/2013 at 21:11:22


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 - 2011 Riemer Grootjans
  • Translations

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

    Microsoft MVP Award



    2007 - 2011 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 4.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!