SpriteFontControl.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // SpriteFontControl.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Content;
  12. using Microsoft.Xna.Framework.Graphics;
  13. #endregion
  14. namespace WinFormsGraphicsDevice
  15. {
  16. /// <summary>
  17. /// Example control inherits from GraphicsDeviceControl, which allows it to
  18. /// render using a GraphicsDevice. This control shows how to use ContentManager
  19. /// inside a WinForms application. It loads a SpriteFont object through the
  20. /// ContentManager, then uses a SpriteBatch to draw text. The control is not
  21. /// animated, so it only redraws itself in response to WinForms paint messages.
  22. /// </summary>
  23. class SpriteFontControl : GraphicsDeviceControl
  24. {
  25. ContentManager content;
  26. SpriteBatch spriteBatch;
  27. SpriteFont font;
  28. /// <summary>
  29. /// Initializes the control, creating the ContentManager
  30. /// and using it to load a SpriteFont.
  31. /// </summary>
  32. protected override void Initialize()
  33. {
  34. content = new ContentManager(Services, "Content");
  35. spriteBatch = new SpriteBatch(GraphicsDevice);
  36. font = content.Load<SpriteFont>("hudFont");
  37. }
  38. /// <summary>
  39. /// Disposes the control, unloading the ContentManager.
  40. /// </summary>
  41. protected override void Dispose(bool disposing)
  42. {
  43. if (disposing)
  44. {
  45. content.Unload();
  46. }
  47. base.Dispose(disposing);
  48. }
  49. /// <summary>
  50. /// Draws the control, using SpriteBatch and SpriteFont.
  51. /// </summary>
  52. protected override void Draw()
  53. {
  54. const string message = "Hello, World!\n" +
  55. "\n" +
  56. "I'm an XNA Framework GraphicsDevice,\n" +
  57. "running inside a WinForms application.\n" +
  58. "\n" +
  59. "This text is drawn using SpriteBatch,\n" +
  60. "with a SpriteFont that was loaded\n" +
  61. "through the ContentManager.\n" +
  62. "\n" +
  63. "The pane to my right contains a\n" +
  64. "spinning 3D triangle.";
  65. GraphicsDevice.Clear(Color.CornflowerBlue);
  66. spriteBatch.Begin();
  67. spriteBatch.DrawString(font, message, new Vector2(23, 23), Color.White);
  68. spriteBatch.End();
  69. }
  70. }
  71. }