MirLabel.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System.Drawing.Drawing2D;
  2. using System.Drawing.Imaging;
  3. using System.Drawing.Text;
  4. using Client.MirGraphics;
  5. using SlimDX;
  6. using SlimDX.Direct3D9;
  7. using Font = System.Drawing.Font;
  8. namespace Client.MirControls
  9. {
  10. public class MirLabel : MirControl
  11. {
  12. #region Auto Size
  13. private bool _autoSize;
  14. public bool AutoSize
  15. {
  16. get { return _autoSize; }
  17. set
  18. {
  19. if (_autoSize == value)
  20. return;
  21. _autoSize = value;
  22. OnAutoSizeChanged(EventArgs.Empty);
  23. }
  24. }
  25. public event EventHandler AutoSizeChanged;
  26. private void OnAutoSizeChanged(EventArgs e)
  27. {
  28. TextureValid = false;
  29. GetSize();
  30. if (AutoSizeChanged != null)
  31. AutoSizeChanged.Invoke(this, e);
  32. }
  33. #endregion
  34. #region DrawFormat
  35. private TextFormatFlags _drawFormat;
  36. public TextFormatFlags DrawFormat
  37. {
  38. get { return _drawFormat; }
  39. set
  40. {
  41. _drawFormat = value;
  42. OnDrawFormatChanged(EventArgs.Empty);
  43. }
  44. }
  45. public event EventHandler DrawFormatChanged;
  46. private void OnDrawFormatChanged(EventArgs e)
  47. {
  48. TextureValid = false;
  49. if (DrawFormatChanged != null)
  50. DrawFormatChanged.Invoke(this, e);
  51. }
  52. #endregion
  53. #region Font
  54. private Font _font;
  55. public Font Font
  56. {
  57. get { return _font; }
  58. set
  59. {
  60. _font = ScaleFont(value);
  61. OnFontChanged(EventArgs.Empty);
  62. }
  63. }
  64. public event EventHandler FontChanged;
  65. private void OnFontChanged(EventArgs e)
  66. {
  67. TextureValid = false;
  68. GetSize();
  69. if (FontChanged != null)
  70. FontChanged.Invoke(this, e);
  71. }
  72. #endregion
  73. #region Out Line
  74. private bool _outLine;
  75. public bool OutLine
  76. {
  77. get { return _outLine; }
  78. set
  79. {
  80. if (_outLine == value)
  81. return;
  82. _outLine = value;
  83. OnOutLineChanged(EventArgs.Empty);
  84. }
  85. }
  86. public event EventHandler OutLineChanged;
  87. private void OnOutLineChanged(EventArgs e)
  88. {
  89. TextureValid = false;
  90. GetSize();
  91. if (OutLineChanged != null)
  92. OutLineChanged.Invoke(this, e);
  93. }
  94. #endregion
  95. #region Out Line Colour
  96. private Color _outLineColour;
  97. public Color OutLineColour
  98. {
  99. get { return _outLineColour; }
  100. set
  101. {
  102. if (_outLineColour == value)
  103. return;
  104. _outLineColour = value;
  105. OnOutLineColourChanged();
  106. }
  107. }
  108. public event EventHandler OutLineColourChanged;
  109. private void OnOutLineColourChanged()
  110. {
  111. TextureValid = false;
  112. if (OutLineColourChanged != null)
  113. OutLineColourChanged.Invoke(this, EventArgs.Empty);
  114. }
  115. #endregion
  116. #region Size
  117. private void GetSize()
  118. {
  119. if (!AutoSize)
  120. return;
  121. if (string.IsNullOrEmpty(_text))
  122. Size = Size.Empty;
  123. else
  124. {
  125. Size = TextRenderer.MeasureText(CMain.Graphics, Text, Font);
  126. //Size = new Size(Size.Width, Size.Height + 5);
  127. if (OutLine && Size != Size.Empty)
  128. Size = new Size(Size.Width + 2, Size.Height + 2);
  129. }
  130. }
  131. #endregion
  132. #region Label
  133. private string _text;
  134. public string Text
  135. {
  136. get { return _text; }
  137. set
  138. {
  139. if (_text == value)
  140. return;
  141. _text = value;
  142. OnTextChanged(EventArgs.Empty);
  143. }
  144. }
  145. public event EventHandler TextChanged;
  146. private void OnTextChanged(EventArgs e)
  147. {
  148. DrawControlTexture = !string.IsNullOrEmpty(Text);
  149. TextureValid = false;
  150. Redraw();
  151. GetSize();
  152. if (TextChanged != null)
  153. TextChanged.Invoke(this, e);
  154. }
  155. #endregion
  156. public MirLabel()
  157. {
  158. DrawControlTexture = true;
  159. _drawFormat = TextFormatFlags.WordBreak;
  160. _font = ScaleFont(new Font(Settings.FontName, 8F));
  161. _outLine = true;
  162. _outLineColour = Color.Black;
  163. _text = string.Empty;
  164. }
  165. protected override unsafe void CreateTexture()
  166. {
  167. if (string.IsNullOrEmpty(Text))
  168. return;
  169. if (Size.Width == 0 || Size.Height == 0)
  170. return;
  171. if (TextureSize != Size)
  172. DisposeTexture();
  173. if (ControlTexture == null || ControlTexture.Disposed)
  174. {
  175. DXManager.ControlList.Add(this);
  176. ControlTexture = new Texture(DXManager.Device, Size.Width, Size.Height, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
  177. TextureSize = Size;
  178. }
  179. DataRectangle stream = ControlTexture.LockRectangle(0, LockFlags.Discard);
  180. using (Bitmap image = new Bitmap(Size.Width, Size.Height, Size.Width * 4, PixelFormat.Format32bppArgb, stream.Data.DataPointer))
  181. {
  182. using (Graphics graphics = Graphics.FromImage(image))
  183. {
  184. graphics.SmoothingMode = SmoothingMode.AntiAlias;
  185. graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
  186. graphics.CompositingQuality = CompositingQuality.HighQuality;
  187. graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
  188. graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  189. graphics.TextContrast = 0;
  190. graphics.Clear(BackColour);
  191. if (OutLine)
  192. {
  193. TextRenderer.DrawText(graphics, Text, Font, new Rectangle(1, 0, Size.Width, Size.Height), OutLineColour, DrawFormat);
  194. TextRenderer.DrawText(graphics, Text, Font, new Rectangle(0, 1, Size.Width, Size.Height), OutLineColour, DrawFormat);
  195. TextRenderer.DrawText(graphics, Text, Font, new Rectangle(2, 1, Size.Width, Size.Height), OutLineColour, DrawFormat);
  196. TextRenderer.DrawText(graphics, Text, Font, new Rectangle(1, 2, Size.Width, Size.Height), OutLineColour, DrawFormat);
  197. TextRenderer.DrawText(graphics, Text, Font, new Rectangle(1, 1, Size.Width, Size.Height), ForeColour, DrawFormat);
  198. //LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, this.Size.Width, this.Size.Height), Color.FromArgb(239, 243, 239), Color.White, LinearGradientMode.Vertical);
  199. ////graphics.DrawString(Text, Font, brush, 37, 9);
  200. ////graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), 39, 9, StringFormat.GenericDefault);
  201. }
  202. else
  203. TextRenderer.DrawText(graphics, Text, Font, new Rectangle(1, 0, Size.Width, Size.Height), ForeColour, DrawFormat);
  204. }
  205. }
  206. ControlTexture.UnlockRectangle(0);
  207. DXManager.Sprite.Flush();
  208. TextureValid = true;
  209. }
  210. #region Disposable
  211. protected override void Dispose(bool disposing)
  212. {
  213. base.Dispose(disposing);
  214. if (!disposing) return;
  215. AutoSizeChanged = null;
  216. _autoSize = false;
  217. DrawFormatChanged = null;
  218. _drawFormat = 0;
  219. FontChanged = null;
  220. if (_font != null)
  221. {
  222. _font.Dispose();
  223. _font = null;
  224. }
  225. OutLineChanged = null;
  226. _outLine = false;
  227. OutLineColourChanged = null;
  228. _outLineColour = Color.Empty;
  229. TextChanged = null;
  230. _text = null;
  231. }
  232. #endregion
  233. }
  234. }