下面是控件显示界面源码,日期选择面板源码太多了2000行左右,没有贴出来,可以下载源码去看。你可以完善这个颜色控件。
/// <summary> /// 颜色选择美化控件 /// </summary> [ToolboxItem(true)] [DefaultProperty("ColorPicker")] [Description("颜色选择美化控件")] public partial class ColorExt : Control { #region private bool backBorderShow = true; /// <summary> /// 是否显示边框 /// </summary> [DefaultValue(true)] [Description("是否显示边框")] public bool BackBorderShow { get { return this.backBorderShow; } set { if (this.backBorderShow == value) return; this.backBorderShow = value; this.Invalidate(); } } private ColorAnchors colorAnchor = ColorAnchors.Right; /// <summary> /// 颜色位置 /// </summary> [DefaultValue(ColorAnchors.Right)] [Description("颜色位置")] public ColorAnchors ColorAnchor { get { return this.colorAnchor; } set { if (this.colorAnchor == value) return; this.colorAnchor = value; this.Invalidate(); } } private Color backBorderColor = Color.FromArgb(192, 192, 192); /// <summary> /// 边框颜色 /// </summary> [DefaultValue(typeof(Color), "192, 192, 192")] [Description("边框颜色")] [Editor(typeof(ColorEditorExt), typeof(System.Drawing.Design.UITypeEditor))] public Color BackBorderColor { get { return this.backBorderColor; } set { if (this.backBorderColor == value) return; this.backBorderColor = value; this.Invalidate(); } } private ColorPickerExt colorPicker = null; /// <summary> /// 颜色选择面板 /// </summary> [Browsable(true)] [Description("颜色选择面板")] [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public ColorPickerExt ColorPicker { get { return this.colorPicker; } set { this.colorPicker = value; } } protected override Size DefaultSize { get { return new Size(130, 23); } } protected override Cursor DefaultCursor { get { return Cursors.Hand; } } /// <summary> /// 背景图 /// </summary> private static Image back_image = global::AnimationLibrary.Properties.Resources.squaresback; /// <summary> ///背景图平铺 /// </summary> private ImageAttributes back_image_ia = new ImageAttributes(); /// <summary> /// 颜色面板显示状态 /// </summary> private bool DisplayStatus = false; private ToolStripDropDown tsdd = null; private ToolStripControlHost tsch = null; private readonly StringFormat color_sf = new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center, Trimming = StringTrimming.None, FormatFlags = StringFormatFlags.NoWrap }; #endregion public ColorExt() { SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.AllPaintingInWmPaint, true); SetStyle(ControlStyles.OptimizedDoubleBuffer, true); SetStyle(ControlStyles.ResizeRedraw, true); SetStyle(ControlStyles.SupportsTransparentBackColor, true); InitializeComponent(); this.BackColor = Color.FromArgb(255, 255, 255); this.ForeColor = Color.FromArgb(105, 105, 105); this.Font = new Font("微软雅黑", 9); this.back_image_ia.SetWrapMode(WrapMode.Tile); this.ColorPicker = new ColorPickerExt(); this.tsdd = new ToolStripDropDown() { Padding = Padding.Empty }; this.tsch = new ToolStripControlHost(this.ColorPicker) { Margin = Padding.Empty, Padding = Padding.Empty }; tsdd.Items.Add(this.tsch); this.tsdd.Closed += new ToolStripDropDownClosedEventHandler(this.tsdd_Closed); this.ColorPicker.ValueChanged += new EventHandler(this.ColorPicker_ValueChanged); this.ColorPicker.ClearClick += new EventHandler(this.ColorPicker_ClearClick); this.ColorPicker.ConfirmClick += new EventHandler(this.ColorPicker_ConfirmClick); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; if (this.BackBorderShow) { Pen backborder_pen = new Pen(this.BackBorderColor, 1); g.DrawRectangle(backborder_pen, new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1)); backborder_pen.Dispose(); } int color_width = (int)(g.ClipBounds.Height - 4); int color_height = (int)(g.ClipBounds.Height - 4); Rectangle color_rect = new Rectangle(e.ClipRectangle.Right - color_width - 2, e.ClipRectangle.Y + (e.ClipRectangle.Height - color_height) / 2, color_width, color_height); Rectangle argb_rect = new Rectangle(e.ClipRectangle.X + 5, e.ClipRectangle.Y, e.ClipRectangle.Width - color_width - 12, e.ClipRectangle.Height); if (this.ColorAnchor == ColorAnchors.Left) { color_rect = new Rectangle(e.ClipRectangle.X + 2, (e.ClipRectangle.Y + e.ClipRectangle.Height - color_height) / 2, color_width, color_height); argb_rect = new Rectangle(e.ClipRectangle.X + 2 + color_width + 5, e.ClipRectangle.Y, e.ClipRectangle.Width - color_width - 12, e.ClipRectangle.Height); } g.DrawImage(back_image, color_rect, 0, 0, color_rect.Width, color_rect.Height, GraphicsUnit.Pixel, this.back_image_ia); if (this.ColorPicker.Value != Color.Empty) { SolidBrush argb_sb = new SolidBrush(this.ColorPicker.Value); g.FillRectangle(argb_sb, color_rect); argb_sb.Color = this.ForeColor; string argb_format = String.Format("{0},{1},{2},{3}", this.ColorPicker.Value.A, this.ColorPicker.Value.R, this.ColorPicker.Value.G, this.ColorPicker.Value.B); g.DrawString(argb_format, this.Font, argb_sb, argb_rect, color_sf); argb_sb.Dispose(); } } protected override void OnClick(EventArgs e) { base.OnClick(e); if (!this.DisplayStatus) { tsdd.Show(this.PointToScreen(new Point(0, this.Height + 2))); this.ColorPicker.InitializeColor(); } } protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); if (this.color_sf != null) this.color_sf.Dispose(); if (this.back_image_ia != null) this.back_image_ia.Dispose(); } base.Dispose(disposing); } private void ColorPicker_ValueChanged(object sender, EventArgs e) { this.Invalidate(); } private void ColorPicker_ClearClick(object sender, EventArgs e) { this.tsdd.Close(); this.DisplayStatus = false; this.Invalidate(); } private void ColorPicker_ConfirmClick(object sender, EventArgs e) { this.tsdd.Close(); this.DisplayStatus = false; } private void tsdd_Closed(object sender, ToolStripDropDownClosedEventArgs e) { this.Invalidate(); this.DisplayStatus = false; } /// <summary> /// 颜色位置 /// </summary> [Description("颜色位置")] public enum ColorAnchors { /// <summary> /// 左边 /// </summary> Left, /// <summary> /// 右边 /// </summary> Right } }
源码下载:颜色选择美化控件.zip
原文地址:https://www.cnblogs.com/tlmbem/p/11520981.html
时间: 2024-10-05 03:31:46