www.pudn.com > sudoku.rar > ResourceHelper.cs


//-------------------------------------------------------------------------- 
//  
//  Copyright (c) Microsoft Corporation.  All rights reserved.  
//  
//  File: ResourceHelper.cs 
// 
//  Description: Strongly-typed access to resources. 
//  
//-------------------------------------------------------------------------- 
 
using System; 
using System.IO; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.Drawing.Drawing2D; 
using System.Resources; 
using System.Reflection; 
using System.Diagnostics; 
 
namespace Microsoft.Sudoku 
{ 
	/// Provides strongly-typed resource access to the rest of the application. 
	internal sealed class ResourceHelper 
	{ 
		/// Prevent external instantiation. 
		private ResourceHelper(){} 
 
		/// The name of the resource file containing the string resources for this application. 
		private const string _stringsResourceFileName = "Microsoft.Sudoku.Strings"; 
		/// The resource manager used to load all resources. 
		private static ResourceManager _manager; 
		/// This assembly. 
		private static Assembly _resourceAssembly; 
 
		/// Image displayed as the shadow for a standard button. 
		private static Bitmap _buttonShadow; 
		/// Image displayed when a button is in the checked state. 
		private static Bitmap _buttonCheckedImage; 
		/// Image displayed when a button is in the unchecked state. 
		private static Bitmap _buttonUncheckedImage; 
		/// Image displayed when a button is in the highlighted state. 
		private static Bitmap _buttonHighlightedImage; 
 
		/// Icon for the pen button. 
		private static Bitmap _penImage; 
		/// Icon for the scratchpad button. 
		private static Bitmap _scratchpadImage; 
		/// Icon for the eraser button. 
		private static Bitmap _eraserImage; 
		/// Icon for the new puzzle button. 
		private static Bitmap _newPuzzleImage; 
		/// Icon for the options button. 
		private static Bitmap _optionsImage; 
		/// Icon for the undo button. 
		private static Bitmap _undoImage; 
 
		/// The board image containing each cell. 
		private static Bitmap _boardImage; 
		/// The background image for the whole app. 
		private static Bitmap _backgroundImage; 
		/// The backdrop image for the board. 
		private static Bitmap _boardBackgroundImage; 
 
		/// The unchecked state image for the exit button. 
		private static Bitmap _exitButtonUncheckedImage; 
		/// The checked state image for the exit button. 
		private static Bitmap _exitButtonCheckedImage; 
		/// The highlighted state image for the exit button. 
		private static Bitmap _exitButtonHighlightedImage; 
		/// The shadow image drawn below the exit button. 
		private static Bitmap _exitButtonShadowImage; 
 
		/// The unchecked state image for the help button. 
		private static Bitmap _helpButtonUncheckedImage; 
		/// The checked state image for the help button. 
		private static Bitmap _helpButtonCheckedImage; 
		/// The highlighted state image for the help button. 
		private static Bitmap _helpButtonHighlightedImage; 
		/// The shadow image drawn below the help button. 
		private static Bitmap _helpButtonShadowImage; 
 
		/// Active cell image for the upper-left corner cell. 
		private static Bitmap _cellActiveUpperLeft; 
		/// Active cell image for the upper-right corner cell. 
		private static Bitmap _cellActiveUpperRight; 
		/// Active cell image for the lower-left corner cell. 
		private static Bitmap _cellActiveLowerLeft; 
		/// Active cell image for the lower-right corner cell. 
		private static Bitmap _cellActiveLowerRight; 
		/// Active cell image for any square cell. 
		private static Bitmap _cellActiveSquare; 
 
		/// Hint cell image for the upper-left corner cell. 
		private static Bitmap _cellHintUpperLeft; 
		/// Hint cell image for the upper-right corner cell. 
		private static Bitmap _cellHintUpperRight; 
		/// Hint cell image for the lower-left corner cell. 
		private static Bitmap _cellHintLowerLeft; 
		/// Hint cell image for the lower-right corner cell. 
		private static Bitmap _cellHintLowerRight; 
		/// Hint cell image for any square cell. 
		private static Bitmap _cellHintSquare; 
 
		/// Image used as the background for the new puzzle "dialog". 
		private static Bitmap _newPuzzleBackground; 
		/// The checked state image for a button on the new puzzle screen. 
		private static Bitmap _newPuzzleItemChecked; 
		/// The unchecked state image for a button on the new puzzle screen. 
		private static Bitmap _newPuzzleItemUnchecked; 
		/// The highlighted state image for a button on the new puzzle screen. 
		private static Bitmap _newPuzzleItemHighlighted; 
 
