Click here to Skip to main content
15,892,674 members
Articles / Programming Languages / C#

CooksMate

Rate me:
Please Sign up or sign in to vote.
3.32/5 (8 votes)
21 Jan 2008GPL32 min read 56.7K   1K   23  
A simple program to help get the timing of a roast dinner
//  � Copyright 2002, Pierre ARNAUD, OPaC bright ideas, Switzerland.
//    All rights reserved.

namespace uk.org.aspellclark.common
{
	public class StringTools
	{
		private StringTools() { }
		
		static public System.Drawing.SizeF MeasureDisplayString(System.Drawing.Graphics graphics, string text, System.Drawing.Font font)
		{
			const int width = 32;
			
			System.Drawing.Bitmap   bitmap = new System.Drawing.Bitmap (width, 1, graphics);
			System.Drawing.SizeF    size   = graphics.MeasureString (text, font);
			System.Drawing.Graphics anagra = System.Drawing.Graphics.FromImage (bitmap);
			
			int measured_width = (int) size.Width;
			
			if (anagra != null) 
			{
				anagra.Clear (System.Drawing.Color.White);
				anagra.DrawString (text+"|", font, System.Drawing.Brushes.Black, width - measured_width, -font.Height / 2);
				
				for (int i = width-1; i >= 0; i--)
				{
					measured_width--;
					if (bitmap.GetPixel (i, 0).R == 0) 
					{
						break;
					}
				}
			}
			
			return new System.Drawing.SizeF (measured_width, size.Height);
		}
		
		static public int MeasureDisplayStringWidth(System.Drawing.Graphics graphics, string text, System.Drawing.Font font)
		{
			return (int) MeasureDisplayString (graphics, text, font).Width;
		}	
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior) Airbus Defense and Space
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions