CustomControl

From no name for this wiki
Jump to: navigation, search

Windows Forms CustomControl mit SmartTag.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms.Design;
using System.ComponentModel;
using System.ComponentModel.Design;
 
namespace XamlReader
{
    [DesignerAttribute(typeof(CustomCompDesigner))]
    class CustomComp : Control
    {       
 
        Color gradientColor = Color.Blue;
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = this.CreateGraphics();
            Brush brush = this.CreateBrush();
           g.FillRectangle(brush, 0, 0, this.Width, this.Height);
        }
 
        public Brush CreateBrush()
        {
            LinearGradientBrush brush  =
                new LinearGradientBrush(new Point(0, 0), new Point(100, 0),
                    this.gradientColor, Color.Azure);
            return brush;
        }
 
        public Color GradientColor
        {
            get
            {
                return this.gradientColor;
            }
            set
            {
                this.gradientColor = value;
            }
        }
    }
 
    public class CustomCompDesigner : ControlDesigner {
 
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                DesignerActionListCollection lists = new DesignerActionListCollection();
                DesignerActionList list = new CustomCompActionList(this.Component);
 
                lists.Add(list);
                list.AutoShow = true;
 
                return lists;
            }
        }
 
    }
 
    public class CustomCompActionList : DesignerActionList
    {
 
 
 
        public CustomCompActionList(IComponent component) : base(component)
        {
            this.AutoShow = true;   
        }
 
        public override DesignerActionItemCollection GetSortedActionItems()
        {
            DesignerActionItemCollection items = new DesignerActionItemCollection();
            DesignerActionPropertyItem item = new DesignerActionPropertyItem("GradientColor", "Some displayname");
            items.Add(item);
            return items;
        }
 
        public Color GradientColor
        {
            get {
                CustomComp ccomp = (CustomComp)this.Component;
                return ccomp.GradientColor;
            }
            set {
                CustomComp ccomp = (CustomComp)this.Component;
                ccomp.GradientColor = value;
            }
        }
    }
}