		/// The string used as the display title for the application. 
		private static string _displayTitle; 
		/// The string used as the display title for the application when a difficulty level has been set. 
		private static string _displayTitleWithDifficultyLevel; 
		/// Easy difficulty level description. 
		private static string _easyDifficultyLevel; 
		/// Medium difficulty level description. 
		private static string _mediumDifficultyLevel; 
		/// Hard difficulty level description. 
		private static string _hardDifficultyLevel; 
		/// The string used to size the width of a single character. 
		private static string _fontSizingString; 
		/// The string used to inform the user that they don't have enough CAS permissions to run the application. 
		private static string _lacksMinimumPermissionsToRun; 
		/// The string displayed to the user when they solve the puzzle. 
		private static string _puzzleSolvedCongratulations; 
		/// The name of the help file. 
		private static string _helpFileName; 
		/// The string displayed when the help file can't be found. 
		private static string _helpFileNotFound; 
		/// The string displayed to a user who attempts to use the app on a non-Tablet device. 
		private static string _notRunningOnTablet; 
		/// The string displayed to a user whose attempt to load a game file fails. 
		private static string _shutdownOnError; 
		/// The string displayed to a user when they're about to lose a puzzle. 
		private static string _aboutToLosePuzzle; 
 
		/// Initializes all resources. 
		static ResourceHelper() 
		{ 
			// Create the resource manager 
			_resourceAssembly = typeof(ResourceHelper).Assembly; 
			_manager = new ResourceManager(_stringsResourceFileName, _resourceAssembly); 
 
			// Retrieve the string resources 
			_displayTitle = _manager.GetString("DisplayTitle"); 
			_displayTitleWithDifficultyLevel = _manager.GetString("DisplayTitleWithDifficultyLevel"); 
			_easyDifficultyLevel = _manager.GetString("EasyDifficultyLevel"); 
			_mediumDifficultyLevel = _manager.GetString("MediumDifficultyLevel"); 
			_hardDifficultyLevel = _manager.GetString("HardDifficultyLevel"); 
			_lacksMinimumPermissionsToRun = _manager.GetString("LacksMinimumPermissionsToRun"); 
			_fontSizingString = _manager.GetString("FontSizingString"); 
			_puzzleSolvedCongratulations = _manager.GetString("PuzzleSolvedCongratulations"); 
			_helpFileName = _manager.GetString("HelpFileName"); 
			_helpFileNotFound = _manager.GetString("HelpFileNotFound"); 
			_notRunningOnTablet = _manager.GetString("NotRunningOnTablet"); 
			_shutdownOnError = _manager.GetString("ShutdownOnError"); 
			_aboutToLosePuzzle = _manager.GetString("AboutToLosePuzzle"); 
 
			// Load the image resources that are compiled into the assembly manifest 
			_backgroundImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.Background.png"); 
			_boardBackgroundImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.BoardBackground.png"); 
			_boardImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.Board.png"); 
 
			// Get the active and hint cell images 
			_cellActiveSquare = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.CellActiveSquare.png"); 
			_cellActiveUpperLeft = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.CellActiveUpperLeft.png"); 
			_cellActiveUpperRight = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.CellActiveUpperRight.png"); 
			_cellActiveLowerLeft = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.CellActiveLowerLeft.png"); 
			_cellActiveLowerRight = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.CellActiveLowerRight.png"); 
			_cellHintSquare = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.CellHintSquare.png"); 
			_cellHintUpperLeft = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.CellHintUpperLeft.png"); 
			_cellHintUpperRight = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.CellHintUpperRight.png"); 
			_cellHintLowerLeft = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.CellHintLowerLeft.png"); 
			_cellHintLowerRight = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.CellHintLowerRight.png"); 
 
			// Get the form button images  
			_buttonShadow = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.ButtonShadow.png"); 
			_buttonCheckedImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.ButtonChecked.png"); 
			_buttonUncheckedImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.ButtonUnchecked.png"); 
			_buttonHighlightedImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.ButtonHighlighted.png"); 
			_penImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.Pen.png"); 
			_eraserImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.Eraser.png"); 
			_optionsImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.Options.png"); 
			_undoImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.Undo.png"); 
			_newPuzzleImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.New.png"); 
			_scratchpadImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.Scratchpad.png"); 
 
			// Get special button images 
			_exitButtonCheckedImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.ExitButtonChecked.png"); 
			_exitButtonUncheckedImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.ExitButtonUnchecked.png"); 
			_exitButtonHighlightedImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.ExitButtonHighlighted.png"); 
			_exitButtonShadowImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.ExitButtonShadow.png"); 
			_helpButtonCheckedImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.HelpButtonChecked.png"); 
			_helpButtonUncheckedImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.HelpButtonUnchecked.png"); 
			_helpButtonHighlightedImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.HelpButtonHighlighted.png"); 
			_helpButtonShadowImage = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.HelpButtonShadow.png"); 
 
			// Get new puzzle dialog images 
			_newPuzzleBackground = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.NewPuzzleBackground.png"); 
			_newPuzzleItemChecked = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.NewPuzzleItemChecked.png"); 
			_newPuzzleItemUnchecked = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.NewPuzzleItemUnchecked.png"); 
			_newPuzzleItemHighlighted = GetResourceImageWithAlphaBlending("Microsoft.Sudoku.Images.NewPuzzleItemHighlighted.png"); 
		} 
 
