PdnBanner.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Drawing.Imaging;
  5. using System.Windows.Forms;
  6. namespace PaintDotNet
  7. {
  8. public sealed class PdnBanner
  9. : Control
  10. {
  11. private System.Windows.Forms.PictureBox bannerImage;
  12. private System.Windows.Forms.Label bannerText;
  13. // for awhile there, this control was designed to fade between several different
  14. // images. for 3.0 this did not end up being the case, but the functionality is
  15. // still here
  16. private System.Windows.Forms.Timer bannerTimer;
  17. private static readonly Size defaultSize = new Size(600, 400);
  18. protected override Size DefaultSize
  19. {
  20. get
  21. {
  22. return defaultSize;
  23. }
  24. }
  25. public string BannerText
  26. {
  27. get
  28. {
  29. return this.bannerText.Text;
  30. }
  31. set
  32. {
  33. this.bannerText.Text = value;
  34. }
  35. }
  36. public Font BannerFont
  37. {
  38. get
  39. {
  40. return (Font)this.bannerText.Font.Clone();
  41. }
  42. set
  43. {
  44. this.bannerText.Font = (Font)value.Clone();
  45. }
  46. }
  47. private void BannerTimer_Tick(object sender, EventArgs e)
  48. {
  49. Form findForm = FindForm();
  50. if (findForm != null &&
  51. findForm.WindowState != FormWindowState.Minimized)
  52. {
  53. const int bannerUpDuration = 4000;
  54. const int bannerFadeDuration = 2000;
  55. const int bannerPeriod = bannerUpDuration + bannerFadeDuration;
  56. int ticks = unchecked(Environment.TickCount - this.firstTick + bannerUpDuration / 2);
  57. int localTick = ticks % bannerPeriod;
  58. double a;
  59. if (localTick < bannerUpDuration)
  60. {
  61. a = 1.0;
  62. }
  63. else
  64. {
  65. int fadeTick = localTick - bannerUpDuration;
  66. a = (double)(bannerFadeDuration - fadeTick) / (double)bannerFadeDuration;
  67. a = 1.0 - a;
  68. a = a * a;
  69. a = 1.0 - a;
  70. }
  71. int newBannerIndex = ticks / bannerPeriod;
  72. float newBannerAlpha = (float)a;
  73. if (banners.Length < 2 || SystemLayer.UserSessions.IsRemote)
  74. {
  75. newBannerAlpha = 1.0f;
  76. }
  77. if (newBannerAlpha != this.bannerAlpha ||
  78. newBannerIndex != this.bannerIndex)
  79. {
  80. this.bannerAlpha = newBannerAlpha;
  81. this.bannerIndex = newBannerIndex;
  82. SetUpBannerImage();
  83. }
  84. }
  85. }
  86. private int indexOffset;
  87. private int firstTick = Environment.TickCount;
  88. private Image pdnLogo = PdnResources.GetImageResource("Images.TransparentLogo.png").GetCopy();
  89. private int bannerIndex = 0;
  90. private float bannerAlpha = 1.0f;
  91. private Bitmap logoAndGradient = new Bitmap(600, 400, PixelFormat.Format24bppRgb);
  92. private Bitmap highQualityBmp = null;
  93. private Image[] banners = new Image[]
  94. {
  95. PdnResources.GetImageResource("Images.Banner1.jpg").GetCopy(),
  96. };
  97. private void SetUpBannerImage()
  98. {
  99. Image banner1 = this.banners[(this.bannerIndex + this.indexOffset) % this.banners.Length];
  100. Image banner2 = this.banners[(this.bannerIndex + 1 + this.indexOffset) % this.banners.Length];
  101. using (Graphics g = Graphics.FromImage(this.logoAndGradient))
  102. {
  103. g.Clear(Color.White);
  104. Rectangle gradientSrcBounds = new Rectangle(
  105. new Point(0, 0),
  106. banner2.Size);
  107. Rectangle gradientDstBounds = new Rectangle(
  108. new Point(logoAndGradient.Width - banner2.Width, 0),
  109. banner2.Size);
  110. float alpha1 = this.bannerAlpha;
  111. float alpha2 = 1.0f - alpha1;
  112. ColorMatrix cm1 = new ColorMatrix(
  113. new float[][]
  114. {
  115. new float[] { 1, 0, 0, 0, 0 },
  116. new float[] { 0, 1, 0, 0, 0 },
  117. new float[] { 0, 0, 1, 0, 0 },
  118. new float[] { 0, 0, 0, alpha1, 0 },
  119. new float[] { 0, 0, 0, 0, 1 }
  120. });
  121. ImageAttributes ia1 = new ImageAttributes();
  122. ia1.SetColorMatrix(cm1);
  123. ColorMatrix cm2 = new ColorMatrix(
  124. new float[][]
  125. {
  126. new float[] { 1, 0, 0, 0, 0 },
  127. new float[] { 0, 1, 0, 0, 0 },
  128. new float[] { 0, 0, 1, 0, 0 },
  129. new float[] { 0, 0, 0, alpha2, 0 },
  130. new float[] { 0, 0, 0, 0, 1 }
  131. });
  132. ImageAttributes ia2 = new ImageAttributes();
  133. ia2.SetColorMatrix(cm2);
  134. if (banner1 != null)
  135. {
  136. float inflateAmt1X = 0; // 1500.0f - (alpha1 * 1500.0f);
  137. float inflateAmt1Y = 0; // (inflateAmt1X * (float)banner1.Height) / (float)banner2.Width;
  138. RectangleF dstRect1 = new RectangleF(
  139. gradientDstBounds.X - inflateAmt1X * 2 + (inflateAmt1X / 150.0f),
  140. gradientDstBounds.Y - (inflateAmt1Y * 3) / 2,
  141. gradientDstBounds.Width + (2 * inflateAmt1X),
  142. gradientDstBounds.Height + (2 * inflateAmt1Y));
  143. g.DrawImage(
  144. banner1,
  145. new PointF[]
  146. {
  147. dstRect1.Location,
  148. new PointF(dstRect1.Right, dstRect1.Top),
  149. new PointF(dstRect1.Left, dstRect1.Bottom)
  150. },
  151. gradientSrcBounds,
  152. GraphicsUnit.Pixel,
  153. ia1);
  154. }
  155. float inflateAmt2X = 0; // 1500.0f - (alpha2 * 1500.0f);
  156. float inflateAmt2Y = 0; // (inflateAmt2X * (float)banner2.Height) / (float)banner2.Width;
  157. RectangleF dstRect2 = new RectangleF(
  158. gradientDstBounds.X - inflateAmt2X * 2 + (inflateAmt2X / 150.0f),
  159. gradientDstBounds.Y - (inflateAmt2Y * 3) / 2,
  160. gradientDstBounds.Width + (2 * inflateAmt2X),
  161. gradientDstBounds.Height + (2 * inflateAmt2Y));
  162. g.DrawImage(
  163. banner2,
  164. new PointF[]
  165. {
  166. dstRect2.Location,
  167. new PointF(dstRect2.Right, dstRect2.Top),
  168. new PointF(dstRect2.Left, dstRect2.Bottom)
  169. },
  170. (RectangleF)gradientSrcBounds,
  171. GraphicsUnit.Pixel,
  172. ia2);
  173. Rectangle pdnLogoBounds = new Rectangle(new Point(0, 0), pdnLogo.Size);
  174. g.DrawImage(pdnLogo, pdnLogoBounds, pdnLogoBounds, GraphicsUnit.Pixel);
  175. ia1.Dispose();
  176. ia1 = null;
  177. ia2.Dispose();
  178. ia2 = null;
  179. }
  180. Bitmap useThis;
  181. if (this.bannerImage.Size == logoAndGradient.Size)
  182. {
  183. useThis = logoAndGradient;
  184. }
  185. else
  186. {
  187. if (this.highQualityBmp == null)
  188. {
  189. this.highQualityBmp = new Bitmap(
  190. this.bannerImage.Width,
  191. this.bannerImage.Height,
  192. PixelFormat.Format24bppRgb);
  193. }
  194. using (Graphics g = Graphics.FromImage(this.highQualityBmp))
  195. {
  196. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  197. g.DrawImage(
  198. logoAndGradient,
  199. new Rectangle(0, 0, this.highQualityBmp.Width, this.highQualityBmp.Height),
  200. new Rectangle(0, 0, logoAndGradient.Width, logoAndGradient.Height),
  201. GraphicsUnit.Pixel);
  202. }
  203. useThis = this.highQualityBmp;
  204. }
  205. this.bannerImage.Image = useThis;
  206. }
  207. public PdnBanner()
  208. {
  209. InitializeComponent();
  210. this.indexOffset = new Random().Next(this.banners.Length);
  211. SetUpBannerImage();
  212. }
  213. protected override void Dispose(bool disposing)
  214. {
  215. if (disposing)
  216. {
  217. if (this.bannerTimer != null)
  218. {
  219. this.bannerTimer.Dispose();
  220. this.bannerTimer = null;
  221. }
  222. if (this.pdnLogo != null)
  223. {
  224. this.pdnLogo.Dispose();
  225. this.pdnLogo = null;
  226. }
  227. if (this.logoAndGradient != null)
  228. {
  229. this.logoAndGradient.Dispose();
  230. this.logoAndGradient = null;
  231. }
  232. if (this.highQualityBmp != null)
  233. {
  234. this.highQualityBmp.Dispose();
  235. this.highQualityBmp = null;
  236. }
  237. if (this.banners != null)
  238. {
  239. foreach (Image image in this.banners)
  240. {
  241. if (image != null)
  242. {
  243. image.Dispose();
  244. }
  245. }
  246. this.banners = null;
  247. }
  248. }
  249. base.Dispose(disposing);
  250. }
  251. private void InitializeComponent()
  252. {
  253. this.bannerImage = new System.Windows.Forms.PictureBox();
  254. this.bannerText = new System.Windows.Forms.Label();
  255. this.bannerTimer = new System.Windows.Forms.Timer();
  256. ((System.ComponentModel.ISupportInitialize)(this.bannerImage)).BeginInit();
  257. this.SuspendLayout();
  258. //
  259. // bannerImage
  260. //
  261. this.bannerImage.BackColor = System.Drawing.Color.White;
  262. this.bannerImage.Dock = System.Windows.Forms.DockStyle.Top;
  263. this.bannerImage.Location = new System.Drawing.Point(0, 0);
  264. this.bannerImage.Name = "headerImage";
  265. this.bannerImage.Size = defaultSize;
  266. this.bannerImage.SizeChanged += new EventHandler(BannerImage_SizeChanged);
  267. this.bannerImage.SizeMode = PictureBoxSizeMode.CenterImage;
  268. this.bannerImage.TabIndex = 0;
  269. this.bannerImage.TabStop = false;
  270. this.bannerImage.Controls.Add(this.bannerText);
  271. //
  272. // bannerText
  273. //
  274. this.bannerText.BackColor = System.Drawing.Color.Transparent;
  275. this.bannerText.ForeColor = System.Drawing.Color.Black;
  276. this.bannerText.Font = Utility.CreateFont("Tahoma", 10.0f, FontStyle.Regular);
  277. this.bannerText.Location = new System.Drawing.Point(70, 47);
  278. this.bannerText.Name = "headingText";
  279. this.bannerText.Size = new System.Drawing.Size(441, 25);
  280. this.bannerText.TabIndex = 4;
  281. this.bannerText.Text = "headingText";
  282. this.bannerText.Visible = false;
  283. //
  284. // bannerTimer
  285. //
  286. this.bannerTimer.Interval = 30;
  287. this.bannerTimer.Tick += new EventHandler(BannerTimer_Tick);
  288. this.bannerTimer.Enabled = true;
  289. //
  290. // PdnBanner
  291. //
  292. this.Controls.Add(this.bannerImage);
  293. this.Name = "PdnBanner";
  294. ((System.ComponentModel.ISupportInitialize)(this.bannerImage)).EndInit();
  295. ResumeLayout();
  296. PerformLayout();
  297. }
  298. private void BannerImage_SizeChanged(object sender, EventArgs e)
  299. {
  300. SetUpBannerImage();
  301. }
  302. }
  303. }