/// <summary> /// Draw the string to the back buffer. /// </summary> /// <param name="x">X destination of text</param> /// <param name="y">Y destination of text</param> /// <param name="text">Text to be displayed</param> /// <param name="color">Color of text</param> /// <param name="font">Font to be used</param> /// <param name="options">Font draw options</param> public void DrawText(int x, int y, string text, Color color, IFont font, FontDrawOptions options) { // make sure the function is passed an appropriate implementation GdiFont gdiFont = null; try { gdiFont = (GdiFont)font; } catch (InvalidCastException e) { throw new ApplicationException( "The font given was not created by" + "this class' CreateFont() method.", e); } float drawX = (float)x; float drawY = (float)y; if ((options & FontDrawOptions.DrawTextCenter) != 0) { SizeF sSize = gBack.MeasureString(text, gdiFont.Font); drawX -= sSize.Width / 2.0F; } else if ((options & FontDrawOptions.DrawTextRight) != 0) { SizeF sSize = gBack.MeasureString(text, gdiFont.Font); drawX -= sSize.Width; } Brush brush = new SolidBrush(color); gBack.DrawString(text, gdiFont.Font, brush, drawX, drawY); }
/// <summary> /// Draw the string to the back buffer. /// </summary> /// <param name="x">X destination of text</param> /// <param name="y">Y destination of text</param> /// <param name="text">Text to be displayed</param> /// <param name="color">Color of text</param> /// <param name="font">Font to be used</param> /// <param name="options">Font draw options</param> public void DrawText(int x, int y, string text, Color color, IFont font, FontDrawOptions options) { // make sure the function is passed an appropriate implementation DirectXFont dxFont = null; try { dxFont = (DirectXFont)font; } catch (InvalidCastException e) { throw new ApplicationException( "The font given was not created by" + "this class' CreateFont() method.", e); } // determine the rectangle to draw in Rectangle rect = new Rectangle(); #if DESKTOP dxFont.Font.DrawText(null, text, ref rect, DrawTextFormat.CalculateRect, color); #else dxFont.Font.MeasureString(null, text, ref rect, 0); #endif rect.Y = y; rect.X = 0; rect.Width = ScreenWidth; // set options for the DrawText call DrawTextFormat drawOptions; if (options == FontDrawOptions.DrawTextCenter) { drawOptions = DrawTextFormat.Center; } else if (options == FontDrawOptions.DrawTextLeft) { drawOptions = DrawTextFormat.Left; } else { drawOptions = DrawTextFormat.Right; } dxFont.Font.DrawText(null, text, rect, drawOptions, color); }