ToolStripFontComboBox.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using FastReport.Utils;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Runtime.InteropServices;
  7. using System.Windows.Forms;
  8. namespace FastReport.Controls
  9. {
  10. internal class ToolStripFontComboBox : ToolStripComboBox
  11. {
  12. private List<string> mruFonts;
  13. private List<string> existingFonts;
  14. private FontStyle[] styles;
  15. private string fontName;
  16. public new Control Owner { get; set; }
  17. public event EventHandler FontSelected;
  18. [Browsable(false)]
  19. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  20. public string FontName
  21. {
  22. get { return (string)ComboBox.SelectedItem; }
  23. set
  24. {
  25. fontName = value;
  26. int i = ComboBox.Items.IndexOf(value);
  27. if (i != -1)
  28. ComboBox.SelectedIndex = i;
  29. else
  30. ComboBox.SelectedItem = null;
  31. }
  32. }
  33. [Browsable(false)]
  34. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  35. public string MruFonts
  36. {
  37. get
  38. {
  39. string result = "";
  40. foreach (string s in mruFonts)
  41. result += "," + s;
  42. if (result.StartsWith(","))
  43. result = result.Substring(1);
  44. return result;
  45. }
  46. set
  47. {
  48. mruFonts.Clear();
  49. if (!String.IsNullOrEmpty(value))
  50. {
  51. string[] fonts = value.Split(',');
  52. foreach (string s in fonts)
  53. mruFonts.Add(s);
  54. }
  55. UpdateFonts();
  56. }
  57. }
  58. [Browsable(false)]
  59. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  60. public new System.Windows.Forms.ComboBox.ObjectCollection Items
  61. {
  62. get { return base.Items; }
  63. }
  64. private FontStyle GetFirstAvailableFontStyle(FontFamily family)
  65. {
  66. foreach (FontStyle style in styles)
  67. {
  68. if (family.IsStyleAvailable(style))
  69. return style;
  70. }
  71. return FontStyle.Regular;
  72. }
  73. private void ComboBox_DrawItem(object sender, DrawItemEventArgs e)
  74. {
  75. if (!Enabled)
  76. return;
  77. Graphics g = e.Graphics;
  78. e.DrawBackground();
  79. if ((e.State & DrawItemState.ComboBoxEdit) > 0)
  80. {
  81. if (!String.IsNullOrEmpty(fontName))
  82. TextRenderer.DrawText(g, fontName, e.Font, e.Bounds, e.ForeColor, TextFormatFlags.VerticalCenter);
  83. }
  84. else if (e.Index >= 0)
  85. {
  86. string name = (string)Items[e.Index];
  87. if (!existingFonts.Contains(name))
  88. return;
  89. int _16 = Owner.LogicalToDevice(16);
  90. int _20 = Owner.LogicalToDevice(20);
  91. int _28 = Owner.LogicalToDevice(28);
  92. using (FontFamily family = FontManager.GetFontFamilyOrDefault(name))
  93. using (Font font = Owner.LogicalToDevice(new Font(family, 14, GetFirstAvailableFontStyle(family)), true))
  94. {
  95. g.DrawImage(Owner.GetImage(59), e.Bounds.X + 2, e.Bounds.Y + (e.Bounds.Height - _16) / 2);
  96. LOGFONT lf = new LOGFONT();
  97. font.ToLogFont(lf);
  98. if (lf.lfCharSet == 2)
  99. {
  100. var sz = g.MeasureString(name, e.Font);
  101. int w = (int)sz.Width;
  102. TextRenderer.DrawText(g, name, e.Font, new Point(e.Bounds.X + _20, e.Bounds.Y + (e.Bounds.Height - (int)sz.Height) / 2), e.ForeColor);
  103. sz = g.MeasureString(name, font);
  104. TextRenderer.DrawText(g, name, font, new Point(e.Bounds.X + w + _28, e.Bounds.Y + (e.Bounds.Height - (int)sz.Height) / 2), e.ForeColor);
  105. }
  106. else
  107. {
  108. var sz = g.MeasureString(name, font);
  109. TextRenderer.DrawText(g, name, font, new Point(e.Bounds.X + _20, e.Bounds.Y + (e.Bounds.Height - (int)sz.Height) / 2), e.ForeColor);
  110. }
  111. if (e.Index == mruFonts.Count - 1)
  112. {
  113. int _1 = Owner.LogicalToDevice(1);
  114. int _3 = Owner.LogicalToDevice(3);
  115. g.DrawLine(Pens.Gray, e.Bounds.Left, e.Bounds.Bottom - _3, e.Bounds.Right, e.Bounds.Bottom - _3);
  116. g.DrawLine(Pens.Gray, e.Bounds.Left, e.Bounds.Bottom - _1, e.Bounds.Right, e.Bounds.Bottom - _1);
  117. }
  118. }
  119. }
  120. }
  121. private void ComboBox_MeasureItem(object sender, MeasureItemEventArgs e)
  122. {
  123. if (Owner != null)
  124. e.ItemHeight = Owner.LogicalToDevice(24);
  125. }
  126. private void ComboBox_SelectionChangeCommitted(object sender, EventArgs e)
  127. {
  128. OnFontSelected();
  129. if (mruFonts.Contains(FontName))
  130. mruFonts.Remove(FontName);
  131. mruFonts.Insert(0, FontName);
  132. while (mruFonts.Count > 5)
  133. mruFonts.RemoveAt(5);
  134. UpdateFonts();
  135. FontName = fontName;
  136. }
  137. private void OnFontSelected()
  138. {
  139. if (FontSelected != null)
  140. FontSelected(this, EventArgs.Empty);
  141. }
  142. private void UpdateFonts()
  143. {
  144. Items.Clear();
  145. foreach (string s in mruFonts)
  146. {
  147. if (existingFonts.Contains(s))
  148. Items.Add(s);
  149. }
  150. foreach (string s in existingFonts)
  151. {
  152. Items.Add(s);
  153. }
  154. }
  155. public void UpdateDpiDependencies()
  156. {
  157. ComboBox.ItemHeight = Owner.LogicalToDevice(15);
  158. DropDownHeight = Owner.LogicalToDevice(302);
  159. DropDownWidth = Owner.LogicalToDevice(270);
  160. }
  161. public void SetStyle(UIStyle style)
  162. {
  163. }
  164. public ToolStripFontComboBox()
  165. {
  166. mruFonts = new List<string>();
  167. existingFonts = new List<string>();
  168. styles = new FontStyle[] { FontStyle.Regular, FontStyle.Bold, FontStyle.Italic,
  169. FontStyle.Strikeout, FontStyle.Underline };
  170. foreach (FontFamily family in FontManager.AllFamilies)
  171. {
  172. existingFonts.Add(family.Name);
  173. }
  174. existingFonts.Sort();
  175. AutoSize = false;
  176. ComboBox.DrawMode = DrawMode.OwnerDrawVariable;
  177. ComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
  178. ComboBox.DrawItem += ComboBox_DrawItem;
  179. ComboBox.MeasureItem += ComboBox_MeasureItem;
  180. ComboBox.SelectionChangeCommitted += ComboBox_SelectionChangeCommitted;
  181. UpdateFonts();
  182. }
  183. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  184. private class LOGFONT
  185. {
  186. public int lfHeight;
  187. public int lfWidth;
  188. public int lfEscapement;
  189. public int lfOrientation;
  190. public int lfWeight;
  191. public byte lfItalic;
  192. public byte lfUnderline;
  193. public byte lfStrikeOut;
  194. public byte lfCharSet;
  195. public byte lfOutPrecision;
  196. public byte lfClipPrecision;
  197. public byte lfQuality;
  198. public byte lfPitchAndFamily;
  199. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  200. public string lfFaceName;
  201. }
  202. }
  203. }