1.字体的初始化类:
1 using Microsoft.Xna.Framework; 2 using Microsoft.Xna.Framework.Graphics; 3 using System; 4 using System.Linq; 5 6 namespace xiaolang 7 { 8 public class Font 9 { 10 XNADeviceService graphicsDeviceService; 11 12 ServiceContainer services = new ServiceContainer(); 13 14 GraphicsDevice graphicsDevice; 15 16 SpriteBatch spriteBatch; 17 18 SpriteFontX spriteFontX; 19 20 public Font(IntPtr mainHandle, GraphicsDevice GraphicsDevice) 21 { 22 graphicsDeviceService = XNADeviceService.AddRef(mainHandle, 1, 1); 23 services.AddService<IGraphicsDeviceService>(graphicsDeviceService); 24 25 graphicsDevice = GraphicsDevice; 26 27 //设定字体,样式,大小 28 spriteBatch = new SpriteBatch(graphicsDevice); 29 spriteFontX = new SpriteFontX(new System.Drawing.Font("宋体", 12f), 30 this.graphicsDeviceService, System.Drawing.Text.TextRenderingHint.SingleBitPerPixel); 31 } 32 public void DrawFont(Vector2 center_V2 , string str_font) 33 { 34 //字体 35 graphicsDevice.DepthStencilState = DepthStencilState.None; 36 37 spriteBatch.Begin(); 38 39 spriteBatch.DrawStringX(spriteFontX, str_font, center_V2, Color.Red); 40 41 spriteBatch.End(); 42 43 graphicsDevice.DepthStencilState = DepthStencilState.Default; 44 } 45 } 46 }
2.初始化类的简单调用:
1 using Microsoft.Xna.Framework; 2 using Microsoft.Xna.Framework.Graphics; 3 using System; 4 5 namespace xiaolang 6 { 7 public class WriteFont 8 { 9 public Font font; 10 11 public bool B_Thread = true; 12 13 public WriteFont() 14 { 15 font= new Font(this.Handle, GraphicsDevice); 16 17 Thread TFont= new Thread(new ThreadStart(Thread_Font)); 18 TFont.Start(); 19 } 20 private void Thread_Font() 21 { 22 while (B_Thread) 23 { 24 font.DrawLable(new Vector2(200,200),"好人一生平安... ..."); 25 Thread.Sleep(50); 26 } 27 } 28 } 29 }
3.字体资源:
链接:https://pan.baidu.com/s/1rgonhi0VZ95LAl4CcarkAg
提取码:2ot1