/// /// Component: SnapDriveSnapIn /// File Name: ScrollControl.cs /// Author: /// Creation Date: 2008/05/12 /// Language: C# /// Description: This file contains ScrollControl class which is being used to perform different scrolling operations. /// Exceptions: /// Precondition: N.A. /// See Also: http://www.codeproject.com/KB/miscctrl/scrollingbox.aspx /// Notes: Removed unwanted code from class ScrollControl, modified the code as per NetApp naming convention. /// 1. Easter egg credit list /// 2. removed the customization dialog from create/connect wizard /// 3. removed the Network appliance -> netapp from MMC controls /// /// using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Drawing; using System.ComponentModel; namespace SnapDriveSnapIn { [ToolboxBitmap("ScrollBoxToolIcon")] public class ScrollControl : System.Windows.Forms.Control { private System.Timers.Timer timer; private ArrowDirection movingDirection; private ScrollingBoxCollection items; private StringFormat stringFormat; private bool startingPositionHasBeenSetAfterHeight; public ScrollControl() { SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer // Change this to ControlStyles.DoubleBuffer if not using .NET v2.0 | ControlStyles.AllPaintingInWmPaint, true); items = new ScrollingBoxCollection(); items.OnCollectionChanged += new EventHandler(items_OnCollectionChanged); movingDirection = ArrowDirection.Up; stringFormat = new StringFormat(); startingPositionHasBeenSetAfterHeight = false; timer = new System.Timers.Timer(); timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); timer.Interval = 25; timer.Enabled = true; } private void RecalculateItems() { SizeF sizeF = new SizeF(); Graphics g = this.CreateGraphics(); int availWidth = this.Width - this.Padding.Left - this.Padding.Right; for (int i = 0; i < items.Count; i++) { ScrollBoxItem item = items[i]; item.rectF.X = this.Padding.Left; item.rectF.Width = (float)availWidth; if (item.GetType() == typeof(ScrollingBoxText)) { ScrollingBoxText textItem = (ScrollingBoxText)item; sizeF = g.MeasureString(textItem.Text, this.Font, availWidth, stringFormat); } else if (item.GetType() == typeof(ScrollingBoxImage)) { ScrollingBoxImage imgItem = (ScrollingBoxImage)item; sizeF = imgItem.Image.Size; imgItem.rectF.Width = sizeF.Width; switch (Alignment) { case StringAlignment.Near: item.rectF.X = this.Padding.Left; break; case StringAlignment.Center: item.rectF.X = (availWidth / 2) - (sizeF.Width / 2) + Padding.Left; break; case StringAlignment.Far: item.rectF.X = this.Width - sizeF.Width - this.Padding.Right; break; } } item.rectF.Height = sizeF.Height; if (i == 0) { if (startingPositionHasBeenSetAfterHeight == false) { item.rectF.Y = this.Height; startingPositionHasBeenSetAfterHeight = true; } } else { item.rectF.Y = (float)items[i - 1].rectF.Y + items[i - 1].rectF.Height; } } } private void PositionItems() { for (int i = 0; i < items.Count; i++) { ScrollBoxItem item = items[i]; if (movingDirection == ArrowDirection.Up) { if (item.rectF.Y + item.rectF.Height < 0) { if (i == 0) { // Goto the bottom of the screen list items item.rectF.Y = items[items.Count - 1].rectF.Y + this.Height + item.rectF.Height; } else { item.rectF.Y = items[i - 1].rectF.Y + items[i - 1].rectF.Height; } } else { // Move up the screen item.rectF.Y -= 1; } } else if (movingDirection == ArrowDirection.Down) { if (item.rectF.Y > this.Height) { if (i == items.Count - 1) { // Goto the top of the screen list items item.rectF.Y = items[0].rectF.Y - this.Height - item.rectF.Height; } else { item.rectF.Y = items[i + 1].rectF.Y - item.rectF.Height; } } else { // Move down the screen item.rectF.Y += 1; } } } this.Invalidate(); } void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { PositionItems(); } protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; SolidBrush textBrush = new SolidBrush(this.ForeColor); RectangleF clipRectF = new RectangleF(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height); for(int i = 0; i < items.Count; i++) { ScrollBoxItem item = items[i]; if (clipRectF.IntersectsWith(item.rectF)) { if (item.GetType() == typeof(ScrollingBoxText)) { g.DrawString(((ScrollingBoxText)item).Text, this.Font, textBrush, item.rectF, stringFormat); } else if (item.GetType() == typeof(ScrollingBoxImage)) { g.DrawImage(((ScrollingBoxImage)item).Image, item.rectF); } } } // Draw a border ControlPaint.DrawBorder(g, this.ClientRectangle, ControlPaint.Dark(this.BackColor), ButtonBorderStyle.Solid); } [Browsable(false)] public ScrollingBoxCollection Items { get { return items; } set { items = value; } } [Browsable(true), DefaultValue(StringAlignment.Far)] public StringAlignment Alignment { get { return stringFormat.Alignment; } set { stringFormat.Alignment = value; RecalculateItems(); this.Invalidate(); } } void items_OnCollectionChanged(object sender, EventArgs e) { RecalculateItems(); } private void InitializeComponent() { this.SuspendLayout(); this.ResumeLayout(false); } } public class ScrollBoxItem { internal System.Drawing.RectangleF rectF; public ScrollBoxItem() { rectF = new System.Drawing.RectangleF(0, 0, 0, 0); } } public class ScrollingBoxText : ScrollBoxItem { private string text; public ScrollingBoxText(String Text) : base() { text = Text; } public string Text { get { return text; } set { text = value; } } } public class ScrollingBoxImage : ScrollBoxItem { private System.Drawing.Image img; public ScrollingBoxImage(System.Drawing.Image Image) : base() { img = Image; } public System.Drawing.Image Image { get { return img; } set { img = value; } } } public class ScrollingBoxCollection : System.Collections.CollectionBase { public event EventHandler OnCollectionChanged; public ScrollingBoxCollection() { } public int Add(ScrollBoxItem value) { int index = base.InnerList.Add(value); if (OnCollectionChanged != null) { OnCollectionChanged(this, new EventArgs()); } return index; } public int Add(string Text) { return this.Add(new ScrollingBoxText(Text)); } public void InsertAt(int index, ScrollBoxItem value) { base.InnerList.Insert(index, value); if (OnCollectionChanged != null) { OnCollectionChanged(this, new EventArgs()); } } public void Remove(ScrollBoxItem value) { base.InnerList.Remove(value); if (OnCollectionChanged != null) { OnCollectionChanged(this, new EventArgs()); } } public ScrollBoxItem this[int index] { get { return (ScrollBoxItem)base.InnerList[index]; } set { base.InnerList[index] = value; } } } }