ControlC ControlC.com

[C#] TabControlDesigner 1.0.0

Views: 12Total Chars: 5,777
Created: 6/4/12 11:32:22PM 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;

//-------------------------------------
//TabControlDesigner
//
//Creator: Eprouvez
//Date: 6/5/2012
//Version: 1.0.0
//
//Credits to:
//Aeonhack
//mavamaarten
//
//Coming soon:
//Tab customization and sharing
//More helpful methods
//Make Tab offsets easier to work with
//--------------------------------------

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 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; }
}


#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;
SizeMode = TabSizeMode.Fixed;
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
}
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

}
5,777 chars, 697 words, 236 lines·Create new version