/// <summary> /// Measure the width that a set of text will take for a given font /// </summary> /// <param name="text"></param> /// <param name="font"></param> /// <returns></returns> public int MeasureString(string text, Font font) { if (text == null || text.Trim() == "") return 0; int size = 0; for (int i = 0; i < text.Length; i++) { size += font.CharWidth(text[i]); } return size; }
private static int alignRight(string text, Font textFont) { int increaseLabelWidth = 0; char[] increaseCharArray = _incrementLabel.ToCharArray(0, _incrementLabel.Length); for (int letterNo = 0; letterNo < increaseCharArray.Length; letterNo++) { increaseLabelWidth += textFont.CharWidth(increaseCharArray[letterNo]); } return DISPLAY_WIDTH - increaseLabelWidth - DISPLAY_MARGIN; }
public Calculator() { font = Resources.GetFont(Resources.FontResources.NinaB); ButtonHelper.Current.OnButtonPress += Current_OnButtonPress; BackgroundColor = Color.White; ForegroundColor = Color.Black; ButtonHeight = font.Height; ButtonWidth = font.CharWidth('8')*2; //each button has a height of 15 //we want to align from the bottom up int leftPadding = 7; Buttons = new ArrayList(); int bottom = Program.AgentSize - ButtonHeight - ButtonBorder*2; int left = leftPadding; int width = ButtonWidth + ButtonBorder; char[] map = new char[] { '0', '.', 'x', '/', '=', '1', '2', '3', '-', '=', '4', '5', '6', '+', '=', '7', '8', '9', 'C', '=' }; for (int i = 0; i <= map.Length - 1; i++) { var newButton = new CalculatorButton() { Point = new Point(left, bottom), Text = map[i].ToString() }; Buttons.Add(newButton); if (SelectedButton == null) { SelectedButton = newButton; newButton.Selected = true; } if (i == 4 || i == 9 || i == 14 || i == 19) { bottom = bottom - ButtonHeight - ButtonBorder*2; left = leftPadding; } else { left = left + (ButtonWidth + ButtonBorder + 4); } } }
static public void DrawString(Font font, String text, int x, int y, UInt32 text_color, UInt32 back_color) { int width = 0; for (int i = 0; i < text.Length; i++) { width+=font.CharWidth((char)(text[i])); } Bitmap bmp = new Bitmap(width, font.Height); bmp.DrawRectangle((Microsoft.SPOT.Presentation.Media.Color)back_color, 100, 0, 0, width, font.Height, 0, 0, (Microsoft.SPOT.Presentation.Media.Color)back_color, 0, 0, (Microsoft.SPOT.Presentation.Media.Color)back_color, 0, 0, 0); bmp.DrawText(text, font, (Microsoft.SPOT.Presentation.Media.Color)text_color, 0, 0); Image imge = new Image(bmp); // Note: Maybe out of memory here DrawImage(imge, x, y); bmp.Dispose(); bmp = null; imge.Dispose(); imge = null; }
/// <summary> /// Draws a string to the display. /// </summary> /// <param name="text">The text to write.</param> /// <param name="x">The x coordinate where to draw.</param> /// <param name="y">The y coordinate where to draw.</param> /// <param name="font">The font to use.</param> /// <param name="foreColor">The color of the text.</param> /// <param name="backColor">The background color of the text.</param> public static void Draw(string text, int x, int y, Font font, Color foreColor, Color backColor) { int width = 0; for (int i = 0; i < text.Length; i++) { width += font.CharWidth((char)(text[i])); } Bitmap bmp = new Bitmap(width, font.Height); bmp.DrawRectangle(backColor, 100, 0, 0, width, font.Height, 0, 0, backColor, 0, 0, backColor, 0, 0, 0); bmp.DrawText(text, font, foreColor, 0, 0); Display.Draw(new TinyBitmap(bmp), x, y); }
/// <summary> /// Constructs a TextScrollViewer using the specified text, font, and color. /// </summary> /// <param name="text"></param> /// <param name="font"></param> /// <param name="color"></param> public TextScrollViewer(string text, Font font, Color color) { // Create the ScrollViewer object. _viewer = new ScrollViewer(); // Create the ScrollText object using the parameters passed into the // constructor and then set other important member values. ScrollText = new ScrollerText(text, font, color); ScrollText.HorizontalAlignment = HorizontalAlignment.Left; ScrollText.VerticalAlignment = VerticalAlignment.Top; // Set the child of the viewer to be the ScrollText object. this._viewer.Child = ScrollText; // Hard code a line with and height based on the character 'A'. _viewer.LineWidth = font.CharWidth('A'); _viewer.LineHeight = font.Height; // Set the child of our class to be the ScrollViewer object. this.Children.Add(_viewer); }