|
|
|
|
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:

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); } } }
- Website design & XNA + DirectX code : Riemer Grootjans - ©2003 - 2011 Riemer Grootjans
|
|
|
|
|