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

 Account settings
  Posted by: Anonymous
  When: 07/05/2014 at 09:48:39

 forced subtitle
  Posted by: Applefly
  When: 07/05/2014 at 06:00:48

 convert DVD into PMS
  Posted by: Applefly
  When: 07/05/2014 at 05:55:25

 DVD to Digital Copy easily
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:52:29

 DVD on Xbox 360/Xbox One Console
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:51:47

 Extract .Srt Subtitles
  Posted by: Applefly
  When: 04/05/2014 at 03:54:38

 Encode Movie collection
  Posted by: Applefly
  When: 04/05/2014 at 03:52:41

 Convert DVD to WMV
  Posted by: Applefly
  When: 29/04/2014 at 05:53:50

 rip DVDs into digital files
  Posted by: Applefly
  When: 29/04/2014 at 05:51:20

 iTunes movies/music to Kindle Fire
  Posted by: ciciyu80
  When: 29/04/2014 at 05:10:20


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!