Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for fontselectbox.cs

(view source code of fontselectbox.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5.  
  6.  
  7. namespace RobvanderWoude
  8. {
  9. 	class FontSelectBox
  10. 	{
  11. 		static string progver = "1.01";
  12.  
  13.  
  14. 		static int Main( string[] args )
  15. 		{
  16. 			#region Initialize Variables
  17.  
  18. 			int rc = -1;
  19.  
  20. 			float initialfontsize = 12;
  21. 			int maxsize = 48;
  22. 			int minsize = 6;
  23.  
  24. 			string initialfontname = "Courier New";
  25. 			string returnproperty = "none";
  26. 			string showproperty = "all";
  27.  
  28. 			bool allowscriptchange = false;
  29. 			bool allowvectorfonts = false;
  30. 			bool fixedpitchonly = false;
  31. 			bool scriptsonly = false;
  32. 			bool showcolor = false;
  33. 			bool showeffects = false;
  34.  
  35. 			#endregion Initialize Variables
  36.  
  37.  
  38. 			#region Command Line Parsing
  39.  
  40. 			foreach ( string arg in args )
  41. 			{
  42. 				if ( arg.Length > 1 && arg[0] == '/' )
  43. 				{
  44. 					if ( arg.Contains( ":" ) )
  45. 					{
  46. 						string key = arg.ToUpper( ).Substring( 1, arg.IndexOf( ':' ) - 1 );
  47. 						string val = arg.Substring( arg.IndexOf( ':' ) + 1 );
  48. 						switch ( key )
  49. 						{
  50. 							case "I":
  51. 								if ( initialfontsize != 12 )
  52. 								{
  53. 									return ShowHelp( "Duplicate initial font size {0}", arg );
  54. 								}
  55. 								try
  56. 								{
  57. 									initialfontsize = Convert.ToSingle( val );
  58. 								}
  59. 								catch ( Exception )
  60. 								{
  61. 									return ShowHelp( "Invalid initial font size {0}", arg );
  62. 								}
  63. 								break;
  64. 							case "MAX":
  65. 								if ( maxsize != 48 )
  66. 								{
  67. 									return ShowHelp( "Duplicate maximum font size {0}", arg );
  68. 								}
  69. 								try
  70. 								{
  71. 									maxsize = Convert.ToInt32( val );
  72. 								}
  73. 								catch ( Exception )
  74. 								{
  75. 									return ShowHelp( "Invalid maximum font size {0}", arg );
  76. 								}
  77. 								break;
  78. 							case "MIN":
  79. 								if ( minsize != 6 )
  80. 								{
  81. 									return ShowHelp( "Duplicate minimum font size {0}", arg );
  82. 								}
  83. 								try
  84. 								{
  85. 									minsize = Convert.ToInt32( val );
  86. 									if ( minsize < 1 )
  87. 									{
  88. 										return ShowHelp( "Invalid minimum font size {0}", arg );
  89. 									}
  90. 								}
  91. 								catch ( Exception )
  92. 								{
  93. 									return ShowHelp( "Invalid minimum font size {0}", arg );
  94. 								}
  95. 								break;
  96. 							case "P":
  97. 								if ( showproperty != "all" )
  98. 								{
  99. 									return ShowHelp( "Duplicate command line switch /P" );
  100. 								}
  101. 								switch ( val.ToUpper( ) )
  102. 								{
  103. 									case "CHARSET":
  104. 									case "COLOR":
  105. 									case "NAME":
  106. 									case "RGB":
  107. 									case "SIZE":
  108. 									case "STYLE":
  109. 										showproperty = val.ToLower( );
  110. 										break;
  111. 									default:
  112. 										return ShowHelp( "Invalid command line argument {0}", arg );
  113. 								}
  114. 								break;
  115. 							case "R":
  116. 								if ( returnproperty != "none" )
  117. 								{
  118. 									return ShowHelp( "Duplicate command line switch /R" );
  119. 								}
  120. 								switch ( val.ToUpper( ) )
  121. 								{
  122. 									case "C":
  123. 									case "R":
  124. 									case "S":
  125. 									case "Y":
  126. 										returnproperty = val.ToLower( );
  127. 										break;
  128. 									default:
  129. 										return ShowHelp( "Invalid command line argument {0}", arg );
  130. 								}
  131. 								break;
  132. 							default:
  133. 								return ShowHelp( "Invalid command line argument {0}", arg );
  134. 						}
  135. 					}
  136. 					else if ( arg.Length == 2 )
  137. 					{
  138. 						switch ( arg.ToUpper( )[1] )
  139. 						{
  140. 							case '?':
  141. 								return ShowHelp( );
  142. 							case 'C':
  143. 								if ( showcolor )
  144. 								{
  145. 									return ShowHelp( "Duplicate command line switch /C" );
  146. 								}
  147. 								showcolor = true;
  148. 								break;
  149. 							case 'E':
  150. 								if ( showeffects )
  151. 								{
  152. 									return ShowHelp( "Duplicate command line switch /E" );
  153. 								}
  154. 								showeffects = true;
  155. 								break;
  156. 							case 'H':
  157. 								if ( scriptsonly )
  158. 								{
  159. 									return ShowHelp( "Duplicate command line switch /H" );
  160. 								}
  161. 								scriptsonly = true;
  162. 								break;
  163. 							case 'S':
  164. 								if ( allowscriptchange )
  165. 								{
  166. 									return ShowHelp( "Duplicate command line switch /S" );
  167. 								}
  168. 								allowscriptchange = true;
  169. 								break;
  170. 							case 'V':
  171. 								if ( allowvectorfonts )
  172. 								{
  173. 									return ShowHelp( "Duplicate command line switch /V" );
  174. 								}
  175. 								allowvectorfonts = true;
  176. 								break;
  177. 							case 'X':
  178. 								if ( fixedpitchonly )
  179. 								{
  180. 									return ShowHelp( "Duplicate command line switch /X" );
  181. 								}
  182. 								fixedpitchonly = true;
  183. 								break;
  184. 							default:
  185. 								return ShowHelp( "Invalid command line switch {0}", arg.ToUpper( ) );
  186. 						}
  187. 					}
  188. 					else
  189. 					{
  190. 						return ShowHelp( "Invalid command line argument {0}", arg );
  191. 					}
  192. 				}
  193. 				else
  194. 				{
  195. 					if ( initialfontname != "Courier New" )
  196. 					{
  197. 						return ShowHelp( "Duplicate font name argument" );
  198. 					}
  199. 					initialfontname = arg;
  200. 				}
  201. 			}
  202.  
  203. 			if ( maxsize < minsize )
  204. 			{
  205. 				return ShowHelp( "Maximum font size ({0}) must be greater than minimum font size ({1})", maxsize.ToString( ), minsize.ToString( ) );
  206. 			}
  207. 			if ( maxsize < initialfontsize || minsize > initialfontsize )
  208. 			{
  209. 				return ShowHelp( "Initial font size ({0}) must be in range {1}..{2}", initialfontsize.ToString( ), minsize.ToString( ), maxsize.ToString( ) );
  210. 			}
  211.  
  212. 			#endregion Command Line Parsing
  213.  
  214.  
  215. 			Font font = new Font( initialfontname, initialfontsize );
  216.  
  217. 			FontDialog fontdialog = new FontDialog
  218. 			{
  219. 				AllowScriptChange = allowscriptchange,
  220. 				AllowVectorFonts = allowvectorfonts,
  221. 				AllowVerticalFonts = false,
  222. 				FixedPitchOnly = fixedpitchonly,
  223. 				Font = font,
  224. 				FontMustExist = true,
  225. 				MaxSize = maxsize,
  226. 				MinSize = minsize,
  227. 				ScriptsOnly = scriptsonly,
  228. 				ShowApply = false,
  229. 				ShowColor = showcolor,
  230. 				ShowEffects = showeffects,
  231. 			};
  232.  
  233. 			if ( fontdialog.ShowDialog( ) == DialogResult.OK )
  234. 			{
  235. 				int charset = fontdialog.Font.GdiCharSet;
  236. 				int rgb = ( fontdialog.Color.R * 256 + fontdialog.Color.G ) * 256 + fontdialog.Color.B;
  237. 				int size = Convert.ToInt32( fontdialog.Font.Size );
  238. 				int style = (int) fontdialog.Font.Style;
  239. 				switch ( showproperty )
  240. 				{
  241. 					case "charset":
  242. 						Console.WriteLine( "{0} ({1})", Enum.GetName( typeof( FontCharSet ), charset ), charset );
  243. 						break;
  244. 					case "color":
  245. 						Console.WriteLine( fontdialog.Color.Name );
  246. 						break;
  247. 					case "name":
  248. 						Console.WriteLine( fontdialog.Font.Name );
  249. 						break;
  250. 					case "rgb":
  251. 						Console.WriteLine( "{0},{1},{2} (0x{3,6:x6})", fontdialog.Color.R, fontdialog.Color.G, fontdialog.Color.B, rgb );
  252. 						break;
  253. 					case "size":
  254. 						Console.WriteLine( fontdialog.Font.Size );
  255. 						break;
  256. 					case "style":
  257. 						Console.WriteLine( "{0} ({1})", fontdialog.Font.Style, style );
  258. 						break;
  259. 					default:
  260. 						Console.WriteLine( "Font Name     : {0}", fontdialog.Font.Name );
  261. 						Console.WriteLine( "Font Size     : {0}", fontdialog.Font.Size );
  262. 						Console.WriteLine( "Font Style    : {0} ({1})", fontdialog.Font.Style, style );
  263. 						Console.WriteLine( "Font Color    : {0}", fontdialog.Color.Name );
  264. 						Console.WriteLine( "RGB Color     : {0},{1},{2} (0x{3,6:x6})", fontdialog.Color.R, fontdialog.Color.G, fontdialog.Color.B, rgb );
  265. 						Console.WriteLine( "Character Set : {0} ({1})", Enum.GetName( typeof( FontCharSet ), charset ), charset );
  266. 						break;
  267. 				}
  268.  
  269. 				switch ( returnproperty )
  270. 				{
  271. 					case "c":
  272. 						rc = charset;
  273. 						break;
  274. 					case "r":
  275. 						rc = rgb;
  276. 						break;
  277. 					case "s":
  278. 						rc = size;
  279. 						break;
  280. 					case "y":
  281. 						rc = style;
  282. 						break;
  283. 					default:
  284. 						rc = 0;
  285. 						break;
  286. 				}
  287. 			}
  288.  
  289.  
  290. 			return rc;
  291. 		}
  292.  
  293.  
  294. 		static int ShowHelp( params string[] errmsg )
  295.  
  296. 		{
  297. 			/*
  298. 			FontSelectBox.exe,  Version 1.00
  299. 			Batch tool to present a Font Select dialog and return selected font properties
  300.  
  301. 			Usage:    FONTSELECTBOX  [ fontname ]  [ options ]  [ /R:return ]
  302.  
  303. 			Where:    fontname     initial font name (default: Courier New)
  304.  
  305. 			Options:  /I:size      Initial font size (default: 12)
  306. 			          /MAX:size    Maximum font size (default: 48)
  307. 			          /MIN:size    Minimum font size (default:  6)
  308. 			          /P:property  show only the requested Property for the selected font
  309. 			                       on screen, instead of "all" properties; property can be
  310. 			                       "Name", "Size", "Style", "Color", "RGB" or "CharSet"
  311. 			          /C           allow Color change (requires /E)
  312. 			          /E           allow Effects (e.g. strikeout and underline)
  313. 			          /H           allow "script" fonts only, no symbols
  314. 			          /S           allow Script (character set) change
  315. 			          /V           allow Vector fonts
  316. 			          /X           allow fiXed pitch fonts only
  317.  
  318. 			Return:   default      return code 0 on valid selection, -1 on cancel or error
  319. 			          /R:C         Return code equals selected Character set number
  320. 			          /R:R         Return code equals RGB value of selected color
  321. 			          /R:S         Return code equals selected font Size (rounded)
  322. 			          /R:Y         Return code equals selected stYle: Regular = 0,
  323. 			                       Bold + 1, Italic + 2, Underline + 4, Strikeout + 8
  324. 			                       e.g. return code 7 means Bold + Italic + Underline
  325.  
  326. 			Written by Rob van der Woude
  327. 			http://www.robvanderwoude.com
  328. 			*/
  329.  
  330.  
  331. 			#region Error Message
  332.  
  333. 			if ( errmsg.Length > 0 )
  334. 			{
  335. 				List<string> errargs = new List<string>( errmsg );
  336. 				errargs.RemoveAt( 0 );
  337. 				Console.Error.WriteLine( );
  338. 				Console.ForegroundColor = ConsoleColor.Red;
  339. 				Console.Error.Write( "ERROR:\t" );
  340. 				Console.ForegroundColor = ConsoleColor.White;
  341. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  342. 				Console.ResetColor( );
  343. 			}
  344.  
  345. 			#endregion Error Message
  346.  
  347.  
  348. 			#region Help Text
  349.  
  350. 			Console.Error.WriteLine( );
  351.  
  352. 			Console.Error.WriteLine( "FontSelectBox.exe,  Version {0}", progver );
  353.  
  354. 			Console.Error.WriteLine( "Batch tool to present a Font Select dialog and return selected font properties" );
  355.  
  356. 			Console.Error.WriteLine( );
  357.  
  358. 			Console.Error.Write( "Usage:    " );
  359. 			Console.ForegroundColor = ConsoleColor.White;
  360. 			Console.Error.WriteLine( "FONTSELECTBOX  [ fontname ]  [ options ]  [ /R:return ]" );
  361. 			Console.ResetColor( );
  362.  
  363. 			Console.Error.WriteLine( );
  364.  
  365. 			Console.Error.Write( "Where:    " );
  366. 			Console.ForegroundColor = ConsoleColor.White;
  367. 			Console.Error.Write( "fontname" );
  368. 			Console.ResetColor( );
  369. 			Console.Error.WriteLine( "     initial font name (default: Courier New)" );
  370.  
  371. 			Console.Error.WriteLine( );
  372.  
  373. 			Console.Error.Write( "Options:  " );
  374. 			Console.ForegroundColor = ConsoleColor.White;
  375. 			Console.Error.Write( "/I:size      I" );
  376. 			Console.ResetColor( );
  377. 			Console.Error.WriteLine( "nitial font size (default: 12)" );
  378.  
  379. 			Console.ForegroundColor = ConsoleColor.White;
  380. 			Console.Error.Write( "          /MAX:size    Max" );
  381. 			Console.ResetColor( );
  382. 			Console.Error.WriteLine( "imum font size (default: 48)" );
  383.  
  384. 			Console.ForegroundColor = ConsoleColor.White;
  385. 			Console.Error.Write( "          /MIN:size    Min" );
  386. 			Console.ResetColor( );
  387. 			Console.Error.WriteLine( "imum font size (default:  6)" );
  388.  
  389. 			Console.ForegroundColor = ConsoleColor.White;
  390. 			Console.Error.Write( "          /P:property" );
  391. 			Console.ResetColor( );
  392. 			Console.Error.Write( "  show only the requested " );
  393. 			Console.ForegroundColor = ConsoleColor.White;
  394. 			Console.Error.Write( "P" );
  395. 			Console.ResetColor( );
  396. 			Console.Error.WriteLine( "roperty for the selected font" );
  397.  
  398. 			Console.Error.Write( "                       on screen, instead of \"all\" properties; " );
  399. 			Console.ForegroundColor = ConsoleColor.White;
  400. 			Console.Error.Write( "property" );
  401. 			Console.ResetColor( );
  402. 			Console.Error.WriteLine( " can be" );
  403.  
  404. 			Console.Error.WriteLine( "                       \"Name\", \"Size\", \"Style\", \"Color\", \"RGB\" or \"CharSet\"" );
  405.  
  406. 			Console.ForegroundColor = ConsoleColor.White;
  407. 			Console.Error.Write( "          /C" );
  408. 			Console.ResetColor( );
  409. 			Console.Error.Write( "           allow " );
  410. 			Console.ForegroundColor = ConsoleColor.White;
  411. 			Console.Error.Write( "C" );
  412. 			Console.ResetColor( );
  413. 			Console.Error.Write( "olor change (requires " );
  414. 			Console.ForegroundColor = ConsoleColor.White;
  415. 			Console.Error.Write( "/E" );
  416. 			Console.ResetColor( );
  417. 			Console.Error.WriteLine( ")" );
  418.  
  419. 			Console.ForegroundColor = ConsoleColor.White;
  420. 			Console.Error.Write( "          /E" );
  421. 			Console.ResetColor( );
  422. 			Console.Error.Write( "           allow " );
  423. 			Console.ForegroundColor = ConsoleColor.White;
  424. 			Console.Error.Write( "E" );
  425. 			Console.ResetColor( );
  426. 			Console.Error.WriteLine( "ffects (e.g. strikeout and underline)" );
  427.  
  428. 			Console.ForegroundColor = ConsoleColor.White;
  429. 			Console.Error.Write( "          /H" );
  430. 			Console.ResetColor( );
  431. 			Console.Error.WriteLine( "           allow \"script\" fonts only, no symbols" );
  432.  
  433. 			Console.ForegroundColor = ConsoleColor.White;
  434. 			Console.Error.Write( "          /S" );
  435. 			Console.ResetColor( );
  436. 			Console.Error.Write( "           allow " );
  437. 			Console.ForegroundColor = ConsoleColor.White;
  438. 			Console.Error.Write( "S" );
  439. 			Console.ResetColor( );
  440. 			Console.Error.WriteLine( "cript (character set) change" );
  441.  
  442. 			Console.ForegroundColor = ConsoleColor.White;
  443. 			Console.Error.Write( "          /V" );
  444. 			Console.ResetColor( );
  445. 			Console.Error.Write( "           allow " );
  446. 			Console.ForegroundColor = ConsoleColor.White;
  447. 			Console.Error.Write( "V" );
  448. 			Console.ResetColor( );
  449. 			Console.Error.WriteLine( "ector fonts" );
  450.  
  451. 			Console.ForegroundColor = ConsoleColor.White;
  452. 			Console.Error.Write( "          /X" );
  453. 			Console.ResetColor( );
  454. 			Console.Error.Write( "           allow fi" );
  455. 			Console.ForegroundColor = ConsoleColor.White;
  456. 			Console.Error.Write( "X" );
  457. 			Console.ResetColor( );
  458. 			Console.Error.WriteLine( "ed pitch fonts only" );
  459.  
  460. 			Console.Error.WriteLine( );
  461.  
  462. 			Console.Error.WriteLine( "Return:   default      return code 0 on valid selection, -1 on cancel or error" );
  463.  
  464. 			Console.ForegroundColor = ConsoleColor.White;
  465. 			Console.Error.Write( "          /R:C         R" );
  466. 			Console.ResetColor( );
  467. 			Console.Error.Write( "eturn code equals selected " );
  468. 			Console.ForegroundColor = ConsoleColor.White;
  469. 			Console.Error.Write( "C" );
  470. 			Console.ResetColor( );
  471. 			Console.Error.WriteLine( "haracter set number" );
  472.  
  473. 			Console.ForegroundColor = ConsoleColor.White;
  474. 			Console.Error.Write( "          /R:R         R" );
  475. 			Console.ResetColor( );
  476. 			Console.Error.Write( "eturn code equals " );
  477. 			Console.ForegroundColor = ConsoleColor.White;
  478. 			Console.Error.Write( "R" );
  479. 			Console.ResetColor( );
  480. 			Console.Error.WriteLine( "GB value of selected color" );
  481.  
  482. 			Console.ForegroundColor = ConsoleColor.White;
  483. 			Console.Error.Write( "          /R:S         R" );
  484. 			Console.ResetColor( );
  485. 			Console.Error.Write( "eturn code equals selected font " );
  486. 			Console.ForegroundColor = ConsoleColor.White;
  487. 			Console.Error.Write( "S" );
  488. 			Console.ResetColor( );
  489. 			Console.Error.WriteLine( "ize (rounded)" );
  490.  
  491. 			Console.ForegroundColor = ConsoleColor.White;
  492. 			Console.Error.Write( "          /R:Y         R" );
  493. 			Console.ResetColor( );
  494. 			Console.Error.Write( "eturn code equals selected st" );
  495. 			Console.ForegroundColor = ConsoleColor.White;
  496. 			Console.Error.Write( "Y" );
  497. 			Console.ResetColor( );
  498. 			Console.Error.WriteLine( "le: Regular = 0," );
  499.  
  500. 			Console.Error.WriteLine( "                       Bold + 1, Italic + 2, Underline + 4, Strikeout + 8" );
  501.  
  502. 			Console.Error.WriteLine( "                       e.g. return code 7 means Bold + Italic + Underline" );
  503.  
  504. 			Console.Error.WriteLine( );
  505.  
  506. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  507.  
  508. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  509.  
  510. 			#endregion Help Text
  511.  
  512.  
  513. 			return -1;
  514. 		}
  515. 	}
  516.  
  517.  
  518. 	public enum FontCharSet : byte
  519. 	{
  520. 		ANSI = 0,
  521. 		Default = 1,
  522. 		Symbol = 2,
  523. 		Mac = 77,
  524. 		ShiftJIS = 128,
  525. 		Hangeul = 129,
  526. 		Johab = 130,
  527. 		GB2312 = 134,
  528. 		ChineseBig5 = 136,
  529. 		Greek = 161,
  530. 		Turkish = 162,
  531. 		Hebrew = 177,
  532. 		Arabic = 178,
  533. 		Baltic = 186,
  534. 		Russian = 204,
  535. 		Thai = 222,
  536. 		EastEurope = 238,
  537. 		OEM = 255,
  538. 	}
  539. }
  540.  

page last modified: 2024-04-16; loaded in 0.0136 seconds