your HarrProgressBar has some memory leaks. but dont wory new code is here:
---------------HarrProgressBar.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Plasmoid.Extensions;
using System.Drawing.Drawing2D;
using System.Diagnostics;
using System.ComponentModel;
namespace Harr
{
public class HarrProgressBar : Panel
{
private Rectangle leftArea;
private Rectangle statusArea;
private Rectangle mainArea;
private Rectangle mainAreaBackground;
private Rectangle rightArea;
public int RoundedCornerAngle { get; set; }
public int LeftBarSize { get; set; }
public int RightBarSize { get; set; }
public int StatusBarSize { get; set; }
//public Padding Padding { get; set; }
//public Font Font { get; set; }
public string MainText { get; set; }
public string LeftText { get; set; }
public string RightText { get; set; }
public string StatusText { get; set; }
private Color StatusColor1;
private Color StatusColor2;
private int _StatusBarColorIndex;
/// <summary>
/// ColorIndex. [0 - Raw active] [1 - Raw inactive] [2 - Dry active] [3 - Dry inactive].
/// </summary>
public int StatusBarColor
{
get { return _StatusBarColorIndex; }
set
{
switch (value)
{
case 0:
// Raw active
StatusColor1 = Color.OliveDrab;
StatusColor2 = Color.DarkOliveGreen;
break;
case 1:
// Raw inactive
StatusColor1 = Color.OliveDrab;
StatusColor2 = Color.Gray;
break;
case 2:
// Dry active
StatusColor1 = Color.Goldenrod;
StatusColor2 = Color.DarkGoldenrod;
break;
case 3:
// Dry inactive
StatusColor1 = Color.Goldenrod;
StatusColor2 = Color.Gray;
break;
default:
StatusColor1 = Color.DimGray;
StatusColor2 = Color.DimGray;
break;
}
}
}
private Color FirstColor;
private Color SecondColor;
private int _FillDegree = 50;
public int FillDegree
{
get { return _FillDegree; }
set
{
if (value >= 100)
{
FirstColor = Color.Red;
SecondColor = Color.DarkRed;
}
else if (value > 90)
{
FirstColor = Color.Orange;
SecondColor = Color.DarkOrange;
}
else if (value > 80)
{
FirstColor = Color.Gold;
SecondColor = Color.DarkGoldenrod;
}
else
{
FirstColor = Color.Green;
SecondColor = Color.DarkGreen;
}
_FillDegree = value;
}
}
//Check radius for begin drag n drop
public bool AllowDrag { get; set; }
private bool _isDragging = false;
private int _DDradius = 40;
private int _mX=0;
private int _mY=0;
public HarrProgressBar()
{
//Font = new Font("Arial", 10);
FillDegree = 50;
RoundedCornerAngle = 10;
Margin = new Padding(0);
LeftText = "LT";
StatusText = "Not set";
MainText = "MainText";
RightText = "RT";
LeftBarSize = 30;
StatusBarSize = 60;
RightBarSize = 30;
StatusBarColor = 99;
AllowDrag = true;
}
protected override void OnResize(EventArgs eventargs)
{
base.OnResize(eventargs);
ReCalcAreas();
}
private void ReCalcAreas()
{
leftArea = GetLeftArea();
statusArea = GetStatusArea();
mainArea = GetMainArea();
mainAreaBackground = GetMainAreaBackground();
rightArea = GetRightArea();
}
protected override void OnGotFocus(EventArgs e)
{
this.BackColor = SystemColors.ActiveCaption;
//Color.SandyBrown;
base.OnGotFocus(e);
}
protected override void OnLostFocus(EventArgs e)
{
this.BackColor = this.FindForm().ContainsFocus ? Color.Transparent : SystemColors.InactiveCaption;
base.OnLostFocus(e);
}
protected override void OnClick(EventArgs e)
{
this.Focus();
base.OnClick(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
this.Focus();
base.OnMouseDown(e);
_mX = e.X;
_mY = e.Y;
this._isDragging = false;
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (!_isDragging)
{
// This is a check to see if the mouse is moving while pressed.
// Without this, the DragDrop is fired directly when the control is clicked, now you have to drag a few pixels first.
if (e.Button == MouseButtons.Left && _DDradius > 0 && this.AllowDrag)
{
int num1 = _mX - e.X;
int num2 = _mY - e.Y;
if (((num1 * num1) + (num2 * num2)) > _DDradius)
{
DoDragDrop(this, DragDropEffects.All);
_isDragging = true;
return;
}
}
base.OnMouseMove(e);
}
}
protected override void OnMouseUp(MouseEventArgs e)
{
_isDragging = false;
base.OnMouseUp(e);
}
protected override void OnCreateControl()
{
this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint
| ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw
| ControlStyles.Selectable | ControlStyles.CacheText
| ControlStyles.SupportsTransparentBackColor, true);
this.DoubleBuffered = true;
base.OnCreateControl();
ReCalcAreas();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
using (LinearGradientBrush _LeftAndRightBrush = new LinearGradientBrush(mainArea, Color.DimGray, Color.Black, LinearGradientMode.Vertical)
, _StatusBrush = new LinearGradientBrush(mainArea, StatusColor1, StatusColor2, LinearGradientMode.Vertical)
, _MainBrush = new LinearGradientBrush(mainArea, FirstColor, SecondColor, LinearGradientMode.Vertical))
{
using (StringFormat f = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center })
{
// Draw left
if (LeftBarSize > 0)
{
g.FillRoundedRectangle(_LeftAndRightBrush, leftArea, this.RoundedCornerAngle, RectangleEdgeFilter.TopLeft | RectangleEdgeFilter.BottomLeft);
g.DrawString(this.LeftText, this.Font, Brushes.White, leftArea, f);
}
// Draw status
if (StatusBarSize > 0)
{
g.FillRoundedRectangle(_StatusBrush, statusArea, this.RoundedCornerAngle, RectangleEdgeFilter.None);
g.DrawString(this.StatusText, this.Font, Brushes.White, statusArea, f);
}
// Draw main background
g.FillRoundedRectangle(Brushes.DimGray, mainAreaBackground, this.RoundedCornerAngle, RectangleEdgeFilter.None);
// Draw main
g.FillRoundedRectangle(_MainBrush, mainArea, this.RoundedCornerAngle, RectangleEdgeFilter.None);
g.DrawString(this.MainText, this.Font, Brushes.White, mainAreaBackground, f);
// Draw right
if (RightBarSize > 0)
{
g.FillRoundedRectangle(_LeftAndRightBrush, rightArea, this.RoundedCornerAngle, RectangleEdgeFilter.TopRight | RectangleEdgeFilter.BottomRight);
g.DrawString(this.RightText, this.Font, Brushes.White, rightArea, f);
}
}
}
}
private Rectangle GetLeftArea()
{
return new Rectangle(
Padding.Left,
Padding.Top,
LeftBarSize,
this.ClientRectangle.Height - Padding.Bottom - Padding.Top);
}
private Rectangle GetStatusArea()
{
return new Rectangle(
Padding.Left + LeftBarSize,
Padding.Top,
StatusBarSize,
this.ClientRectangle.Height - Padding.Bottom - Padding.Top);
}
private Rectangle GetMainArea()
{
return new Rectangle(
Padding.Left + LeftBarSize + StatusBarSize,
Padding.Top,
Convert.ToInt32(((this.ClientRectangle.Width - (Padding.Left + LeftBarSize + StatusBarSize + RightBarSize + Padding.Right)) * FillDegree) / 100),
this.ClientRectangle.Height - Padding.Bottom - Padding.Top);
}
private Rectangle GetMainAreaBackground()
{
return new Rectangle(
Padding.Left + LeftBarSize + StatusBarSize,
Padding.Top,
this.ClientRectangle.Width - (Padding.Left + LeftBarSize + StatusBarSize + RightBarSize + Padding.Right),
this.ClientRectangle.Height - Padding.Bottom - Padding.Top);
}
private Rectangle GetRightArea()
{
return new Rectangle(
this.ClientRectangle.Width - (RightBarSize + Padding.Right),
Padding.Top,
RightBarSize,
this.ClientRectangle.Height - Padding.Bottom - Padding.Top);
}
}
}
|