CaptchaControl.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Text;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. using System.Windows.Media;
  8. namespace Muchinfo.WPF.Controls.CaptchaSpace
  9. {
  10. public class CaptchaControl : ItemsControl
  11. {
  12. static CaptchaControl()
  13. {
  14. DefaultStyleKeyProperty.OverrideMetadata(typeof(CaptchaControl), new FrameworkPropertyMetadata(typeof(CaptchaControl)));
  15. }
  16. public string QuantityCharacters
  17. {
  18. get { return (string)GetValue(QuantityCharactersProperty); }
  19. set { SetValue(QuantityCharactersProperty, value); }
  20. }
  21. // Using a DependencyProperty as the backing store for QuantityCharacters. This enables animation, styling, binding, etc...
  22. public static readonly DependencyProperty QuantityCharactersProperty =
  23. DependencyProperty.Register("QuantityCharacters", typeof(string), typeof(CaptchaControl), new PropertyMetadata("", OnQuantityCharactersChanged));
  24. public Color BackgroundColorStart
  25. {
  26. get { return (Color)GetValue(BackgroundColorStartProperty); }
  27. set { SetValue(BackgroundColorStartProperty, value); }
  28. }
  29. // Using a DependencyProperty as the backing store for BackgroundColorStart. This enables animation, styling, binding, etc...
  30. public static readonly DependencyProperty BackgroundColorStartProperty =
  31. DependencyProperty.Register("BackgroundColorStart", typeof(Color), typeof(CaptchaControl), new PropertyMetadata(Colors.Coral, OnColorStartChanged));
  32. public Color BackgroundColorEnd
  33. {
  34. get { return (Color)GetValue(BackgroundColorEndProperty); }
  35. set { SetValue(BackgroundColorEndProperty, value); }
  36. }
  37. // Using a DependencyProperty as the backing store for BackgroundColorEnd. This enables animation, styling, binding, etc...
  38. public static readonly DependencyProperty BackgroundColorEndProperty =
  39. DependencyProperty.Register("BackgroundColorEnd", typeof(Color), typeof(CaptchaControl), new PropertyMetadata(Colors.YellowGreen, OnColorEndChanged));
  40. public Color ForegroundColorStart
  41. {
  42. get { return (Color)GetValue(ForegroundColorStartProperty); }
  43. set { SetValue(ForegroundColorStartProperty, value); }
  44. }
  45. // Using a DependencyProperty as the backing store for ForegroundColorStart. This enables animation, styling, binding, etc...
  46. public static readonly DependencyProperty ForegroundColorStartProperty =
  47. DependencyProperty.Register("ForegroundColorStart", typeof(Color), typeof(CaptchaControl), new PropertyMetadata(Colors.Turquoise, OnForeColorStartChanged));
  48. public Color ForegroundColorEnd
  49. {
  50. get { return (Color)GetValue(ForegroundColorEndProperty); }
  51. set { SetValue(ForegroundColorEndProperty, value); }
  52. }
  53. // Using a DependencyProperty as the backing store for ForegroundColorEnd. This enables animation, styling, binding, etc...
  54. public static readonly DependencyProperty ForegroundColorEndProperty =
  55. DependencyProperty.Register("ForegroundColorEnd", typeof(Color), typeof(CaptchaControl), new PropertyMetadata(Colors.Lavender, OnForeColorEndChanged));
  56. public int SpaceBetweenLetters
  57. {
  58. get { return (int)GetValue(SpaceBetweenLettersProperty); }
  59. set { SetValue(SpaceBetweenLettersProperty, value); }
  60. }
  61. // Using a DependencyProperty as the backing store for SpaceBetweenLetters. This enables animation, styling, binding, etc...
  62. public static readonly DependencyProperty SpaceBetweenLettersProperty =
  63. DependencyProperty.Register("SpaceBetweenLetters", typeof(int), typeof(CaptchaControl), new PropertyMetadata(10, OnSpaceBetweenLettersChanged));
  64. public bool IsUniqueForegroundColor
  65. {
  66. get { return (bool)GetValue(IsUniqueForegroundColorProperty); }
  67. set { SetValue(IsUniqueForegroundColorProperty, value); }
  68. }
  69. // Using a DependencyProperty as the backing store for IsUniqueForegroundColor. This enables animation, styling, binding, etc...
  70. public static readonly DependencyProperty IsUniqueForegroundColorProperty =
  71. DependencyProperty.Register("IsUniqueForegroundColor", typeof(bool), typeof(CaptchaControl), new PropertyMetadata(true, OnIsUniqueForegroundColorChanged));
  72. public bool IsUniqueBackgroundColor
  73. {
  74. get { return (bool)GetValue(IsUniqueBackgroundColorProperty); }
  75. set { SetValue(IsUniqueBackgroundColorProperty, value); }
  76. }
  77. // Using a DependencyProperty as the backing store for IsUniqueBackgroundColor. This enables animation, styling, binding, etc...
  78. public static readonly DependencyProperty IsUniqueBackgroundColorProperty =
  79. DependencyProperty.Register("IsUniqueBackgroundColor", typeof(bool), typeof(CaptchaControl), new PropertyMetadata(true, OnIsUniqueBackgroundColorChanged));
  80. public string CaptchaText
  81. {
  82. get { return (string)GetValue(CaptchaTextProperty); }
  83. set { SetValue(CaptchaTextProperty, value); }
  84. }
  85. // Using a DependencyProperty as the backing store for CaptchaText. This enables animation, styling, binding, etc...
  86. public static readonly DependencyProperty CaptchaTextProperty =
  87. DependencyProperty.Register("CaptchaText", typeof(string), typeof(CaptchaControl), new PropertyMetadata("", OnCaptchaTextChanged));
  88. #region Global Variables
  89. private readonly Random random;
  90. private readonly Color color = new Color();
  91. private static readonly char[] CharArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123467890".ToCharArray();
  92. #endregion
  93. private ObservableCollection<Captcha> captchaLetters = new ObservableCollection<Captcha>();
  94. public ObservableCollection<Captcha> CaptchaLetters
  95. {
  96. get { return captchaLetters; }
  97. set { captchaLetters = value; }
  98. }
  99. public CaptchaControl()
  100. {
  101. random = new Random();
  102. }
  103. private static void OnCaptchaTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  104. {
  105. CaptchaControl captchacontrol = d as CaptchaControl;
  106. captchacontrol.SetBackgroundGradient();
  107. captchacontrol.SetForegroundGradient();
  108. captchacontrol.SetBackgroundGradient();
  109. }
  110. private static void OnIsUniqueBackgroundColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  111. {
  112. CaptchaControl captchacontrol = d as CaptchaControl;
  113. captchacontrol.SetBackgroundGradient();
  114. }
  115. private static void OnSpaceBetweenLettersChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  116. {
  117. CaptchaControl captchacontrol = d as CaptchaControl;
  118. //foreach (var captcha in captchacontrol.CaptchaLetters)
  119. //{
  120. // captcha.SpaceBetweenLetters = captchacontrol.SpaceBetweenLetters;
  121. //}
  122. }
  123. private static void OnIsUniqueForegroundColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  124. {
  125. CaptchaControl captcha = d as CaptchaControl;
  126. captcha.SetForegroundGradient();
  127. }
  128. private static void OnForeColorEndChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  129. {
  130. CaptchaControl captcha = d as CaptchaControl;
  131. captcha.SetForegroundGradient();
  132. }
  133. private static void OnForeColorStartChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  134. {
  135. CaptchaControl captcha = d as CaptchaControl;
  136. captcha.SetForegroundGradient();
  137. }
  138. private static void OnQuantityCharactersChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
  139. {
  140. //CaptchaControl captcha = d as CaptchaControl;
  141. //var quantity = 8;
  142. //if (int.TryParse(captcha.QuantityCharacters, out quantity))
  143. //{
  144. // captcha.InitializeCaptcha(quantity);
  145. //}
  146. //else
  147. //{
  148. // throw new Exception("You have to set QuantityCharacter property");
  149. //}
  150. }
  151. private static void OnColorStartChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
  152. {
  153. CaptchaControl captcha = d as CaptchaControl;
  154. captcha.SetBackgroundGradient();
  155. }
  156. private static void OnColorEndChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
  157. {
  158. CaptchaControl captcha = d as CaptchaControl;
  159. captcha.SetBackgroundGradient();
  160. }
  161. private void InitializeCaptcha(int quantityCharacters)
  162. {
  163. CaptchaLetters = new ObservableCollection<Captcha>();
  164. var captcha = new char[quantityCharacters];
  165. for (var x = 0; x < captcha.Length; x++)
  166. {
  167. captcha[x] = CharArray[random.Next(CharArray.Length)];
  168. var c = new Captcha
  169. {
  170. Letter = captcha[x].ToString(),
  171. FontSize = (int)this.FontSize,
  172. SpaceBetweenLetters = this.SpaceBetweenLetters
  173. };
  174. SetTransformGroup(c);
  175. CaptchaLetters.Add(c);
  176. }
  177. var builder = new StringBuilder();
  178. foreach (var letter in CaptchaLetters)
  179. {
  180. builder.Append(letter.Letter);
  181. }
  182. CaptchaText = builder.ToString();
  183. this.ItemsSource = CaptchaLetters;
  184. }
  185. public override void OnApplyTemplate()
  186. {
  187. var quantity = 8;
  188. if (int.TryParse(QuantityCharacters, out quantity))
  189. {
  190. InitializeCaptcha(quantity);
  191. }
  192. else
  193. {
  194. throw new Exception("You have to set QuantityCharacter property");
  195. }
  196. base.OnApplyTemplate();
  197. }
  198. protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
  199. {
  200. ReflushCaptchaText();
  201. base.OnMouseLeftButtonDown(e);
  202. }
  203. /// <summary>
  204. /// 刷新验证码
  205. /// </summary>
  206. public void ReflushCaptchaText()
  207. {
  208. var quantity = 8;
  209. if (int.TryParse(QuantityCharacters, out quantity))
  210. {
  211. InitializeCaptcha(quantity);
  212. }
  213. else
  214. {
  215. throw new Exception("You have to set QuantityCharacter property");
  216. }
  217. }
  218. protected override void OnMouseEnter(MouseEventArgs e)
  219. {
  220. // e.MouseDevice.SetCursor(Cursors.Hand);
  221. Mouse.SetCursor(Cursors.Hand);
  222. base.OnMouseEnter(e);
  223. }
  224. protected override void OnMouseLeave(MouseEventArgs e)
  225. {
  226. Mouse.SetCursor(Cursors.Arrow);
  227. base.OnMouseLeave(e);
  228. }
  229. private void SetForegroundGradient()
  230. {
  231. if (IsUniqueForegroundColor)
  232. {
  233. foreach (var captcha in CaptchaLetters)
  234. {
  235. captcha.ForegroundGradient = this.SetForegroundGradientSameColor();
  236. }
  237. }
  238. else
  239. {
  240. foreach (var captcha in CaptchaLetters)
  241. {
  242. captcha.ForegroundGradient = this.SetForegroundGradientRandomColor();
  243. }
  244. }
  245. }
  246. private LinearGradientBrush SetForegroundGradientRandomColor()
  247. {
  248. var gradient = new LinearGradientBrush();
  249. gradient.StartPoint = new Point(0.5f, 1);
  250. gradient.EndPoint = new Point(0.8f, 0);
  251. gradient.GradientStops.Add(new GradientStop()
  252. {
  253. Color = Color.FromArgb((byte)random.Next(0, 255),
  254. (byte)random.Next(0, 255),
  255. (byte)random.Next(0, 255),
  256. (byte)random.Next(0, 255)),
  257. Offset = 3
  258. });
  259. gradient.GradientStops.Add(new GradientStop()
  260. {
  261. Color = Color.FromArgb((byte)random.Next(0, 255),
  262. (byte)random.Next(0, 255),
  263. (byte)random.Next(0, 255),
  264. (byte)random.Next(0, 255))
  265. });
  266. return gradient;
  267. }
  268. private LinearGradientBrush SetForegroundGradientSameColor()
  269. {
  270. var gradient = new LinearGradientBrush();
  271. gradient.StartPoint = new Point(0.5f, 1);
  272. gradient.EndPoint = new Point(0.8f, 0);
  273. gradient.GradientStops.Add(new GradientStop()
  274. {
  275. Color = ForegroundColorStart,
  276. Offset = 3
  277. });
  278. gradient.GradientStops.Add(new GradientStop()
  279. {
  280. Color = ForegroundColorEnd
  281. });
  282. return gradient;
  283. }
  284. private void SetBackgroundGradient()
  285. {
  286. var gradient = new LinearGradientBrush();
  287. gradient.StartPoint = new Point(0.5f, 0);
  288. gradient.EndPoint = new Point(0.5f, 1);
  289. if (IsUniqueBackgroundColor)
  290. {
  291. gradient.GradientStops.Add(new GradientStop() { Color = BackgroundColorStart, Offset = 3 });
  292. gradient.GradientStops.Add(new GradientStop() { Color = BackgroundColorStart });
  293. }
  294. else
  295. {
  296. gradient.GradientStops.Add(new GradientStop() { Color = BackgroundColorStart, Offset = 3 });
  297. gradient.GradientStops.Add(new GradientStop() { Color = BackgroundColorEnd });
  298. }
  299. this.Background = gradient;
  300. }
  301. private void SetTransformGroup(Captcha captcha)
  302. {
  303. this.SetRotate(ref captcha);
  304. this.SetScale(ref captcha);
  305. this.SetSkew(ref captcha);
  306. this.SetTranslate(ref captcha);
  307. this.SetFontColor(ref captcha);
  308. }
  309. private void SetRotate(ref Captcha captcha)
  310. {
  311. captcha.Rotate.Angle = random.Next(-10, 10);
  312. captcha.Rotate.CenterX = random.Next(1, 3);
  313. captcha.Rotate.CenterY = random.Next(1, 3);
  314. captcha.TransformGroup.Children.Add(captcha.Rotate);
  315. }
  316. private void SetScale(ref Captcha captcha)
  317. {
  318. captcha.Scale.ScaleX = random.Next(2, 3);
  319. captcha.Scale.ScaleY = random.Next(1, 2);
  320. //captcha.Scale.CenterX = random.Next(1,5)/10.0;
  321. //captcha.Scale.CenterY = random.Next(1, 5)/10.0;
  322. captcha.TransformGroup.Children.Add(captcha.Scale);
  323. }
  324. private void SetSkew(ref Captcha captcha)
  325. {
  326. captcha.Skew.AngleX = random.Next(-10, 10);
  327. captcha.Skew.AngleY = random.Next(-10, 10);
  328. captcha.Skew.CenterX = random.Next(1, 3);
  329. captcha.Skew.CenterY = random.Next(1, 3);
  330. captcha.TransformGroup.Children.Add(captcha.Skew);
  331. }
  332. private void SetTranslate(ref Captcha captcha)
  333. {
  334. captcha.Translate.X = random.Next(2, 4);
  335. captcha.Translate.Y = random.Next(1, 4);
  336. captcha.TransformGroup.Children.Add(captcha.Translate);
  337. }
  338. private void SetFontColor(ref Captcha captcha)
  339. {
  340. captcha.FontColor = Color.FromArgb(255,
  341. 0,
  342. 0,
  343. (byte)random.Next(0, 20));
  344. }
  345. }
  346. }