winform基礎控制元件樣式重繪
閒來無事,把一些自認為比較能夠用的上的做一個記錄,以備後用,也能在淡忘之際能夠重新記憶。
主要是針對winform中的一些基本控制元件做一個樣式更改,原因是自帶的控制元件在某些方面達不到要求,其實就是美觀的問題。
如下:
1、繪製圓角Button
/// <summary>
/// 圓角Button
/// </summary>
/// <param name="sender">buttonName</param>
/// <param name="p_1">10</param>
/// <param name="p_2">0.1</param>
public static void DrawButton(Control sender, int p_1, double p_2)
{
GraphicsPath oPath = new GraphicsPath();
oPath.AddClosedCurve(new Point[]{
new Point(0,sender.Height/p_1),
new Point(sender.Width/p_1,0),
new Point(sender.Width - sender.Height/p_1,0),
new Point(sender.Width,sender.Height/p_1),
new Point(sender.Width,sender.Height - sender.Height / p_1),
new Point(sender.Width - sender.Width / p_1,sender.Height),
new Point(sender.Width / p_1,sender.Height),
new Point(0,sender.Height - sender.Height / p_1)
},
(float)p_2);
sender.Region = new Region(oPath);
}
呼叫: Button btn = (Button)sender;
ControlDraw.DrawButton(btn, 10, 0.1);
2、繪製圓角窗體
/// <summary>
/// 圓角窗體
/// </summary>
/// <param name="rect"></param>
/// <param name="radius"></param>
/// <returns></returns>
public static GraphicsPath GetRoundedRecPath(Rectangle rect, int radius)
{
int diameter = radius;
Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
GraphicsPath path = new GraphicsPath();
path.AddArc(arcRect, 180, 90);
arcRect.X = rect.Right - diameter;
path.AddArc(arcRect, 270, 90);
arcRect.Y = rect.Bottom - diameter;
path.AddArc(arcRect, 0, 90);
arcRect.X = rect.Left;
path.AddArc(arcRect, 90, 90);
path.CloseFigure();
return path;
}
呼叫:窗體InstrumentStateFrm的Resize事件
private void InstrumentStateFrm_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
GraphicsPath formPath = new GraphicsPath();
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
formPath = ControlDraw.GetRoundedRecPath(rect, 50);
this.Region = new Region(formPath);
}
else
{
this.Region = null;
}
}
3、繪製圓角Panel
/// <summary>
/// 圓角panel
/// </summary>
/// <param name="rectangle">panel1_Paint事件e.ClipRectangle</param>
/// <param name="g">e.Graphics</param>
/// <param name="_radius">圓角弧度50</param>
/// <param name="cusp">true = 右邊突出尖角,false = 圓角Panel</param>
/// <param name="begin_color">顏色</param>
/// <param name="end_color">顏色</param>
public static void DrawPanle(Rectangle rectangle,Graphics g,int _radius,bool cusp,Color
begin_color,Color end_color)
{
int span = 2;
g.SmoothingMode = SmoothingMode.AntiAlias;
LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush(rectangle,
begin_color, end_color, LinearGradientMode.Vertical);
if (cusp)
{
span = 10;
PointF p1 = new PointF(rectangle.Width - 12, rectangle.Y + 10);
PointF p2 = new PointF(rectangle.Width - 12, rectangle.Y + 30);
PointF p3 = new PointF(rectangle.Width, rectangle.Y + 20);
PointF[] ptsArray = { p1, p2, p3 };
g.FillPolygon(myLinearGradientBrush, ptsArray);
}
g.FillPath(myLinearGradientBrush, DrawRoundRect(rectangle.X, rectangle.Y,
rectangle.Width - span, rectangle.Height - 1, _radius));
}
/// <summary>
/// 畫橢圓
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="radius"></param>
/// <returns></returns>
public static GraphicsPath DrawRoundRect(int x,int y,int width,int height,int radius)
{
GraphicsPath gp = new GraphicsPath();
gp.AddArc(x, y, radius, radius, 180, 90);
gp.AddArc(width - radius, y, radius, radius, 270, 90);
gp.AddArc(width - radius, height - radius, radius, radius, 0, 90);
gp.AddArc(x, height - radius, radius, radius, 90, 90);
return gp;
}
呼叫:panel的Paint事件
private void panel1_Paint(object sender, PaintEventArgs e)
{
//ControlDraw.DrawPanle(e.ClipRectangle, e.Graphics, 50, false, Color.FromArgb(224, 224,
224), Color.FromArgb(224, 224, 224));
ControlDraw.DrawPanle(e.ClipRectangle, e.Graphics, 5, false, Color.FromName("Control"),
Color.FromName("Control"));
base.OnPaint(e);
}
4、dataGridView內嵌入進度條
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace GS_XN.ControlFigure
{
/// <summary>
/// 自定義dataGridView進度條列
/// </summary>
public class DataGridViewProgressBarColumn : DataGridViewTextBoxColumn
{
public DataGridViewProgressBarColumn()
{
this.CellTemplate = new DataGridViewProgressBarCell();
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
if (!(value is DataGridViewProgressBarCell))
throw new InvalidCastException("DataGridViewProgressBarCell");
base.CellTemplate = value;
}
}
public int Maximum
{
get
{
return ((DataGridViewProgressBarCell)this.CellTemplate).Maximum;
}
set
{
if (this.Maximum == value)
return;
((DataGridViewProgressBarCell)this.CellTemplate).Maximum = value;
if (this.DataGridView == null)
return;
int rowCount = this.DataGridView.RowCount;
for (int i = 0; i < rowCount; i++)
{
DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
((DataGridViewProgressBarCell)r.Cells[this.Index]).Maximum = value;
}
}
}
public int Mimimum
{
get
{
return ((DataGridViewProgressBarCell)this.CellTemplate).Mimimum;
}
set
{
if (this.Mimimum == value)
return;
((DataGridViewProgressBarCell)this.CellTemplate).Mimimum = value;
if (this.DataGridView == null)
return;
int rowCount = this.DataGridView.RowCount;
for (int i = 0; i < rowCount; i++)
{
DataGridViewRow r = this.DataGridView.Rows.SharedRow(i);
((DataGridViewProgressBarCell)r.Cells[this.Index]).Mimimum = value;
}
}
}
}
public class DataGridViewProgressBarCell : DataGridViewTextBoxCell
{
public DataGridViewProgressBarCell()
{
this.maximymValue = 100;
this.mimimumValue = 0;
}
private int maximymValue;
public int Maximum
{
get { return this.maximymValue; }
set { this.maximymValue = value; }
}
private int mimimumValue;
public int Mimimum
{
get { return this.mimimumValue; }
set { this.mimimumValue = value; }
}
public override object DefaultNewRowValue
{
get
{
return 0;
}
}
public override object Clone()
{
DataGridViewProgressBarCell cell = (DataGridViewProgressBarCell)base.Clone();
cell.Maximum = this.Maximum;
cell.Mimimum = this.Mimimum;
return cell;
}
protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle
cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
if (value == null)
{
value = 0;
}
//double rate = (double)(intValue - this.mimimumValue) / (this.maximymValue - this.mimimumValue);
double rate = Convert.ToDouble(value);
if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border)
{
this.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
}
Rectangle borderRect = this.BorderWidths(advancedBorderStyle);
Rectangle paintRect = new Rectangle(
cellBounds.Left + borderRect.Left,
cellBounds.Top + borderRect.Top,
cellBounds.Width - borderRect.Right,
cellBounds.Height - borderRect.Bottom);
bool isSelected = (cellState & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected;
Color bkColor;
if (isSelected && (paintParts & DataGridViewPaintParts.SelectionBackground) == DataGridViewPaintParts.SelectionBackground)
{
bkColor = cellStyle.SelectionBackColor;
}
else
{
bkColor = cellStyle.BackColor;
}
if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background)
{
using (SolidBrush backBrush = new SolidBrush(bkColor))
{
graphics.FillRectangle(backBrush, paintRect);
}
}
paintRect.Offset(cellStyle.Padding.Right, cellStyle.Padding.Top);
paintRect.Width -= cellStyle.Padding.Horizontal;
paintRect.Height -= cellStyle.Padding.Vertical;
if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)
{
if (ProgressBarRenderer.IsSupported)
{
ProgressBarRenderer.DrawHorizontalBar(graphics, paintRect);
Rectangle barBounds = new Rectangle(
paintRect.Left + 3, paintRect.Top + 3,
paintRect.Width - 4, paintRect.Height - 6);
//barBounds.Width = (int)Math.Round(barBounds.Width * rate);
barBounds.Width = (int)Math.Round(barBounds.Width * rate/100);
ProgressBarRenderer.DrawHorizontalChunks(graphics, barBounds);
}
else
{
graphics.FillRectangle(Brushes.White, paintRect);
graphics.DrawRectangle(Pens.Black, paintRect);
Rectangle barBounds = new Rectangle(
paintRect.Left + 1, paintRect.Top + 1,
paintRect.Width - 1, paintRect.Height - 1);
//barBounds.Width = (int)Math.Round(barBounds.Width * rate);
barBounds.Width = (int)Math.Round(barBounds.Width * rate / 100);
graphics.FillRectangle(Brushes.Blue, barBounds);
}
}
if (this.DataGridView.CurrentCellAddress.X == this.ColumnIndex
&& this.DataGridView.CurrentCellAddress.Y == this.RowIndex
&& (paintParts & DataGridViewPaintParts.Focus) == DataGridViewPaintParts.Focus
&& this.DataGridView.Focused)
{
Rectangle focusRect = paintRect;
focusRect.Inflate(-3, -3);
ControlPaint.DrawFocusRectangle(graphics, focusRect);
}
if ((paintParts & DataGridViewPaintParts.ContentForeground) == DataGridViewPaintParts.ContentForeground)
{
//string txt = string.Format("{0}%", Math.Round(rate * 100));
string txt = string.Format("{0}%", Math.Round(rate));
TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
Color fColor = cellStyle.ForeColor;
paintRect.Inflate(-2, -2);
TextRenderer.DrawText(graphics, txt, cellStyle.Font, paintRect, fColor, flags);
}
if ((paintParts & DataGridViewPaintParts.ErrorIcon) == DataGridViewPaintParts.ErrorIcon
&& this.DataGridView.ShowCellErrors
&& !string.IsNullOrEmpty(errorText))
{
Rectangle iconBounds = this.GetErrorIconBounds(graphics, cellStyle, rowIndex);
iconBounds.Offset(cellBounds.X, cellBounds.Y);
this.PaintErrorIcon(graphics, iconBounds, cellBounds, errorText);
}
//base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
}
}
public class DataGridViewProgressColumn : DataGridViewImageColumn
{
public DataGridViewProgressColumn()
{
this.CellTemplate = new DataGridViewProgressCell();
}
}
public class DataGridViewProgressCell : DataGridViewImageCell
{
static Image emptyImage;
static DataGridViewProgressCell()
{
emptyImage = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
}
public DataGridViewProgressCell()
{
this.ValueType = typeof(int);
}
public string ShowText { get; set; } //如果要顯示獨立的文字而不是百分比,設定此屬性。
protected override object GetFormattedValue(object value,
int rowIndex, ref DataGridViewCellStyle cellStyle,
TypeConverter valueTypeConverter,
TypeConverter formattedValueTypeConverter,
DataGridViewDataErrorContexts context)
{
return emptyImage;
}
protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle
cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
{
string tValue = value.ToString();
if (tValue == "") tValue = "0";
int progressVal;
try { progressVal = Convert.ToInt16(tValue); }
catch
{
progressVal = 0;
}
float percentage = ((float)progressVal / 100.0f);
Brush backColorBrush = new SolidBrush(cellStyle.BackColor);
Brush foreColorBrush = new SolidBrush(cellStyle.ForeColor);
base.Paint(g, clipBounds, cellBounds,
rowIndex, cellState, value, formattedValue, errorText,
cellStyle, advancedBorderStyle, (paintParts & ~DataGridViewPaintParts.ContentForeground));
string DrawStringStr = progressVal.ToString() + "%";
if (ShowText != "")
{
DrawStringStr = ShowText;
}
if (percentage > 0.0)
{
g.FillRectangle(new SolidBrush(Color.FromArgb(163, 189, 242)), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 4)), cellBounds.Height - 4);
g.DrawString(DrawStringStr, cellStyle.Font, foreColorBrush, cellBounds.X + 30, cellBounds.Y + 5);
}
else
{
if (this.DataGridView.CurrentRow.Index == rowIndex)
g.DrawString(DrawStringStr, cellStyle.Font, new SolidBrush(cellStyle.SelectionForeColor), cellBounds.X + 30, cellBounds.Y + 5);
else
g.DrawString(DrawStringStr, cellStyle.Font, foreColorBrush, cellBounds.X + 30, cellBounds.Y + 5);
}
}
}
}
呼叫:
DataGridViewProgressBarColumn pbColumn = new DataGridViewProgressBarColumn();
pbColumn.HeaderText = "進度";
pbColumn.ValueType = typeof(int);
pbColumn.Name = "進度";
pbColumn.DataPropertyName = "進度";
pbColumn.HeaderText = "進度";
dataGridView1.Columns.Add(pbColumn);
DataColumn dc1 = new DataColumn("進度", Type.GetType("System.String"));
dt.Columns.Add(dc1);
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i]["進度"] = i + 50;
}