ControlC ControlC.com

[C#] TabControlDesigner 1.1.1

Views: 95Total Chars: 8,677
Created: 6/12/12 10:52:13PM ET
Self Destructs: Never
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing.Drawing2D;
using System.IO;

//-------------------------------------
//TabControlDesigner
//
//Creator: Eprouvez
//Date: 6/5/2012
//Updated: 6/6/2012
//Version: 1.1.1
//
//Credits to:
//Aeonhack
//mavamaarten
//
//Update:
//Fixed a bug where ColorHook wasn't properly working when the control was created
//--------------------------------------

class Bloom
{

public Bloom(string Name, Color Color)
{
_Name = Name;
_Value = Color;
}
public Bloom(string Name, int Red, int Green, int Blue)
{
_Name = Name;
_Value = Color.FromArgb(Red, Green, Blue);
}
public Bloom(string Name, int Alpha, int Red, int Green, int Blue)
{
_Name = Name;
_Value = Color.FromArgb(Alpha, Red, Green, Blue);
}

private string _Name;
public string Name {
get { return _Name; }
}

private Color _Value;
public Color Color {
get { return _Value; }
set { _Value = value; }
}

}

abstract class TabControlDesigner : TabControl
{

protected Graphics G;
protected MouseState State;

protected Point Coordinates;
public enum MouseState : int
{
None = 0,
Over = 1,
Down = 2
}

#region " Routines "

public TabControlDesigner()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
DoubleBuffered = true;
SizeMode = TabSizeMode.Fixed;
ItemSize = new Size(250, 26);
}

public void SetState(MouseState current)
{
State = current;
Invalidate();
}

#endregion

#region " Hooks "

protected abstract void TabPaint(int Index);

protected abstract void ColorHook();
protected virtual void PaintHook()
{
}

#endregion

#region " Properties "

private int _InactiveIconOpacity = 50;
public int InactiveIconOpacity {
get { return _InactiveIconOpacity; }
set {
if ((value == null))
value = 50;
if (value < 0)
value = 0;
if (value > 100)
value = 100;
_InactiveIconOpacity = value;
}
}

private Color _PanelColor;
public Color PanelColor {
get { return _PanelColor; }
set { _PanelColor = value; }
}

private Dictionary<string, Color> Items = new Dictionary<string, Color>();
public Bloom[] Colors {
get {
List<Bloom> T = new List<Bloom>();
Dictionary<string, Color>.Enumerator E = Items.GetEnumerator();

while (E.MoveNext()) {
T.Add(new Bloom(E.Current.Key, E.Current.Value));
}

return T.ToArray();
}
set {
foreach (Bloom B in value) {
if (Items.ContainsKey(B.Name))
Items[B.Name] = B.Color;
}

InvalidateCustimization();
ColorHook();
Invalidate();
}
}

private string _Customization;
public string Customization {
get { return _Customization; }
set {
if (value == _Customization)
return;

byte[] Data = null;
Bloom[] Items = Colors;

try {
Data = Convert.FromBase64String(value);
for (int I = 0; I <= Items.Length - 1; I++) {
Items[I].Color = Color.FromArgb(BitConverter.ToInt32(Data, I * 4));
}
} catch {
return;
}

_Customization = value;

Colors = Items;
ColorHook();
Invalidate();
}
}
#endregion

#region " Property Helpers "

//Credits to Aeonhack
protected Pen GetPen(string name)
{
return new Pen(Items[name]);
}
protected Pen GetPen(string name, float width)
{
return new Pen(Items[name], width);
}
protected SolidBrush GetBrush(string name)
{
return new SolidBrush(Items[name]);
}
protected Color GetColor(string name)
{
return Items[name];
}
protected void SetColor(string name, Color value)
{
if (Items.ContainsKey(name))
Items[name] = value;
else
Items.Add(name, value);
}
protected void SetColor(string name, byte r, byte g, byte b)
{
SetColor(name, Color.FromArgb(r, g, b));
}
protected void SetColor(string name, byte a, byte r, byte g, byte b)
{
SetColor(name, Color.FromArgb(a, r, g, b));
}
protected void SetColor(string name, byte a, Color value)
{
SetColor(name, Color.FromArgb(a, value));
}
private void InvalidateCustimization()
{
MemoryStream M = new MemoryStream(Items.Count * 4);

foreach (Bloom B in Colors) {
M.Write(BitConverter.GetBytes(B.Color.ToArgb), 0, 4);
}

M.Close();
_Customization = Convert.ToBase64String(M.ToArray());
}

#endregion

#region " Drawing Methods "

public SizeF Measure(string Text)
{
return G.MeasureString(Text, Font);
}
public SizeF Measure(string Text, Font Font)
{
return G.MeasureString(Text, Font);
}
public Point Center(string Text, Rectangle Area)
{
return Center(Text, Font, Area);
}
public Point Center(string Text, Font Font, Rectangle Area)
{
return Center(Text, Font, Area, 0, 0);
}
public Point Center(string Text, Font Font, Rectangle Area, int XOffset, int YOffset)
{
dynamic M = Measure(Text, Font);
return new Point(Convert.ToInt32(Area.X + Area.Width / 2 - M.Width / 2) + XOffset, Convert.ToInt32(Area.Y + Area.Height / 2 - M.Height / 2) + YOffset);
}
public Pen ToPen(Color color)
{
return new Pen(color);
}
public Pen ToPen(Color color, float width)
{
return new Pen(color, width);
}
public Brush ToBrush(Color color)
{
return new SolidBrush(color);
}
public Color RGB(int Red, int Green, int Blue)
{
return Color.FromArgb(Red, Green, Blue);
}
public Color ARGB(int Alpha, Color color)
{
return color.FromArgb(Alpha, color);
}
public Color ARGB(int Alpha, int Red, int Green, int Blue)
{
return Color.FromArgb(Alpha, Red, Green, Blue);
}
public Rectangle Shrink(Rectangle rectangle, int Offset)
{
return Shrink(rectangle, Offset, true);
}
public Rectangle Shrink(Rectangle rectangle, int Offset, bool CenterPoint)
{
dynamic O = CenterPoint == true ? Offset : 0;
dynamic R = new Rectangle(rectangle.X + O, rectangle.Y + O, rectangle.Width - Offset * 2, rectangle.Height - Offset * 2);
return R;
}
public Rectangle Enlarge(Rectangle rectangle, int Offset)
{
return Enlarge(rectangle, Offset, true);
}
public Rectangle Enlarge(Rectangle rectangle, int Offset, bool CenterPoint)
{
dynamic O = CenterPoint == true ? Offset : 0;
dynamic R = new Rectangle(rectangle.X - O, rectangle.Y - O, rectangle.Width + Offset * 2, rectangle.Height + Offset * 2);
return R;
}
public Image ImageOpacity(Bitmap Image, float Opacity)
{
Bitmap Result = new Bitmap(Image.Width, Image.Height, Imaging.PixelFormat.Format32bppArgb);
var _with1 = Image;
Opacity = Math.Min(Opacity, 100);
using (Imaging.ImageAttributes Attributes = new Imaging.ImageAttributes()) {
Imaging.ColorMatrix Matrix = new Imaging.ColorMatrix();
Matrix.Matrix33 = Opacity / 100f;
Attributes.SetColorMatrix(Matrix);
PointF[] Points = {
new Point(0, 0),
new Point(_with1.Width, 0),
new Point(0, _with1.Height)
};
using (Graphics I = Graphics.FromImage(Result)) {
I.Clear(Color.Transparent);
I.DrawImage(Image, Points, new RectangleF(Point.Empty, _with1.Size), GraphicsUnit.Pixel, Attributes);
}
}
return Result;
}

#endregion

#region " Override Methods "

protected override void CreateHandle()
{
base.CreateHandle();
base.DoubleBuffered = true;
InvalidateCustimization();
ColorHook();
SizeMode = TabSizeMode.Fixed;
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
}

protected override void OnHandleCreated(System.EventArgs e)
{
ColorHook();
base.OnHandleCreated(e);
}

protected override void OnMouseEnter(System.EventArgs e)
{
SetState(MouseState.Over);
base.OnMouseHover(e);
}
protected override void OnMouseLeave(System.EventArgs e)
{
SetState(MouseState.None);
for (int I = 0; I <= TabPages.Count - 1; I++) {
if (TabPages(I).DisplayRectangle.Contains(Coordinates)) {
base.Invalidate();
break; // TODO: might not be correct. Was : Exit For
}
}
base.OnMouseHover(e);
}
protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
{
Coordinates = e.Location;
for (int I = 0; I <= TabPages.Count - 1; I++) {
if (TabPages(I).DisplayRectangle.Contains(Coordinates)) {
base.Invalidate();
break; // TODO: might not be correct. Was : Exit For
}
}
base.OnMouseMove(e);
}
protected override void OnPaint(PaintEventArgs e)
{
G = e.Graphics;

if ((_PanelColor != null))
G.FillRectangle(ToBrush(_PanelColor), e.ClipRectangle);

PaintHook();

for (int I = 0; I <= TabPages.Count - 1; I++) {
TabPaint(I);
}
}

#endregion

}
8,677 chars, 1,060 words, 379 lines·Create new version