Color selection beautify controls ---------- WinForm Control Development Series

Here is the source code control to display the interface, select the date of the panel too much about 2000 lines of source code, not posted, you can download the source code to see. You can improve the color controls.

///  <Summary> 
  /// color selection control landscaping
   ///  </ Summary> 
  [the ToolboxItem ( to true )]
  [DefaultProperty("ColorPicker")]
  [The Description ( " color selection beautification Control " )]
   public  partial  class ColorExt: Control
  {
    #region

    Private  BOOL backBorderShow = to true ;
     ///  <Summary> 
    /// Whether to show border
     ///  </ Summary> 
    [the DefaultValue ( to true )]
    [The Description ( " Whether to show border " )]
     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)]
    [The Description ( " color location " )]
     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")]
    [The Description ( " Border Color " )]
    [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> 
    /// color selection panel
     ///  </ Summary> 
    [the Browsable ( to true )]
    [The Description ( " Color Palette " )]
    [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> 
    /// background
     ///  </ Summary> 
    Private  static Image back_image = Global :: AnimationLibrary.Properties.Resources.squaresback;
     ///  <Summary> 
    /// background tile 
     ///  </ Summary> 
    Private the ImageAttributes back_image_ia = new new the ImageAttributes ();
     ///  <Summary> 
    /// color panel display state
     ///  </ Summary> 
    Private  BOOL DisplayStatus = to false ;

    private ToolStripDropDown tsdd = null;

    private ToolStripControlHost ch = 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> 
    /// position of the color
     ///  </ Summary> 
    [the Description ( " color location " )]
     public  enum ColorAnchors
    {
      /// <summary>
      /// 左边
      /// </summary>
      Left,
      /// <summary>
      /// 右边
      /// </summary>
      Right
    }

  }

Source Downloads: color selection controls beautification .zip

Guess you like

Origin www.cnblogs.com/tlmbem/p/11520981.html