using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; namespace Volian.Controls.Library { public enum ExpanderStyle : int { Square, Round } public delegate void vlnExpanderEvent(object sender, vlnExpanderEventArgs args); public partial class vlnExpander : UserControl { #region Events public event vlnExpanderEvent BeforeExpand; private void OnBeforeExpand(object sender, vlnExpanderEventArgs args) { if (BeforeExpand != null) BeforeExpand(sender, args); } public event vlnExpanderEvent BeforeColapse; private void OnBeforeColapse(object sender, vlnExpanderEventArgs args) { if (BeforeColapse != null) BeforeColapse(sender, args); } public event vlnExpanderEvent AttachmentClick; private void OnAttachmentClick(object sender, vlnExpanderEventArgs args) { if (AttachmentClick != null) AttachmentClick(sender, args); } #endregion public vlnExpander() { InitializeComponent(); } private bool _Expanded; public bool Expanded { get { return _Expanded; } set { if (value) OnBeforeExpand(this, new vlnExpanderEventArgs()); else OnBeforeColapse(this, new vlnExpanderEventArgs()); _Expanded = value; this.Refresh(); } } public void ShowExpanded() { _Expanded = true; this.Refresh(); } private int _Trans1 = 128; public int Trans1 { get { return _Trans1; } set { _Trans1 = value; this.Refresh(); } } private int _Trans2 = 128; public int Trans2 { get { return _Trans2; } set { _Trans2 = value; this.Refresh(); } } private Color _Color1 = Color.Aquamarine; public Color Color1 { get { return _Color1; } set { _Color1 = value; this.Refresh(); } } private Color _Color2 = Color.Violet; public Color Color2 { get { return _Color2; } set { _Color2 = value; this.Refresh(); } } private int _PenWidth = 0; public int PenWidth { get { return _PenWidth; } set { _PenWidth = value; this.Refresh(); } } private int _WidthFactor=7; public int WidthFactor { get { return _WidthFactor; } set { _WidthFactor = value; this.Refresh(); } } private Color _BorderColor=Color.Silver; public Color BorderColor { get { return _BorderColor; } set { _BorderColor = value; this.Refresh(); } } private int _GradientAngle=45; public int GradientAngle { get { return _GradientAngle; } set { _GradientAngle = value; this.Refresh(); } } private bool _Attachment = false; public bool Attachment { get { return _Attachment; } set { _Attachment = value; this.Refresh(); } } private bool _AutoTOC = false; public bool AutoTOC { get { return _AutoTOC; } set { _AutoTOC = value; this.Refresh(); } } private ExpanderStyle _MyExpanderStyle = ExpanderStyle.Square; public ExpanderStyle MyExpanderStyle { get { return _MyExpanderStyle; } set { _MyExpanderStyle = value; this.Refresh(); } } private void vlnExpander_Paint(object sender, PaintEventArgs e) { Color c1 = Color.FromArgb(_Trans1, _Color1); Color c2 = Color.FromArgb(_Trans2, _Color2); Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, _GradientAngle); //Brush b2 = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c2, c2, _GradientAngle); Pen p = new Pen(ForeColor); Brush b2 = p.Brush; float smallest = this.Width; if (this.Height < this.Width) smallest = this.Height; float weight = (smallest - 1) / 12; float radius = (smallest - 1) / 2; float diameter = 2 * radius; if (this.Height <= diameter) diameter = this.Height - 1; e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; p = new Pen(_BorderColor, _PenWidth); int penWidth = ClientRectangle.Width / _WidthFactor; switch (_MyExpanderStyle) { case ExpanderStyle.Round: //e.Graphics.FillEllipse(Brushes.Gray, 0F, 0F, diameter, diameter); //e.Graphics.DrawRectangle(p, new Rectangle(0, 0, leg * 2 + penWidth * 3, leg * 2 + penWidth * 3)); // Draw a white cross-hair //e.Graphics.FillRectangle(Brushes.White, 2 * weight , radius - weight, diameter - 4 * weight, weight * 2); //e.Graphics.FillRectangle(Brushes.White, radius - weight, 2 * weight, weight * 2, diameter - 4 * weight); if(!Attachment) e.Graphics.FillEllipse(b, 0F, 0F, diameter, diameter); DrawInterior(e, b, b2, weight, radius, diameter); e.Graphics.DrawEllipse(p, 0, 0, diameter, diameter); p.Dispose(); b.Dispose(); b2.Dispose(); break; case ExpanderStyle.Square: //c1 = Color.FromArgb(_Trans1, _Color1); //c2 = Color.FromArgb(_Trans2, _Color2); //b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, _GradientAngle); int leg = ((ClientRectangle.Width - 3 * penWidth) / 2) - 1; if (!Attachment) e.Graphics.FillRectangle(b, new Rectangle(0, 0, leg * 2 + penWidth * 3, leg * 2 + penWidth * 3)); int center = leg + penWidth; int limit = 2 * (penWidth + leg); p = new Pen(_BorderColor, _PenWidth); e.Graphics.DrawRectangle(p, new Rectangle(0, 0, leg * 2 + penWidth * 3, leg * 2 + penWidth * 3)); //b = new SolidBrush(this.ForeColor); DrawInterior(e, b, b2, weight, radius, diameter); //e.Graphics.FillRectangle(b, new Rectangle(penWidth, center, leg * 2 + penWidth, penWidth)); //if (!_Expanded) e.Graphics.FillRectangle(b, new Rectangle(center, penWidth, penWidth, leg * 2 + penWidth)); ////pe.Graphics.DrawLine(p, new Point(penWidth, center), new Point(limit, center)); ////if (!Expanded) pe.Graphics.DrawLine(p, new Point(center, penWidth), new Point(center, limit)); p.Dispose(); b.Dispose(); break; } } private void DrawInterior(PaintEventArgs e, Brush bBackground, Brush bForeground, float weight, float radius, float diameter) { if (Attachment) { if (!AutoTOC) { PointF[] myArrow = new PointF[7]; myArrow[0] = new PointF(7 * weight, 3 * weight); myArrow[1] = new PointF(11 * weight, 6 * weight); myArrow[2] = new PointF(7 * weight, 9 * weight); myArrow[3] = new PointF(7 * weight, 7 * weight); myArrow[4] = new PointF(2 * weight, 7 * weight); myArrow[5] = new PointF(2 * weight, 5 * weight); myArrow[6] = new PointF(7 * weight, 5 * weight); e.Graphics.FillPolygon(bBackground, myArrow); } } else { e.Graphics.FillRectangle(bForeground, 2 * weight, radius - weight, diameter - 4 * weight, weight * 2); if (!_Expanded) e.Graphics.FillRectangle(bForeground, radius - weight, 2 * weight, weight * 2, diameter - 4 * weight); } } private void vlnExpander_Resize(object sender, EventArgs e) { this.Width = this.Height; this.Refresh(); } private void vlnExpander_MouseDown(object sender, MouseEventArgs e) { if (Attachment) OnAttachmentClick(sender, new vlnExpanderEventArgs()); else Expanded = !Expanded; } } public partial class vlnExpanderEventArgs : EventArgs { public vlnExpanderEventArgs() { ;} } }