К примеру, можно наследовать Panel... MyControl.cs - движок
| Код | using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms;
namespace MyControl { public class MyRollingPanel : System.Windows.Forms.Panel { #region Enums public enum Conditions { RolledDown, RolledUp } #endregion
#region Props'n'Fields private System.ComponentModel.Container components = null;
private System.Windows.Forms.Timer RollingTimer;
private int MouseX; private int MouseY; private int MouseXOnDown; private int MouseYOnDown; private bool Moveable = false;
private Color pri_HeaderBackground; [Category("Appearance"), Description("Цвет шапки")] public Color HeaderBackground { get { return this.pri_HeaderBackground; } set { this.pri_HeaderBackground = value; this.Refresh(); } }
private string pri_Caption; [Category("Appearance"), Description("Заголовок")] public string Caption { get { return this.pri_Caption; } set { this.pri_Caption = value; this.Refresh(); } }
[DefaultValue(25)] private int pri_HeaderHight; [Category("Appearance"), Description("Высота шапки")] public int HeaderHight { get { return this.pri_HeaderHight; } set { this.pri_HeaderHight = value; this.DockPadding.Top = value; this.Refresh(); } }
public Rectangle HeaderRect { get { return new Rectangle(this.ClientRectangle.X, this.ClientRectangle.Y, this.ClientRectangle.Width, this.pri_HeaderHight); } }
[DefaultValue(Conditions.RolledUp)] private Conditions pri_Condition; public Conditions Condition { get { return this.pri_Condition; } set { this.pri_Condition = value; this.Refresh(); } }
private int pri__RolledUpHight; private int pri_RolledUpHight { get { return this.pri__RolledUpHight; } } #endregion
#region Constructor public MyRollingPanel() { InitializeComponent(); } #endregion
#region Overriden Methods
protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); }
protected override void OnPaint(PaintEventArgs e) { Painter.DrawRolligPanel(this, e.Graphics); }
protected override void OnMouseDown(MouseEventArgs e) { if(e.Button == MouseButtons.Left) this.Moveable = true;
this.MouseXOnDown = e.X; this.MouseYOnDown = e.Y; base.OnMouseDown (e); }
protected override void OnMouseMove(MouseEventArgs e) { this.MouseX = e.X; this.MouseY = e.Y;
if(this.Moveable) { int Xstart, Ystart; int Xoffset, Yoffset;
Xstart = this.Left; Ystart = this.Top; Xoffset = e.X - this.MouseXOnDown; Yoffset = e.Y - this.MouseYOnDown;
this.Left += Xoffset; this.Top += Yoffset; }
base.OnMouseMove (e); }
protected override void OnMouseUp(MouseEventArgs e) { this.Moveable = false; base.OnMouseUp (e); }
protected override void OnDoubleClick(EventArgs e) { Rectangle FakeMouseRect = new Rectangle(this.MouseX, this.MouseY, 1, 1); if(FakeMouseRect.IntersectsWith(this.HeaderRect)) { if(this.Condition == Conditions.RolledUp) { this.RollMeDown(); } else this.RollMeUp(); } base.OnDoubleClick (e); }
#endregion
#region Private Methods private void RollMeDown() { this.RollingTimer.Stop(); this.pri__RolledUpHight = this.Height; this.Condition = Conditions.RolledDown; this.RollingTimer.Start(); }
private void RollMeUp() { this.Condition = Conditions.RolledUp; this.RollingTimer.Start(); }
private void RollingTimer_Tick(object sender, System.EventArgs e) { int D = 20; if(this.Condition == Conditions.RolledDown) { if(this.Height > this.HeaderHight) { if(this.Height - D < this.HeaderHight) this.Height = this.HeaderHight; else this.Height-=D; } else this.RollingTimer.Stop(); } else if(this.Condition == Conditions.RolledUp) { if(this.Height < this.pri__RolledUpHight) this.Height+=D; else this.RollingTimer.Stop(); } this.Refresh(); }
private void InitializeComponent() { SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.ResizeRedraw, true); SetStyle(ControlStyles.DoubleBuffer, true);
this.pri_HeaderHight = 15; this.DockPadding.Top = this.pri_HeaderHight; this.pri_Condition = Conditions.RolledDown; this.pri__RolledUpHight = this.ClientRectangle.Height;
components = new System.ComponentModel.Container(); this.RollingTimer = new System.Windows.Forms.Timer(this.components); this.RollingTimer.Interval = 1; this.RollingTimer.Tick += new System.EventHandler(this.RollingTimer_Tick); } #endregion } }
|
Painter.cs - класс занимающийся отрисовкой контрола
| Код | using System; using System.Drawing;
namespace MyControl { public class Painter { #region Rolling Panel Methods
public static void DrawRolligPanel(MyRollingPanel rlpnl, Graphics grfx) { Rectangle tmpRect = new Rectangle(rlpnl.ClientRectangle.X, rlpnl.ClientRectangle.Y, rlpnl.ClientRectangle.Width-1, rlpnl.ClientRectangle.Height-1);
grfx.FillRectangle(new SolidBrush(rlpnl.HeaderBackground), rlpnl.HeaderRect);
grfx.DrawRectangle(new Pen(new SolidBrush(Color.Black)), tmpRect);
Painter.DrawRollingPanelCaption(rlpnl, rlpnl.Caption, grfx); }
private static void DrawRollingPanelCaption(MyRollingPanel rlpnl, string txt, Graphics grfx) { SizeF szfStr = grfx.MeasureString(txt, rlpnl.Font);
int txtHeight = (int)szfStr.Height;
int YOffset;
StringFormat strfmt = new StringFormat();
strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;
Rectangle HeaderRect = new Rectangle(rlpnl.ClientRectangle.Left, rlpnl.ClientRectangle.Top, rlpnl.ClientRectangle.Width, rlpnl.HeaderHight);
YOffset = (HeaderRect.Height - txtHeight)/2; if(YOffset < 0) YOffset = 0; Rectangle txtShow = new Rectangle(HeaderRect.X, HeaderRect.Y + YOffset, rlpnl.ClientRectangle.Width, txtHeight);
grfx.DrawString(" "+txt, rlpnl.Font, new SolidBrush(rlpnl.ForeColor), txtShow, strfmt); } #endregion } }
|
|