using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; namespace DPumpHydr.WinFrmUI.WenSkin.Controls { public class WenControl : Control { public WenControl() { base.SetStyle( ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true); base.UpdateStyles(); this.BackColor = Color.Transparent; } #region 私有属性 private Color customForeColor = Color.White; private bool autoReverseForeColorColor; private Color backColor = Color.Transparent; private Color foreColor = Color.White; #endregion #region 公有属性 [Category("Wen")] [Description("当前用户控件信息")] [Bindable(true)] [Browsable(true)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] [EditorBrowsable(EditorBrowsableState.Always)] public override string Text { get => base.Text; set => base.Text = value; } [Browsable(false)] public Color CustomForeColor { get => customForeColor; set { customForeColor = value; base.ForeColor = value; this.Invalidate(); } } [Category("WenAuto"), Description("颜色是否自动反色"), DefaultValue(false)] public bool AutoReverseForeColorColor { get => autoReverseForeColorColor; set { autoReverseForeColorColor = value; this.Invalidate(); } } [DefaultValue(typeof(Color), "White"), Category("WenColor"), Description("前景色")] public override Color ForeColor { get => foreColor; set { foreColor = value; base.ForeColor = value; this.Invalidate(); } } [DefaultValue(typeof(Color), "Transparent"), Category("WenColor"), Description("背景色")] public override Color BackColor { get => this.backColor; set { //若为系统颜色改为酷黑色 if (value == System.Drawing.SystemColors.Control) { value = Color.Transparent; } base.BackColor = value; this.backColor = value; this.Invalidate(); } } #endregion #region 重绘 /// /// 自定义绘制窗体 /// /// protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.SmoothingMode = SmoothingMode.AntiAlias; //使绘图质量最高,即消除锯齿 g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.CompositingQuality = CompositingQuality.HighQuality; Rectangle rec = this.ClientRectangle; WenOnPaint(g, rec, e); base.OnPaint(e); } protected virtual void WenOnPaint(Graphics g, Rectangle rec, PaintEventArgs e) { } #endregion #region 绘制文字 /// /// 绘制文字上下居中靠左 /// /// /// /// public void DrawString(string s, Graphics g, RectangleF rec) { DrawString(s, g, rec, StringAlignment.Near); } /// /// 绘制文字上下居中 需要指定水平方向 /// /// /// /// /// public void DrawString(string s, Graphics g, RectangleF rec, StringAlignment stringAlignment) { g.DrawString(s, this.Font, new SolidBrush(this.ForeColor), rec, new StringFormat(StringFormatFlags.NoClip | StringFormatFlags.NoWrap) { LineAlignment = StringAlignment.Center, Alignment = stringAlignment, Trimming = StringTrimming.EllipsisCharacter }); } #endregion #region 消息提示框 /// /// 消息 /// /// /// public bool MsgBoxInformation(string text) { var m = new MsgBox(text, MsgBox.MsgBoxIcon.Info).ShowDialog(); if (m == DialogResult.OK) return true; else return false; } /// /// 提醒 /// /// /// public bool MsgBoxAsterisk(string text) { var m = new MsgBox(text, MsgBox.MsgBoxIcon.Asterisk).ShowDialog(); if (m == DialogResult.OK) return true; else return false; } /// /// 警告 /// /// /// public bool MsgBoxWarning(string text) { var m = new MsgBox(text, MsgBox.MsgBoxIcon.Warning).ShowDialog(); if (m == DialogResult.OK) return true; else return false; } /// /// 错误 /// /// /// public bool MsgBoxError(string text) { var m = new MsgBox(text, MsgBox.MsgBoxIcon.Error).ShowDialog(); if (m == DialogResult.OK) return true; else return false; } #endregion #region 前景色自动反色 protected override void OnBackColorChanged(EventArgs e) { base.OnBackColorChanged(e); if (AutoReverseForeColorColor) { Color color = this.BackColor; //颜色反色 this.ForeColor = ControlHelper.ColorReverse(color); //当颜色为透明时候 ,前景色改为默认前景色不改变 if (BackColor == Color.Transparent) this.ForeColor = CustomForeColor; } } #endregion } }