using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Diagnostics;
using System.Threading;
using System.ComponentModel;
using System.Reflection;
using System.Text.RegularExpressions;
namespace JinYuan.ControlLib.UCHelper
{
///
/// 控件工具类
///
public static class ControlHelper
{
#region Graphics
///
/// 获取指定窗口(包括非工作区)的Graphics
///
/// 指定窗口的handle
/// 返回g
/// 返回hDC
public static void GetWindowGraphics(IntPtr handle, out Graphics g, out IntPtr hDC)
{
hDC = NativeMethods.GetWindowDC(handle);
g = Graphics.FromHdc(hDC);
}
///
/// 获取指定窗口(只包括工作区)的Graphics
///
/// 指定窗口的handle
/// 返回g
/// 返回hDC
public static void GetWindowClientGraphics(IntPtr handle, out Graphics g, out IntPtr hDC)
{
hDC = NativeMethods.GetDC(handle);
g = Graphics.FromHdc(hDC);
}
#endregion
#region 圆角、边框
///
/// 绘制Rectangle 1个像素边框(圆角边框半径为3个像素)
///
/// 要绘制边框的控件
/// 要绘制边框的控件画布
/// 是否为圆角边框(圆角边框半径为3个像素)
/// 边框颜色
/// 父控件背景色(圆角边框平滑过度颜色)
/// 控件背景色(圆角边框平滑过度颜色)
public static void DrawControlBorder(Control control, Graphics g, bool roundCorner, Color borderColor, Color borderOutOverColor, Color borderInOverColor)
{
Rectangle rect = control.ClientRectangle;
SmoothingMode smoothingMode = g.SmoothingMode;
CompositingMode compositingMode = g.CompositingMode;
CompositingQuality compositingQuality = g.CompositingQuality;
InterpolationMode interpolationMode = g.InterpolationMode;
g.SmoothingMode = SmoothingMode.None;
g.CompositingMode = CompositingMode.SourceCopy;
g.CompositingQuality = CompositingQuality.Default;
g.InterpolationMode = InterpolationMode.Default;
if (roundCorner)
{
Pen border_pen = new Pen(borderColor);
//边框
g.DrawPolygon(border_pen, new Point[] {
new Point(rect.Left,rect.Top+1),
new Point(rect.Left+1,rect.Top+1),
new Point(rect.Left+1,rect.Top),
new Point(rect.Right-1-1,rect.Top),
new Point(rect.Right-1-1,rect.Top+1),
new Point(rect.Right-1,rect.Top+1),
new Point(rect.Right-1,rect.Bottom-1-1),
new Point(rect.Right-1-1,rect.Bottom-1-1),
new Point(rect.Right-1-1,rect.Bottom-1),
new Point(rect.Left+1,rect.Bottom-1),
new Point(rect.Left+1,rect.Bottom-1-1),
new Point(rect.Left,rect.Bottom-1-1),
});
//边框圆角外平滑过度
border_pen.Color = Color.FromArgb(borderColor.R + (borderOutOverColor.R - borderColor.R) / 4, borderColor.G + (borderOutOverColor.G - borderColor.G) / 4, borderColor.B + (borderOutOverColor.B - borderColor.B) / 4);
g.DrawLine(border_pen, new Point(rect.Left, rect.Top + 1), new Point(rect.Left + 1, rect.Top));
g.DrawLine(border_pen, new Point(rect.Right - 2, rect.Top), new Point(rect.Right - 1, rect.Top + 1));
g.DrawLine(border_pen, new Point(rect.Right - 1, rect.Bottom - 2), new Point(rect.Right - 2, rect.Bottom - 1));
g.DrawLine(border_pen, new Point(rect.Left + 1, rect.Bottom - 1), new Point(rect.Left, rect.Bottom - 2));
//边框圆角内平滑过度
SolidBrush sb = new SolidBrush(Color.FromArgb((borderColor.R + borderInOverColor.R) / 2, (borderColor.G + borderInOverColor.G) / 2, (borderColor.B + borderInOverColor.B) / 2));
g.FillRectangle(sb, new Rectangle(rect.Left + 1, rect.Top + 1, 1, 1));
g.FillRectangle(sb, new Rectangle(rect.Right - 2, rect.Top + 1, 1, 1));
g.FillRectangle(sb, new Rectangle(rect.Right - 2, rect.Bottom - 2, 1, 1));
g.FillRectangle(sb, new Rectangle(rect.Left + 1, rect.Bottom - 2, 1, 1));
sb.Dispose();
border_pen.Dispose();
}
else
{
Pen border_pen = new Pen(borderColor);
g.DrawRectangle(border_pen, new Rectangle(rect.X, rect.Y, rect.Width - 1, rect.Height - 1));
border_pen.Dispose();
}
g.SmoothingMode = smoothingMode;
g.CompositingMode = compositingMode;
g.CompositingQuality = compositingQuality;
g.InterpolationMode = interpolationMode;
}
///
/// 计算Rectangle 1个像素圆角形状路径(圆角边框半径为3个像素)
///
/// rect
///
public static GraphicsPath AdjustRectangleShapePath(Rectangle rect)
{
GraphicsPath gp = new GraphicsPath();
gp.AddPolygon(new Point[] {
new Point(rect.Left,rect.Top+1),
new Point(rect.Left+1,rect.Top+1),
new Point(rect.Left+1,rect.Top),
new Point(rect.Right-1,rect.Top),
new Point(rect.Right-1,rect.Top+1),
new Point(rect.Right,rect.Top+1),
new Point(rect.Right,rect.Bottom-1),
new Point(rect.Right-1,rect.Bottom-1),
new Point(rect.Right-1,rect.Bottom),
new Point(rect.Left+1,rect.Bottom),
new Point(rect.Left+1,rect.Bottom-1),
new Point(rect.Left,rect.Bottom-1),
});
return gp;
}
///
/// 计算点到矩形的最大圆形最大半径
///
/// 矩形
/// 点
///
public static int AdjustRectangleMaxRadiusForPoint(Rectangle rect, Point point)
{
int radius = 0;
radius = Math.Max(radius, (int)Math.Ceiling(Math.Sqrt(Math.Pow(Math.Abs(point.X - rect.X), 2) + Math.Pow(Math.Abs(point.Y - rect.Y), 2))));
radius = Math.Max(radius, (int)Math.Ceiling(Math.Sqrt(Math.Pow(Math.Abs(point.X - rect.Right), 2) + Math.Pow(Math.Abs(point.Y - rect.Y), 2))));
radius = Math.Max(radius, (int)Math.Ceiling(Math.Sqrt(Math.Pow(Math.Abs(point.X - rect.Right), 2) + Math.Pow(Math.Abs(point.Y - rect.Bottom), 2))));
radius = Math.Max(radius, (int)Math.Ceiling(Math.Sqrt(Math.Pow(Math.Abs(point.X - rect.X), 2) + Math.Pow(Math.Abs(point.Y - rect.Bottom), 2))));
return radius;
}
///
/// 转换成圆角
///
/// 要转换的rect
/// 圆角半径的大小
///
public static GraphicsPath AdjustCircularPath(Rectangle rect, int radius)
{
return AdjustCircularPath(rect, radius, radius, radius, radius);
}
///
/// 转换成圆角
///
/// 要转换的rect
/// 左上角
/// 右上角
/// 右下角
/// 左下角
///
public static GraphicsPath AdjustCircularPath(Rectangle rect, int leftTopRadius, int rightTopRadius, int rightBottomRadius, int leftBottomRadius)
{
leftTopRadius = Math.Abs(leftTopRadius);
int leftTopDiameter = leftTopRadius * 2;
rightTopRadius = Math.Abs(rightTopRadius);
int rightTopDiameter = rightTopRadius * 2;
rightBottomRadius = Math.Abs(rightBottomRadius);
int rightBottomDiameter = rightBottomRadius * 2;
leftBottomRadius = Math.Abs(leftBottomRadius);
int leftBottomDiameter = leftBottomRadius * 2;
PointF leftTop_x = new PointF(rect.Left, rect.Top);
PointF leftTop_y = new PointF(rect.Left, rect.Top);
if (leftTopRadius > 0)
{
leftTop_x = new PointF(rect.Left + leftTopRadius, rect.Top);
leftTop_y = new PointF(rect.Left, rect.Top + leftTopRadius);
}
PointF rightTop_x = new PointF(rect.Right, rect.Top);
PointF rightTop_y = new PointF(rect.Right, rect.Top);
if (rightTopRadius > 0)
{
rightTop_x = new PointF(rect.Right - rightTopRadius, rect.Top);
rightTop_y = new PointF(rect.Right, rect.Top + rightTopRadius);
}
PointF rightBottom_x = new PointF(rect.Right, rect.Bottom);
PointF rightBottom_y = new PointF(rect.Right, rect.Bottom);
if (rightBottomRadius > 0)
{
rightBottom_x = new PointF(rect.Right - rightBottomRadius, rect.Bottom);
rightBottom_y = new PointF(rect.Right, rect.Bottom - rightBottomRadius);
}
PointF leftBottom_x = new PointF(rect.Left, rect.Bottom);
PointF leftBottom_y = new PointF(rect.Left, rect.Bottom);
if (leftBottomRadius > 0)
{
leftBottom_x = new PointF(rect.Left + leftBottomRadius, rect.Bottom);
leftBottom_y = new PointF(rect.Left, rect.Bottom - leftBottomRadius);
}
GraphicsPath gp = new GraphicsPath();
if (leftTopRadius > 0)
{
RectangleF lefttop_rect = new RectangleF(rect.Left, rect.Top, leftTopDiameter, leftTopDiameter);
gp.AddArc(lefttop_rect, 180, 90);
}
gp.AddLine(leftTop_x, rightTop_x);
if (rightTopRadius > 0)
{
RectangleF righttop_rect = new RectangleF(rect.Right - rightTopDiameter, rect.Top, rightTopDiameter, rightTopDiameter);
gp.AddArc(righttop_rect, 270, 90);
}
gp.AddLine(rightTop_y, rightBottom_y);
if (rightBottomRadius > 0)
{
RectangleF rightbottom_rect = new RectangleF(rect.Right - rightBottomDiameter, rect.Bottom - rightBottomDiameter, rightBottomDiameter, rightBottomDiameter);
gp.AddArc(rightbottom_rect, 0, 90);
}
gp.AddLine(rightBottom_x, leftBottom_x);
if (leftBottomRadius > 0)
{
RectangleF leftbottom_rect = new RectangleF(rect.Left, rect.Bottom - leftBottomDiameter, leftBottomDiameter, leftBottomDiameter);
gp.AddArc(leftbottom_rect, 90, 90);
}
gp.AddLine(leftBottom_y, leftTop_y);
gp.CloseAllFigures();
return gp;
}
///
/// 转换成圆角
///
/// 要转换的rect
/// 左上角
/// 右上角
/// 右下角
/// 左下角
///
public static Region AdjustCircularRegion(Rectangle rect, int leftTopRadius, int rightTopRadius, int rightBottomRadius, int leftBottomRadius)
{
Region r = new Region(rect);
GraphicsPath gp = new GraphicsPath();
leftTopRadius = Math.Abs(leftTopRadius);
int leftTopDiameter = leftTopRadius * 2;
rightTopRadius = Math.Abs(rightTopRadius);
int rightTopDiameter = rightTopRadius * 2;
rightBottomRadius = Math.Abs(rightBottomRadius);
int rightBottomDiameter = rightBottomRadius * 2;
leftBottomRadius = Math.Abs(leftBottomRadius);
int leftBottomDiameter = leftBottomRadius * 2;
if (leftTopRadius > 0)
{
Region lefttop_r = new Region(new Rectangle(rect.Left, rect.Top, leftTopRadius, leftTopRadius));
gp.Reset();
gp.AddPie(new Rectangle(rect.Left, rect.Top, leftTopDiameter, leftTopDiameter), 180, 90);
lefttop_r.Exclude(gp);
r.Exclude(lefttop_r);
lefttop_r.Dispose();
}
if (rightTopRadius > 0)
{
Region righttop_r = new Region(new Rectangle(rect.Right - rightTopRadius, rect.Top, rightTopRadius, rightTopRadius));
gp.Reset();
gp.AddPie(new Rectangle(rect.Right - rightTopDiameter, rect.Top, rightTopDiameter, rightTopDiameter), 270, 90);
righttop_r.Exclude(gp);
r.Exclude(righttop_r);
righttop_r.Dispose();
}
if (rightBottomRadius > 0)
{
Region rightbottom_r = new Region(new Rectangle(rect.Right - rightBottomRadius, rect.Bottom - rightBottomRadius, rightBottomRadius, rightBottomRadius));
gp.Reset();
gp.AddPie(new Rectangle(rect.Right - rightBottomDiameter, rect.Bottom - rightBottomDiameter, rightBottomDiameter, rightBottomDiameter), 0, 90);
rightbottom_r.Exclude(gp);
r.Exclude(rightbottom_r);
rightbottom_r.Dispose();
}
if (leftBottomRadius > 0)
{
Region leftbottom_r = new Region(new Rectangle(rect.Left, rect.Bottom - leftBottomRadius, leftBottomRadius, leftBottomRadius));
gp.Reset();
gp.AddPie(new Rectangle(rect.Left, rect.Bottom - leftBottomDiameter, leftBottomDiameter, leftBottomDiameter), 90, 90);
leftbottom_r.Exclude(gp);
r.Exclude(leftbottom_r);
leftbottom_r.Dispose();
}
gp.Dispose();
return r;
}
///
/// 根据画笔大小计算出真是rectf
///
/// 要转换的rectf
/// 画笔大小大小
/// 画笔对齐方式
///
public static RectangleF TransformRectangleF(RectangleF rectf, float pen, PenAlignment alignment = PenAlignment.Center)
{
if (pen <= 0)
{
return rectf;
}
RectangleF result = new RectangleF();
if (alignment == PenAlignment.Center || alignment == PenAlignment.Left || alignment == PenAlignment.Right || alignment == PenAlignment.Outset)
{
result.Width = rectf.Width - pen;
result.Height = rectf.Height - pen;
result.X = rectf.X + ((int)pen) / 2;
result.Y = rectf.Y + ((int)pen) / 2;
}
else if (alignment == PenAlignment.Inset)
{
if (pen > 0 && pen < 2)
{
result.Width = rectf.Width - 1;
result.Height = rectf.Height - 1;
}
else
{
result.Width = rectf.Width;
result.Height = rectf.Height;
}
result.X = rectf.X;
result.Y = rectf.Y;
}
return result;
}
///
/// 根据画笔大小计算出真是rectf
///
/// 要转换的rect
/// 画笔大小大小
/// 画笔对齐方式
///
public static Rectangle TransformRectangle(Rectangle rect, int pen, PenAlignment alignment = PenAlignment.Center)
{
if (pen <= 0)
{
return rect;
}
Rectangle result = new Rectangle();
if (alignment == PenAlignment.Center || alignment == PenAlignment.Left || alignment == PenAlignment.Right || alignment == PenAlignment.Outset)
{
result.Width = rect.Width - pen;
result.Height = rect.Height - pen;
result.X = rect.X + ((int)pen) / 2;
result.Y = rect.Y + ((int)pen) / 2;
}
else if (alignment == PenAlignment.Inset)
{
if (pen > 0 && pen < 2)
{
result.Width = rect.Width - 1;
result.Height = rect.Height - 1;
}
else
{
result.Width = rect.Width;
result.Height = rect.Height;
}
result.X = rect.X;
result.Y = rect.Y;
}
return result;
}
#endregion
#region 字体
///
/// 获取字体顶部空格
///
///
public static int GetFontTopHiatus(Font font)
{
return Math.Max(1, font.Height * 2 / 10);
}
///
/// 获取字体真实高度
///
///
public static int GetFontRealHeight(Font font)
{
return Math.Max(1, font.Height * 8 / 10);
}
#endregion
#region (Size、Point)角度
///
/// 计算指定角度的坐标
///
/// 圆心坐标
/// 圆半径
/// 角度
///
public static PointF CalculatePointForAngle(PointF center, float radius, float angle)
{
if (radius == 0)
return center;
float w = 0;
float h = 0;
if (angle <= 90)
{
w = radius * (float)Math.Cos(Math.PI / 180 * angle);
h = radius * (float)Math.Sin(Math.PI / 180 * angle);
}
else if (angle <= 180)
{
w = -radius * (float)Math.Sin(Math.PI / 180 * (angle - 90));
h = radius * (float)Math.Cos(Math.PI / 180 * (angle - 90));
}
else if (angle <= 270)
{
w = -radius * (float)Math.Cos(Math.PI / 180 * (angle - 180));
h = -radius * (float)Math.Sin(Math.PI / 180 * (angle - 180));
}
else
{
w = radius * (float)Math.Sin(Math.PI / 180 * (angle - 270));
h = -radius * (float)Math.Cos(Math.PI / 180 * (angle - 270));
}
return new PointF(center.X + w, center.Y + h);
}
///
/// 计算两个坐标的夹角
///
/// 坐标1
/// 坐标2
///
public static double CalculateAngleForPoint(PointF point1, PointF point2)
{
double tmp = Math.Atan2((point2.Y - point1.Y), (point2.X - point1.X)) * 180 / Math.PI;
if (tmp < 0)
{
tmp = 180 + 180 - Math.Abs(tmp);
}
return tmp;
}
///
/// 获取Size旋转指定角度后新的Size
///
/// 源Size
/// 指定角度
/// 新的Size
public static SizeF ConvertToAngleSize(SizeF size, float angle)
{
PointF angle_point = ControlHelper.CalculatePointForAngle(new PointF(0, 0), size.Width / 2, (angle + 360) % 360);
PointF righttop_point = ControlHelper.CalculatePointForAngle(angle_point, size.Height / 2, (angle - 90 + 360) % 360);
PointF rightbottom_point = ControlHelper.CalculatePointForAngle(angle_point, size.Height / 2, (angle + 90 + 360) % 360);
float w = Math.Max(Math.Abs(righttop_point.X), Math.Abs(rightbottom_point.X)) * 2;
float h = Math.Max(Math.Abs(righttop_point.Y), Math.Abs(rightbottom_point.Y)) * 2;
return new SizeF(w, h);
}
#endregion
#region 画笔Rectangle转换
///
/// 根据画笔大小转换rectf
///
/// 要转换的rectf
/// 画笔大小大小
///
public static RectangleF TransformRectangleByPen(RectangleF rectf, float pen)
{
RectangleF result = new RectangleF();
result.Width = rectf.Width - (pen < 1 ? 0 : pen);
result.Height = rectf.Height - (pen < 1 ? 0 : pen);
result.X = rectf.X + (float)(pen / 2);
result.Y = rectf.Y + (float)(pen / 2);
return result;
}
#endregion
#region 颜色
///
/// 转换成灰色颜色值
///
/// 要转换的颜色
///
public static Color ConvertToDisableColor(Color color)
{
int tmp = (color.R + color.G + color.B) / 3;
return Color.FromArgb(color.A, tmp, tmp, tmp);
}
///
/// 根据控件Enabled状态转换成对应颜色值
///
/// 颜色所属的控件
/// 要转换的颜色
///
public static Color ConvertToAutoColor(Control control, Color color)
{
if (control == null || control.Enabled)
{
return color;
}
return ConvertToDisableColor(color);
}
///
/// 根据状态转换成对应颜色值
///
/// 是否为禁用状态
/// 要转换的颜色
///
public static Color ConvertToAutoColor(bool disable, Color color)
{
if (!disable)
{
return color;
}
return ConvertToDisableColor(color);
}
///
/// 转换成动画效果颜色值
///
/// 要转换的颜色
///
public static Color ConvertToAnimationColor(Color color)
{
if (color.GetBrightness() >= 0.5)
{
Color tmp = ControlPaint.Dark(color);
return Color.FromArgb(15, tmp.R, tmp.G, tmp.B);
}
else
{
Color tmp = ControlPaint.Light(color);
return Color.FromArgb(80, tmp.R, tmp.G, tmp.B);
}
}
///
/// 根据控件Enabled状态和背景色转换文本字体对应颜色值
///
/// 颜色所属的控件
/// 背景色
/// 前景色
///
public static Color ConvertToForeColorByBackColor(Control control, Color backColor, Color foreColor)
{
if (control == null || control.Enabled)
{
return foreColor;
}
Color tmp = ConvertToAutoColor(control, backColor);
if (Math.Abs(tmp.GetBrightness() - foreColor.GetBrightness()) < 0.2)
{
return tmp.GetBrightness() >= 0.5 ? ControlPaint.Dark(backColor) : ControlPaint.Light(backColor);
}
else
{
return ConvertToDisableColor(foreColor);
}
}
///
/// 根据状态和背景色转换文本字体对应颜色值
///
/// 是否为禁用状态
/// 背景色
/// 前景色
///
public static Color ConvertToForeColorByBackColor(bool disable, Color backColor, Color foreColor)
{
if (!disable)
{
return foreColor;
}
Color tmp = ConvertToAutoColor(disable, backColor);
if (Math.Abs(tmp.GetBrightness() - foreColor.GetBrightness()) < 0.2)
{
return tmp.GetBrightness() >= 0.5 ? ControlPaint.Dark(backColor) : ControlPaint.Light(backColor);
}
else
{
return ConvertToDisableColor(foreColor);
}
}
///
/// 根据指定颜色Brightness转换成有对比颜色
///
/// 指定颜色
///
public static Color ConvertToDarkOrLightColor(Color color)
{
return ((double)color.GetBrightness() >= 0.5) ? ControlPaint.Dark(color) : ControlPaint.Light(color);
}
///
/// 检查RGB值ed有效范围
///
///
///
public static int VerifyRGB(int rgb)
{
if (rgb < 0)
return 0;
if (rgb > 255)
return 255;
return rgb;
}
#endregion
#region 图片
private static ColorMatrix disabledImageColorMatrix;
///
/// 用于创建禁用状态灰色图片(NET源码拷贝)
///
private static ColorMatrix DisabledImageColorMatrix
{
get
{
if (disabledImageColorMatrix == null)
{
float[][] greyscale = new float[5][];
greyscale[0] = new float[5] { 0.2125f, 0.2125f, 0.2125f, 0, 0 };
greyscale[1] = new float[5] { 0.2577f, 0.2577f, 0.2577f, 0, 0 };
greyscale[2] = new float[5] { 0.0361f, 0.0361f, 0.0361f, 0, 0 };
greyscale[3] = new float[5] { 0, 0, 0, 1, 0 };
greyscale[4] = new float[5] { 0.38f, 0.38f, 0.38f, 0, 1 };
float[][] transparency = new float[5][];
transparency[0] = new float[5] { 1, 0, 0, 0, 0 };
transparency[1] = new float[5] { 0, 1, 0, 0, 0 };
transparency[2] = new float[5] { 0, 0, 1, 0, 0 };
transparency[3] = new float[5] { 0, 0, 0, .7F, 0 };
transparency[4] = new float[5] { 0, 0, 0, 0, 0 };
int size = 5;
float[][] result = new float[size][];
for (int row = 0; row < size; row++)
{
result[row] = new float[size];
}
float[] column = new float[size];
for (int j = 0; j < size; j++)
{
for (int k = 0; k < size; k++)
{
column[k] = transparency[k][j];
}
for (int i = 0; i < size; i++)
{
float[] row = greyscale[i];
float s = 0;
for (int k = 0; k < size; k++)
{
s += row[k] * column[k];
}
result[i][j] = s;
}
}
disabledImageColorMatrix = new ColorMatrix(result);
}
return disabledImageColorMatrix;
}
}
///
/// 创建禁用状态灰色图片(NET源码拷贝)
///
/// 要处理的图片
///
public static Image CreateDisabledImage(Image normalImage)
{
ImageAttributes imgAttrib = new ImageAttributes();
imgAttrib.ClearColorKey();
imgAttrib.SetColorMatrix(DisabledImageColorMatrix);
Size size = normalImage.Size;
Bitmap disabledBitmap = new Bitmap(size.Width, size.Height);
Graphics graphics = Graphics.FromImage(disabledBitmap);
graphics.DrawImage(normalImage, new Rectangle(0, 0, size.Width, size.Height), 0, 0, size.Width, size.Height, GraphicsUnit.Pixel, imgAttrib);
graphics.Dispose();
return disabledBitmap;
}
///
/// 缩放图片
///
/// 源图片
/// 目标Size
///
public static Image ScaleImage(Image sourceImage, Size targetSize)
{
if (sourceImage == null)
return null;
Bitmap bmp = new Bitmap(targetSize.Width, targetSize.Height);
Graphics g = Graphics.FromImage(bmp);
g.DrawImage(sourceImage, new Rectangle(0, 0, targetSize.Width, targetSize.Height), 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel);
g.Dispose();
return bmp;
}
#endregion
#region 小数位数截取
///
/// 保留指定位数小数
///
/// 要处理数
/// 要保留小数位数
public static float ReserveDecimals(float value, int places)
{
return (float)ReserveDecimals(value, places);
}
///
/// 保留指定位数小数
///
/// 要处理数
/// 要保留小数位数
public static double ReserveDecimals(double value, int places)
{
return (double)ReserveDecimals(value, places);
}
///
/// 保留指定位数小数
///
/// 要处理数
/// 要保留小数位数
public static string ReserveDecimals(string value, int places)
{
if (places < 0)
return value;
value = value.Trim();
int index = value.IndexOf('.');
if (places == 0 && index > -1)
{
return value.Substring(0, index);
}
if (index > -1)
{
value = value.Substring(0, Math.Min(index + places + 1, value.Length));
value = value.PadRight(value.IndexOf('.') + places + 1, '0');
}
else if (places > 0)
{
value = (value + ".").PadRight(value.Length + 1 + places, '0');
}
return value;
}
#endregion
///
/// 设置GDI高质量模式抗锯齿
///
///
public static void SetGDIHigh(this Graphics g)
{
//消除锯齿
g.SmoothingMode = SmoothingMode.AntiAlias;
//文字显示效果
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
//插补模式
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//图片呈现质量
g.CompositingQuality = CompositingQuality.HighQuality;
}
#region 设置控件Enabled,切不改变控件颜色
///
/// 功能描述:设置控件Enabled,切不改变控件颜色
/// 作 者:HZH
/// 创建日期:2019-03-04 13:43:32
/// 任务编号:POS
///
/// c
/// enabled
public static void SetControlEnabled(this Control c, bool enabled)
{
if (!c.IsDisposed)
{
if (enabled)
{
ControlHelper.SetWindowLong(c.Handle, -16, -134217729 & ControlHelper.GetWindowLong(c.Handle, -16));
}
else
{
ControlHelper.SetWindowLong(c.Handle, -16, 134217728 + ControlHelper.GetWindowLong(c.Handle, -16));
}
}
}
///
/// 功能描述:设置控件Enabled,切不改变控件颜色
/// 作 者:HZH
/// 创建日期:2019-03-04 13:43:32
/// 任务编号:POS
///
/// cs
/// enabled
public static void SetControlEnableds(Control[] cs, bool enabled)
{
for (int i = 0; i < cs.Length; i++)
{
Control c = cs[i];
SetControlEnabled(c, enabled);
}
}
#endregion
///
/// Sets the window long.
///
/// The h WND.
/// Index of the n.
/// The wndproc.
/// System.Int32.
[DllImport("user32.dll ")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int wndproc);
///
/// Gets the window long.
///
/// The h WND.
/// Index of the n.
/// System.Int32.
[DllImport("user32.dll ")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
///
/// Gets the foreground window.
///
/// IntPtr.
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetForegroundWindow();
///
/// Threads the base call back.
///
/// The parent.
/// The object.
private static void ThreadBaseCallBack(Control parent, object obj)
{
if (obj is Exception)
{
if (parent != null)
{
ThreadInvokerControl(parent, delegate
{
Exception ex = obj as Exception;
});
}
}
}
///
/// 委托调用主线程控件
///
/// 主线程控件
/// 修改控件方法
public static void ThreadInvokerControl(Control parent, Action action)
{
if (parent != null)
{
if (parent.InvokeRequired)
{
parent.BeginInvoke(action);
}
else
{
action();
SetForegroundWindow(parent.Handle);
}
}
}
///
/// 使用一个线程执行一个操作
///
/// 父控件
/// 执行内容
/// 执行后回调
/// 执行期间禁用控件列表
/// 执行期间是否显示等待提示
/// 执行期间等待提示内容,默认为“正在处理,请稍候...”
/// 延迟显示等待提示时间
public static void ThreadRunExt(
Control parent,
Action func,
Action