using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JYControl
{
///
/// Base class for the IndustrialCtrls
///
public partial class LBIndustrialCtrlBase : UserControl
{
#region (* Constructor *)
public LBIndustrialCtrlBase()
{
InitializeComponent();
// Set the styles for drawing
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.DoubleBuffer |
ControlStyles.SupportsTransparentBackColor,
true);
// Transparent background
this.BackColor = Color.Transparent;
// Creation of the default renderer
this._defaultRenderer = CreateDefaultRenderer();
if (this._defaultRenderer != null)
this._defaultRenderer.Control = this;
}
#endregion
#region (* Properties *)
///
/// Default renderer of the control
///
private ILBRenderer _defaultRenderer = null;
[Browsable(false)]
public ILBRenderer DefaultRenderer
{
get { return this._defaultRenderer; }
}
///
/// User defined renderer
///
private ILBRenderer _renderer = null;
[Browsable(false)]
public ILBRenderer Renderer
{
set
{
// set the renderer
this._renderer = value;
if (this._renderer != null)
{
// Set the control tu the renderer
this._renderer.Control = this;
// Update the renderer
this._renderer.Update();
}
// Redraw the renderer
Invalidate();
}
get { return this._renderer; }
}
#endregion
#region (* Events delegates *)
///
/// Font change event
///
///
[System.ComponentModel.EditorBrowsableAttribute()]
protected override void OnFontChanged(EventArgs e)
{
// Calculate dimensions
this.CalculateDimensions();
}
///
/// SizeChanged event
///
///
[System.ComponentModel.EditorBrowsableAttribute()]
protected override void OnSizeChanged(EventArgs e)
{
// Default
base.OnSizeChanged(e);
// Calculate al the data for
// drawing the control
this.CalculateDimensions();
// Redraw
this.Invalidate();
}
///
/// Resize event
///
///
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
// Calculate al the data for
// drawing the control
this.CalculateDimensions();
// Redraw
this.Invalidate();
}
///
/// Paint event
///
///
[System.ComponentModel.EditorBrowsableAttribute()]
protected override void OnPaint(PaintEventArgs e)
{
// Rectangle of the control
RectangleF _rc = new RectangleF(0, 0, this.Width, this.Height);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// Call the default renderer if the user
// rendere is null
if (this.Renderer == null)
{
this.DefaultRenderer.Draw(e.Graphics);
return;
}
// Draw with the user renderer
this.Renderer.Draw(e.Graphics);
}
#endregion
#region (* Virtual method *)
///
/// Call from the constructor to create the default renderer
///
///
protected virtual ILBRenderer CreateDefaultRenderer()
{
return new LBRendererBase();
}
///
/// Calculate the dimensions of the control
///
protected virtual void CalculateDimensions()
{
this.DefaultRenderer.Update();
// Update the data in the renderer
if (this.Renderer != null)
this.Renderer.Update();
this.Invalidate();
}
#endregion
}
///
/// Base class for the controls renderer
///
public class LBRendererBase : ILBRenderer
{
#region (* Constructor *)
public LBRendererBase()
{
}
#endregion
#region (* IDisposable implementation *)
public void Dispose()
{
this.OnDispose();
}
#endregion
#region (* Properties *)
///
/// Associated control
///
protected object _control = null;
public object Control
{
set { this._control = value; }
get { return this._control; }
}
#endregion
#region (* Virtual methods *)
///
/// Dispose the resource of the object
///
public virtual void OnDispose()
{
}
///
/// Update the renderer
///
///
public virtual bool Update()
{
return false;
}
///
/// Drawing method
///
///
public virtual void Draw(Graphics Gr)
{
// Check the graphics
if (Gr == null)
throw new ArgumentNullException("Gr");
// Check the control
Control ctrl = this.Control as Control;
if (ctrl == null)
throw new NullReferenceException("Associated control is not valid");
// Default drawing
Rectangle rc = ctrl.Bounds;
Gr.FillRectangle(Brushes.White, ctrl.Bounds);
Gr.DrawRectangle(Pens.Black, ctrl.Bounds);
Gr.DrawLine(Pens.Red,
ctrl.Left,
ctrl.Top,
ctrl.Right,
ctrl.Bottom);
Gr.DrawLine(Pens.Red,
ctrl.Right,
ctrl.Top,
ctrl.Left,
ctrl.Bottom);
}
#endregion
}
}