		/// Gets the board image containing each cell. 
		public static Bitmap BoardImage { get { return _boardImage; } } 
		/// Gets the background image for the whole app. 
		public static Bitmap BoardBackgroundImage { get { return _boardBackgroundImage; } } 
		/// Gets the backdrop image for the board. 
		public static Bitmap BackgroundImage { get { return _backgroundImage; } } 
 
		/// Gets the image displayed as the shadow for a standard button. 
		public static Bitmap ButtonShadow { get { return _buttonShadow; } } 
		/// Gets the image displayed when a button is in the checked state. 
		public static Bitmap ButtonCheckedImage { get { return _buttonCheckedImage; } } 
		/// Gets the image displayed when a button is in the unchecked state. 
		public static Bitmap ButtonUncheckedImage { get { return _buttonUncheckedImage; } } 
		/// Gets the image displayed when a button is in the highlighted state. 
		public static Bitmap ButtonHighlightedImage { get { return _buttonHighlightedImage; } } 
 
		/// Gets the active cell image for any square cell. 
		public static Bitmap CellActiveSquare { get { return _cellActiveSquare; } } 
		/// Gets the active cell image for the upper-left corner cell. 
		public static Bitmap CellActiveUpperLeft { get { return _cellActiveUpperLeft; } } 
		/// Gets the active cell image for the upper-right corner cell. 
		public static Bitmap CellActiveUpperRight { get { return _cellActiveUpperRight; } } 
		/// Gets the active cell image for the lower-left corner cell. 
		public static Bitmap CellActiveLowerLeft { get { return _cellActiveLowerLeft; } } 
		/// Gets the active cell image for the lower-right corner cell. 
		public static Bitmap CellActiveLowerRight { get { return _cellActiveLowerRight; } } 
		/// Gets the hint cell image for any square cell. 
		public static Bitmap CellHintSquare { get { return _cellHintSquare; } } 
		/// Gets the hint cell image for the upper-left corner cell. 
		public static Bitmap CellHintUpperLeft { get { return _cellHintUpperLeft; } } 
		/// Gets the hint cell image for the upper-right corner cell. 
		public static Bitmap CellHintUpperRight { get { return _cellHintUpperRight; } } 
		/// Gets the hint cell image for the lower-left corner cell. 
		public static Bitmap CellHintLowerLeft { get { return _cellHintLowerLeft; } } 
		/// Gets the hint cell image for the lower-right corner cell. 
		public static Bitmap CellHintLowerRight { get { return _cellHintLowerRight; } } 
		 
		/// Gets the icon for the pen button. 
		public static Bitmap PenImage { get { return _penImage; } } 
		/// Gets the icon for the eraser button. 
		public static Bitmap EraserImage { get { return _eraserImage; } } 
		/// Gets the icon for the options button. 
		public static Bitmap OptionsImage { get { return _optionsImage; } } 
		/// Gets the icon for the undo button. 
		public static Bitmap UndoImage { get { return _undoImage; } } 
		/// Gets the icon for the scratchpad button. 
		public static Bitmap ScratchpadImage { get { return _scratchpadImage; } } 
		/// Gets the icon for the new game button. 
		public static Bitmap NewImage { get { return _newPuzzleImage; } } 
 
		/// The unchecked state image for the exit button. 
		public static Bitmap ExitButtonCheckedImage { get { return _exitButtonCheckedImage; } } 
		/// The highlighted state image for the exit button. 
		public static Bitmap ExitButtonHighlightedImage { get { return _exitButtonHighlightedImage; } } 
		/// The unchecked state image for the exit button. 
		public static Bitmap ExitButtonUncheckedImage { get { return _exitButtonUncheckedImage; } } 
		/// The shadow image for the exit button. 
		public static Bitmap ExitButtonShadowImage { get { return _exitButtonShadowImage; } } 
 
