UIButton.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using System.Drawing;
  2. using System.Drawing.Text;
  3. using System.Windows.Forms;
  4. using Microsoft.Drawing;
  5. namespace Microsoft.Windows.Forms
  6. {
  7. /// <summary>
  8. /// 虚拟按钮
  9. /// </summary>
  10. public class UIButton : UIControl
  11. {
  12. private Color m_HoveredBackColor = DefaultTheme.DarkDarkTransparent;
  13. /// <summary>
  14. /// 鼠标悬停背景色
  15. /// </summary>
  16. public virtual Color HoveredBackColor
  17. {
  18. get
  19. {
  20. return this.m_HoveredBackColor;
  21. }
  22. set
  23. {
  24. if (value != this.m_HoveredBackColor)
  25. {
  26. this.m_HoveredBackColor = value;
  27. this.Invalidate();
  28. }
  29. }
  30. }
  31. private Color m_PressedBackColor = DefaultTheme.DarkTransparent;
  32. /// <summary>
  33. /// 鼠标按下背景色
  34. /// </summary>
  35. public virtual Color PressedBackColor
  36. {
  37. get
  38. {
  39. return this.m_PressedBackColor;
  40. }
  41. set
  42. {
  43. if (value != this.m_PressedBackColor)
  44. {
  45. this.m_PressedBackColor = value;
  46. this.Invalidate();
  47. }
  48. }
  49. }
  50. private Image m_Image;
  51. /// <summary>
  52. /// 图片
  53. /// </summary>
  54. public virtual Image Image
  55. {
  56. get
  57. {
  58. return this.m_Image;
  59. }
  60. set
  61. {
  62. if (value != this.m_Image)
  63. {
  64. this.m_Image = value;
  65. this.Invalidate();
  66. }
  67. }
  68. }
  69. private Image m_ImageHovered;
  70. /// <summary>
  71. /// 图片
  72. /// </summary>
  73. public virtual Image ImageHovered
  74. {
  75. get
  76. {
  77. return this.m_ImageHovered;
  78. }
  79. set
  80. {
  81. if (value != this.m_ImageHovered)
  82. {
  83. this.m_ImageHovered = value;
  84. this.Invalidate();
  85. }
  86. }
  87. }
  88. private Image m_ImagePressed;
  89. /// <summary>
  90. /// 图片
  91. /// </summary>
  92. public virtual Image ImagePressed
  93. {
  94. get
  95. {
  96. return this.m_ImagePressed;
  97. }
  98. set
  99. {
  100. if (value != this.m_ImagePressed)
  101. {
  102. this.m_ImagePressed = value;
  103. this.Invalidate();
  104. }
  105. }
  106. }
  107. private Image m_ImageDisabled;
  108. /// <summary>
  109. /// 图片
  110. /// </summary>
  111. public virtual Image ImageDisabled
  112. {
  113. get
  114. {
  115. return this.m_ImageDisabled;
  116. }
  117. set
  118. {
  119. if (value != this.m_ImageDisabled)
  120. {
  121. this.m_ImageDisabled = value;
  122. this.Invalidate();
  123. }
  124. }
  125. }
  126. private Size m_ImageSize;
  127. /// <summary>
  128. /// 图片大小
  129. /// </summary>
  130. public virtual Size ImageSize
  131. {
  132. get
  133. {
  134. return this.m_ImageSize;
  135. }
  136. set
  137. {
  138. if (value != this.m_ImageSize)
  139. {
  140. this.m_ImageSize = value;
  141. this.Invalidate();
  142. }
  143. }
  144. }
  145. private ContentAlignment m_ImageAlign = ContentAlignment.MiddleCenter;
  146. /// <summary>
  147. /// 图片对齐方式
  148. /// </summary>
  149. public virtual ContentAlignment ImageAlign
  150. {
  151. get
  152. {
  153. return this.m_ImageAlign;
  154. }
  155. set
  156. {
  157. if (value != this.m_ImageAlign)
  158. {
  159. this.m_ImageAlign = value;
  160. this.Invalidate();
  161. }
  162. }
  163. }
  164. private TextRenderingHint m_TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
  165. /// <summary>
  166. /// 文本呈现质量
  167. /// </summary>
  168. public virtual TextRenderingHint TextRenderingHint
  169. {
  170. get
  171. {
  172. return this.m_TextRenderingHint;
  173. }
  174. set
  175. {
  176. if (value != this.m_TextRenderingHint)
  177. {
  178. this.m_TextRenderingHint = value;
  179. this.Invalidate();
  180. }
  181. }
  182. }
  183. private ContentAlignment m_TextAlign = ContentAlignment.MiddleCenter;
  184. /// <summary>
  185. /// 文本对齐方式
  186. /// </summary>
  187. public virtual ContentAlignment TextAlign
  188. {
  189. get
  190. {
  191. return this.m_TextAlign;
  192. }
  193. set
  194. {
  195. if (value != this.m_TextAlign)
  196. {
  197. this.m_TextAlign = value;
  198. this.Invalidate();
  199. }
  200. }
  201. }
  202. private TextImageRelation m_TextImageRelation = TextImageRelation.ImageBeforeText;
  203. /// <summary>
  204. /// 文本图片关系
  205. /// </summary>
  206. public virtual TextImageRelation TextImageRelation
  207. {
  208. get
  209. {
  210. return this.m_TextImageRelation;
  211. }
  212. set
  213. {
  214. if (value != this.m_TextImageRelation)
  215. {
  216. this.m_TextImageRelation = value;
  217. this.Invalidate();
  218. }
  219. }
  220. }
  221. /// <summary>
  222. ///
  223. /// </summary>
  224. /// <returns></returns>
  225. protected override State GetState()
  226. {
  227. if (this.Enabled)
  228. {
  229. if (this.Capture)
  230. {
  231. if ((Control.MouseButtons & MouseButtons.Left) != 0)//左键按下
  232. return State.Pressed;
  233. else
  234. return State.Hovered;
  235. }
  236. else
  237. {
  238. return State.Normal;
  239. }
  240. }
  241. else
  242. {
  243. return State.Disabled;
  244. }
  245. }
  246. /// <summary>
  247. /// 构造函数
  248. /// </summary>
  249. public UIButton()
  250. {
  251. this.BackColor = DefaultTheme.Transparent;
  252. }
  253. /// <summary>
  254. /// 渲染控件
  255. /// </summary>
  256. /// <param name="e">数据</param>
  257. protected override void RenderSelf(PaintEventArgs e)
  258. {
  259. //准备
  260. Graphics g = e.Graphics;
  261. Rectangle rect = RectangleEx.Subtract(this.ClientRectangle, this.Padding);
  262. //渲染
  263. this.Sprite.BorderVisibleStyle = BorderVisibleStyle.None;
  264. this.Sprite.BackColor = this.BackColor;
  265. this.Sprite.BackColorHovered = this.HoveredBackColor;
  266. this.Sprite.BackColorPressed = this.PressedBackColor;
  267. this.Sprite.Image = this.Image;
  268. this.Sprite.ImageHovered = this.ImageHovered;
  269. this.Sprite.ImagePressed = this.ImagePressed;
  270. this.Sprite.ImageDisabled = this.ImageDisabled;
  271. this.Sprite.ImageSize = this.ImageSize;
  272. this.Sprite.ImageAlign = this.ImageAlign;
  273. this.Sprite.Font = this.Font;
  274. this.Sprite.ForeColor = this.ForeColor;
  275. this.Sprite.ForeColorHovered = this.ForeColor;
  276. this.Sprite.ForeColorPressed = this.ForeColor;
  277. this.Sprite.ForeColorDisabled = this.ForeColor;
  278. this.Sprite.Text = this.Text;
  279. this.Sprite.TextRenderingHint = this.TextRenderingHint;
  280. this.Sprite.TextAlign = this.TextAlign;
  281. this.Sprite.TextImageRelation = this.TextImageRelation;
  282. this.Sprite.State = this.State;
  283. this.Sprite.BeginRender(g);
  284. this.Sprite.RenderBackColor(rect);
  285. this.Sprite.RenderTextAndImage(rect);
  286. this.Sprite.EndRender();
  287. }
  288. }
  289. }