漸層:
1.FlatComboBox.cs
public class FlatComboBox : ComboBox
{
private Color borderColor = Color.Gray;
[DefaultValue(typeof(Color), "Gray")]
public Color BorderColor
{
get { return borderColor; }
set
{
if (borderColor != value)
{
borderColor = value;
Invalidate();
}
}
}
private Color buttonColor = Color.LightGray;
[DefaultValue(typeof(Color), "LightGray")]
public Color ButtonColor
{
get { return buttonColor; }
set
{
if (buttonColor != value)
{
buttonColor = value;
Invalidate();
}
}
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_PAINT && DropDownStyle != ComboBoxStyle.Simple)
{
var clientRect = ClientRectangle;
var dropDownButtonWidth = SystemInformation.HorizontalScrollBarArrowWidth;
var outerBorder = new Rectangle(clientRect.Location,
new Size(clientRect.Width - 1, clientRect.Height - 1));
var innerBorder = new Rectangle(outerBorder.X + 1, outerBorder.Y + 1,
outerBorder.Width - dropDownButtonWidth - 2, outerBorder.Height - 2);
var innerInnerBorder = new Rectangle(innerBorder.X + 1, innerBorder.Y + 1,
innerBorder.Width - 2, innerBorder.Height - 2);
var dropDownRect = new Rectangle(innerBorder.Right + 1, innerBorder.Y,
dropDownButtonWidth, innerBorder.Height + 1);
if (RightToLeft == RightToLeft.Yes)
{
innerBorder.X = clientRect.Width - innerBorder.Right;
innerInnerBorder.X = clientRect.Width - innerInnerBorder.Right;
dropDownRect.X = clientRect.Width - dropDownRect.Right;
dropDownRect.Width += 1;
}
var innerBorderColor = Enabled ? BackColor : SystemColors.Control;
var outerBorderColor = Enabled ? BorderColor : SystemColors.ControlDark;
var buttonColor = Enabled ? ButtonColor : SystemColors.Control;
var middle = new Point(dropDownRect.Left + dropDownRect.Width / 2,
dropDownRect.Top + dropDownRect.Height / 2);
var arrow = new Point[]
{
new Point(middle.X - 3, middle.Y - 2),
new Point(middle.X + 4, middle.Y - 2),
new Point(middle.X, middle.Y + 2)
};
var ps = new PAINTSTRUCT();
bool shoulEndPaint = false;
IntPtr dc;
if (m.WParam == IntPtr.Zero)
{
dc = BeginPaint(Handle, ref ps);
m.WParam = dc;
shoulEndPaint = true;
}
else
{
dc = m.WParam;
}
var rgn = CreateRectRgn(innerInnerBorder.Left, innerInnerBorder.Top,
innerInnerBorder.Right, innerInnerBorder.Bottom);
SelectClipRgn(dc, rgn);
DefWndProc(ref m);
DeleteObject(rgn);
rgn = CreateRectRgn(clientRect.Left, clientRect.Top,
clientRect.Right, clientRect.Bottom);
SelectClipRgn(dc, rgn);
using (var g = Graphics.FromHdc(dc))
{
using (var b = new SolidBrush(buttonColor))
{
g.FillRectangle(b, dropDownRect);
}
using (var b = new SolidBrush(outerBorderColor))
{
g.FillPolygon(b, arrow);
}
using (var p = new Pen(innerBorderColor))
{
g.DrawRectangle(p, innerBorder);
g.DrawRectangle(p, innerInnerBorder);
}
using (var p = new Pen(outerBorderColor))
{
g.DrawRectangle(p, outerBorder);
}
}
if (shoulEndPaint)
EndPaint(Handle, ref ps);
DeleteObject(rgn);
}
else
base.WndProc(ref m);
}
private const int WM_PAINT = 0xF;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int L, T, R, B;
}
[StructLayout(LayoutKind.Sequential)]
public struct PAINTSTRUCT
{
public IntPtr hdc;
public bool fErase;
public int rcPaint_left;
public int rcPaint_top;
public int rcPaint_right;
public int rcPaint_bottom;
public bool fRestore;
public bool fIncUpdate;
public int reserved1;
public int reserved2;
public int reserved3;
public int reserved4;
public int reserved5;
public int reserved6;
public int reserved7;
public int reserved8;
}
[DllImport("user32.dll")]
private static extern IntPtr BeginPaint(IntPtr hWnd,
[In, Out] ref PAINTSTRUCT lpPaint);
[DllImport("user32.dll")]
private static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT lpPaint);
[DllImport("gdi32.dll")]
public static extern int SelectClipRgn(IntPtr hDC, IntPtr hRgn);
[DllImport("user32.dll")]
public static extern int GetUpdateRgn(IntPtr hwnd, IntPtr hrgn, bool fErase);
public enum RegionFlags
{
ERROR = 0,
NULLREGION = 1,
SIMPLEREGION = 2,
COMPLEXREGION = 3,
}
[DllImport("gdi32.dll")]
internal static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
private static extern IntPtr CreateRectRgn(int x1, int y1, int x2, int y2);
}
2.GroupBoxEx.cs
public class GroupBoxEx : GroupBox
{
private Color borderColor = Color.Black;
[DefaultValue(typeof(Color), "Black")]
public Color BorderColor
{
get { return borderColor; }
set { borderColor = value; this.Invalidate(); }
}
private Color textColor = Color.Black;
[DefaultValue(typeof(Color), "Black")]
public Color TextColor
{
get { return textColor; }
set { textColor = value; this.Invalidate(); }
}
protected override void OnPaint(PaintEventArgs e)
{
GroupBoxState state = base.Enabled ? GroupBoxState.Normal :
GroupBoxState.Disabled;
TextFormatFlags flags = TextFormatFlags.PreserveGraphicsTranslateTransform |
TextFormatFlags.PreserveGraphicsClipping | TextFormatFlags.TextBoxControl |
TextFormatFlags.WordBreak;
Color titleColor = this.TextColor;
if (!this.ShowKeyboardCues)
flags |= TextFormatFlags.HidePrefix;
if (this.RightToLeft == RightToLeft.Yes)
flags |= TextFormatFlags.RightToLeft | TextFormatFlags.Right;
if (!this.Enabled)
titleColor = SystemColors.GrayText;
DrawUnthemedGroupBoxWithText(e.Graphics, new Rectangle(0, 0, base.Width,
base.Height), this.Text, this.Font, titleColor, flags, state);
RaisePaintEvent(this, e);
}
private void DrawUnthemedGroupBoxWithText(Graphics g, Rectangle bounds,
string groupBoxText, Font font, Color titleColor,
TextFormatFlags flags, GroupBoxState state)
{
Rectangle rectangle = bounds;
rectangle.Width -= 8;
Size size = TextRenderer.MeasureText(g, groupBoxText, font,
new Size(rectangle.Width, rectangle.Height), flags);
rectangle.Width = size.Width;
rectangle.Height = size.Height;
if ((flags & TextFormatFlags.Right) == TextFormatFlags.Right)
rectangle.X = (bounds.Right - rectangle.Width) - 8;
else
rectangle.X += 8;
TextRenderer.DrawText(g, groupBoxText, font, rectangle, titleColor, flags);
if (rectangle.Width > 0)
rectangle.Inflate(2, 0);
using (var pen = new Pen(this.BorderColor))
{
int num = bounds.Top + (font.Height / 2);
g.DrawLine(pen, bounds.Left, num - 1, bounds.Left, bounds.Height - 2);
g.DrawLine(pen, bounds.Left, bounds.Height - 2, bounds.Width - 1,
bounds.Height - 2);
g.DrawLine(pen, bounds.Left, num - 1, rectangle.X - 3, num - 1);
g.DrawLine(pen, rectangle.X + rectangle.Width + 2, num - 1,
bounds.Width - 2, num - 1);
g.DrawLine(pen, bounds.Width - 2, num - 1, bounds.Width - 2,
bounds.Height - 2);
}
}
}
3.LabelGradient.cs
public class LabelGradient : Label
{
#region Implementtation Member Fields
protected Color gradientColorOne = Color.White;
protected Color gradientColorTwo = Color.Blue;
protected LinearGradientMode lgm =
LinearGradientMode.ForwardDiagonal;
protected Border3DStyle b3dstyle = Border3DStyle.Bump;
#endregion
#region GradientColorOne Properties
[
DefaultValue(typeof(Color), "White"),
System.ComponentModel.Description("The first gradient color."),
System.ComponentModel.Category("Appearance"),
]
//GradientColorOne Properties
public Color GradientColorOne
{
get
{
return gradientColorOne;
}
set
{
gradientColorOne = value;
Invalidate();
}
}
#endregion
#region GradientColorTwo Properties
[
DefaultValue(typeof(Color), "Blue"),
Description("The second gradient color."),
Category("Appearance"),
]
//GradientColorTwo Properties
public Color GradientColorTwo
{
get
{
return gradientColorTwo;
}
set
{
gradientColorTwo = value;
Invalidate();
}
}
#endregion
#region LinearGradientMode Properties
//LinearGradientMode Properties
[
DefaultValue(typeof(LinearGradientMode), "ForwardDiagonal"),
Description("Gradient Mode"),
Category("Appearance"),
]
public LinearGradientMode GradientMode
{
get
{
return lgm;
}
set
{
lgm = value;
Invalidate();
}
}
#endregion
#region Border3DStyle Properties
//Border3DStyle Properties
[
DefaultValue(typeof(Border3DStyle), "Bump"),
Description("BorderStyle"),
Category("Appearance"),
]
// hide BorderStyle inherited from the base class
new public Border3DStyle BorderStyle
{
get
{
return b3dstyle;
}
set
{
b3dstyle = value;
Invalidate();
}
}
#endregion
#region Removed Properties
// Remove BackColor Property
[
Browsable(false),
EditorBrowsable(EditorBrowsableState.Never)
]
public override System.Drawing.Color BackColor
{
get
{
return new System.Drawing.Color();
}
set {; }
}
#endregion
public LabelGradient()
{
//
// TODO: Add constructor logic here
//
}
protected override void
OnPaintBackground(System.Windows.Forms.PaintEventArgs pevent)
{
Graphics gfx = pevent.Graphics;
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
// Dispose of brush resources after use
using (LinearGradientBrush lgb = new
LinearGradientBrush(rect,
gradientColorOne, gradientColorTwo, lgm))
gfx.FillRectangle(lgb, rect);
ControlPaint.DrawBorder3D(gfx, rect, b3dstyle);
}
}
4.RoundedButton.cs
public class RoundedButton : Button
{
GraphicsPath GetRoundPath(RectangleF Rect, int radius)
{
float r2 = radius / 2f;
GraphicsPath GraphPath = new GraphicsPath();
GraphPath.AddArc(Rect.X, Rect.Y, radius, radius, 180, 90);
GraphPath.AddLine(Rect.X + r2, Rect.Y, Rect.Width - r2, Rect.Y);
GraphPath.AddArc(Rect.X + Rect.Width - radius, Rect.Y, radius, radius, 270, 90);
GraphPath.AddLine(Rect.Width, Rect.Y + r2, Rect.Width, Rect.Height - r2);
GraphPath.AddArc(Rect.X + Rect.Width - radius,
Rect.Y + Rect.Height - radius, radius, radius, 0, 90);
GraphPath.AddLine(Rect.Width - r2, Rect.Height, Rect.X + r2, Rect.Height);
GraphPath.AddArc(Rect.X, Rect.Y + Rect.Height - radius, radius, radius, 90, 90);
GraphPath.AddLine(Rect.X, Rect.Height - r2, Rect.X, Rect.Y + r2);
GraphPath.CloseFigure();
return GraphPath;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
RectangleF Rect = new RectangleF(0, 0, this.Width, this.Height);
using (GraphicsPath GraphPath = GetRoundPath(Rect, 50))
{
this.Region = new Region(GraphPath);
using (Pen pen = new Pen(Color.CadetBlue, 1.75f))
{
pen.Alignment = PenAlignment.Inset;
e.Graphics.DrawPath(pen, GraphPath);
}
}
}
}
5.BigDateTimePicker.cs
public class BigDateTimePicker : DateTimePicker
{
private const int SWP_NOMOVE = 0x0002;
private const int DTM_First = 0x1000;
private const int DTM_GETMONTHCAL = DTM_First + 8;
private const int MCM_GETMINREQRECT = DTM_First + 9;
[DllImport("uxtheme.dll")]
private static extern int SetWindowTheme(IntPtr hWnd, string appName, string idList);
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, ref RECT lParam);
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
int X, int Y, int cx, int cy, int uFlags);
[DllImport("User32.dll")]
private static extern IntPtr GetParent(IntPtr hWnd);
[StructLayout(LayoutKind.Sequential)]
private struct RECT { public int L, T, R, B; }
protected override void OnDropDown(EventArgs eventargs)
{
var hwndCalendar = SendMessage(this.Handle, DTM_GETMONTHCAL, 0, 0);
SetWindowTheme(hwndCalendar, string.Empty, string.Empty);
var r = new RECT();
SendMessage(hwndCalendar, MCM_GETMINREQRECT, 0, ref r);
var hwndDropDown = GetParent(hwndCalendar);
SetWindowPos(hwndDropDown, IntPtr.Zero, 0, 0,
r.R - r.L + 6, r.B - r.T + 6, SWP_NOMOVE);
base.OnDropDown(eventargs);
}
}
5.MDI
using System;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsFormsAppMDI
{
partial class Form1
{
/// <summary>
/// 設計工具所需的變數。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清除任何使用中的資源。
/// </summary>
/// <param name="disposing">如果應該處置受控資源則為 true,否則為 false。</param>
protected override void Dispose(bool disposing)
{
//if (disposing && (components != null))
//{
// components.Dispose();
//}
//base.Dispose(disposing);
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改
/// 這個方法的內容。
/// </summary>
private void InitializeComponent()
{
//this.components = new System.ComponentModel.Container();
//this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
//this.ClientSize = new System.Drawing.Size(800, 450);
//this.Text = "Form1";
this.tabControl1 = new System.Windows.Forms.TabControl();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Appearance = System.Windows.Forms.TabAppearance.FlatButtons;
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.tabControl1.Location = new System.Drawing.Point(0, 269);
this.tabControl1.Multiline = true;
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(292, 0);
this.tabControl1.TabIndex = 0;
this.tabControl1.Resize += new System.EventHandler(this.tabControl1_Resize);
this.tabControl1.ControlAdded += new System.Windows.Forms.ControlEventHandler(this.tabControl1_ControlAdded);
this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "New";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 269);
this.Controls.Add(this.tabControl1);
this.IsMdiContainer = true;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.MdiChildActivate += new System.EventHandler(this.Form1_MdiChildActivate);
this.ResumeLayout(false);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private Hashtable tabs = new Hashtable();
private void menuItem1_Click(object sender, System.EventArgs e)
{
Form f = new Form();
f.Text = GetAvailableName();
f.WindowState = FormWindowState.Maximized;
f.Closing += new CancelEventHandler(ChildFormClosing);
f.MdiParent = this;
AddChildForm(f);
}
private void AddChildForm(Form f)
{
if (Array.IndexOf(this.MdiChildren, f) == -1) return;
TabPage tab = new TabPage(f.Text);
f.Tag = tab;
tabs.Add(tab, tab.Text);
tabControl1.TabPages.Add(tab);
f.Show();
}
private void RemoveChildForm(Form f)
{
f.Closing -= new CancelEventHandler(ChildFormClosing);
tabs.Remove(f.Tag);
tabControl1.TabPages.Remove((TabPage)f.Tag);
tabControl1_Resize(this, EventArgs.Empty);
}
private void Form1_MdiChildActivate(object sender, System.EventArgs e)
{
if (this.ActiveMdiChild == null) return;
if (this.ActiveMdiChild.Tag == null) return;
this.tabControl1.SelectedTab = (TabPage)this.ActiveMdiChild.Tag;
}
private void ChildFormClosing(Object sender, System.ComponentModel.CancelEventArgs e)
{
RemoveChildForm((Form)sender);
}
private void tabControl1_ControlAdded(object sender, ControlEventArgs e)
{
tabControl1_Resize(this, EventArgs.Empty);
}
private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (tabControl1.SelectedIndex == -1 || !tabControl1.Created)
return;
if (tabs.Contains(tabControl1.SelectedTab))
{
foreach (Form child in this.MdiChildren)
{
if (child.Tag == tabControl1.SelectedTab)
{
child.Activate();
break;
}
}
}
}
private void tabControl1_Resize(object sender, System.EventArgs e)
{
tabControl1.Height = tabControl1.ItemSize.Height * tabControl1.RowCount;
}
private String GetAvailableName()
{
String newName = "Child0";
for (int index = 0; index <= tabs.Count; index++)
{
newName = "Child" + index;
if (!tabs.ContainsValue(newName))
break;
}
return newName;
}
}
}
6.Gradient
public void Form_Background(object sender, PaintEventArgs e)
{
Color c1 = Color.FromArgb(255, 252, 254, 255);
Color c2 = Color.FromArgb(255, 247, 251, 253);
Color c3 = Color.FromArgb(255, 228, 239, 247);
Color c4 = Color.FromArgb(255, 217, 228, 238);
Color c5 = Color.FromArgb(255, 200, 212, 217);
Color c6 = Color.FromArgb(255, 177, 198, 215);
Color c7 = Color.FromArgb(255, 166, 186, 208);
// Changed: c1 / c7 as start colors, and at 90 degrees. Removed later transform.
LinearGradientBrush br = new LinearGradientBrush(this.ClientRectangle, c1, c7, 90, true);
ColorBlend cb = new ColorBlend();
cb.Positions = new[] { 0, (float)0.146, (float)0.317, (float)0.439, (float)0.585, (float)0.797, 1 };
cb.Colors = new[] { c1, c2, c3, c4, c5, c6, c7 };
br.InterpolationColors = cb;
// removed rotate call
// paint
e.Graphics.FillRectangle(br, this.ClientRectangle);
}
沒有留言:
張貼留言