		/// The unchecked state image for the help button. 
		public static Bitmap HelpButtonCheckedImage { get { return _helpButtonCheckedImage; } } 
		/// The highlighted state image for the help button. 
		public static Bitmap HelpButtonHighlightedImage { get { return _helpButtonHighlightedImage; } } 
		/// The unchecked state image for the help button. 
		public static Bitmap HelpButtonUncheckedImage { get { return _helpButtonUncheckedImage; } } 
		/// The shadow image for the help button. 
		public static Bitmap HelpButtonShadowImage { get { return _helpButtonShadowImage; } } 
 
		/// Gets the image used as the background for the new puzzle "dialog". 
		public static Bitmap NewPuzzleBackground { get { return _newPuzzleBackground; } } 
		/// Gets the checked state image for a button on the new puzzle screen. 
		public static Bitmap NewPuzzleItemChecked { get { return _newPuzzleItemChecked; } } 
		/// Gets the unchecked state image for a button on the new puzzle screen. 
		public static Bitmap NewPuzzleItemUnchecked { get { return _newPuzzleItemUnchecked; } } 
		/// Gets the highlighted state image for a button on the new puzzle screen. 
		public static Bitmap NewPuzzleItemHighlighted { get { return _newPuzzleItemHighlighted; } } 
 
		/// Gets the string used as the display title for the application. 
		public static string DisplayTitle { get { return _displayTitle; } } 
		/// Gets the string used as the display title for the application when a difficulty level is set. 
		public static string DisplayTitleWithDifficultyLevel { get { return _displayTitleWithDifficultyLevel; } } 
		/// Easy difficulty level description. 
		public static string EasyDifficultyLevel { get { return _easyDifficultyLevel; } } 
		/// Medium difficulty level description. 
		public static string MediumDifficultyLevel { get { return _mediumDifficultyLevel; } } 
		/// Hard difficulty level description. 
		public static string HardDifficultyLevel { get { return _hardDifficultyLevel; } } 
		/// Gets the string used to size the width of a single character. 
		public static string FontSizingString { get { return _fontSizingString; } } 
		/// Gets the string used to inform the user that they don't have enough CAS permissions to run. 
		public static string LacksMinimumPermissionsToRun { get { return _lacksMinimumPermissionsToRun; } } 
		/// Gets the string displayed to the user when they solve the puzzle. 
		public static string PuzzleSolvedCongratulations { get { return _puzzleSolvedCongratulations; } } 
		/// Gets the name of the help file. 
		public static string HelpFileName { get { return _helpFileName; } } 
		/// Gets the string displayed when the help file can't be found. 
		public static string HelpFileNotFound { get { return _helpFileNotFound; } } 
		/// Gets the string displayed to a user who attempts to use the app on a non-Tablet device. 
		public static string NotRunningOnTablet { get { return _notRunningOnTablet; } } 
		/// Gets the string displayed to a user whose attempt to load a game file fails. 
		public static string ShutdownOnError { get { return _shutdownOnError; } } 
		/// Gets the string displayed to a user when they're about to lose a puzzle. 
		public static string AboutToLosePuzzle { get { return _aboutToLosePuzzle; } } 
 
		/// Gets an image from assembly manifest, adding premultiplied alpha. 
		/// The name of the resource. 
		/// The image. 
		internal static Bitmap GetResourceImageWithAlphaBlending(string resourceName) 
		{ 
			using(Bitmap bmp = GetResourceImage(resourceName)) 
			{ 
				return PremultiplyAlpha(bmp); 
			} 
		} 
 
		/// Gets an image from assembly manifest. 
		/// The name of the resource. 
		/// The image. 
		internal static Bitmap GetResourceImage(string resourceName) 
		{ 
			return new Bitmap(_resourceAssembly.GetManifestResourceStream(resourceName));  
		} 
 
		/// Creates a new bitmap with a premultiplied alpha layer. 
		/// The Bitmap. 
		/// The new Bitmap. 
		internal static Bitmap PremultiplyAlpha(Bitmap bmp) 
		{ 
			int width = bmp.Width, height = bmp.Height; 
			Bitmap renderBmp = new Bitmap(width, height, PixelFormat.Format32bppPArgb); 
			using(Graphics g = Graphics.FromImage(renderBmp)) 
			{ 
				g.DrawImage(bmp, 0, 0, width, height); 
			} 
			return renderBmp; 
		} 
	} 
}