用户控件和用户组件

用户控件和用户组件

代码如下:

1、用户组件代码:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ControlAndModule
{
    [DefaultEvent("OnValueChange")]
    public  class UserComponentModel: Component
    {
       private bool state;
    private EventHandler handler;
    private static object EventValueChanged = new object();
        public delegate void  ThisValueChange(string _value);

        [Category("WeiheSoft"), Browsable(true), Description("测试的值"),]
        public event ThisValueChange OnValueChange;
    public UserComponentModel() {
    }

        [Category("WeiheSoft"),Browsable(true ),Description ("测试的值")]
    public bool Value {
      get {
        return state;
      }
      set {
        if (this.state != value) {
          this.state = value;
          OnValueChanged(new EventArgs());
                    OnValueChange(value.ToString ());
        }
      }
    }

    protected virtual void OnValueChanged(EventArgs e) {
      if (handler != null) {
        handler(this, e);
      }
    }

    //public void RemoveOnValueChanged(EventHandler h) {
    //  handler = (EventHandler)Delegate.Remove(handler, h);
    //}
        //public void AddOnValueChanged(EventHandler h) {
        //  handler = (EventHandler)Delegate.Combine(handler, h);
        //}
    }
}

 

2、用户控件代码:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;

namespace ControlAndModule
{
    public partial class UserControlDraw : UserControl
    {
        private ArrayList brushArray = new ArrayList();
        private ArrayList fontArray = new ArrayList();
        public UserControlDraw()
        {
            InitializeComponent();
            //创建字符串格式
            fontArray.Add(new Font("Ariel", 8, FontStyle.Bold));
            fontArray.Add(new Font("Courier", 8, FontStyle.Italic));
            fontArray.Add(new Font("Veranda", 8, FontStyle.Bold));
            fontArray.Add(new Font("System", 8, FontStyle.Strikeout));
            fontArray.Add(new Font("Century SchoolBook", 8, FontStyle.Underline));
            fontArray.Add(new Font("Helevctia", 8, FontStyle.Italic));
            //创建画刷
            brushArray.Add(new SolidBrush(Color.Red));
            brushArray.Add(new SolidBrush(Color.Blue));
            brushArray.Add(new SolidBrush(Color.Green));
            brushArray.Add(new SolidBrush(Color.Yellow));
            brushArray.Add(new SolidBrush(Color.Black));
            brushArray.Add(new SolidBrush(Color.Azure));
            brushArray.Add(new SolidBrush(Color.Firebrick));
            brushArray.Add(new SolidBrush(Color.DarkMagenta));
            brushArray.Add(new SolidBrush(Color.DarkTurquoise));
            brushArray.Add(new SolidBrush(Color.Khaki));
            //画comboBox1,注意它要调用comboBox1_DrawItem来画
            comboBox1.Items.Add("中国");
            comboBox1.Items.Add("巴西");
            comboBox1.Items.Add("哥斯达黎加");
            comboBox1.Items.Add("土耳其");
            comboBox1.Items.Add("韩国");
            comboBox1.Items.Add("日本");
            //画comboBox2,注意它要调用comboBox2_DrawItem来画
            comboBox2.Items.Add("");
            comboBox2.Items.Add("");
            comboBox2.Items.Add("");
            comboBox2.Items.Add("");
            comboBox2.Items.Add("");
            comboBox2.Items.Add("");
            comboBox2.Items.Add("");
            comboBox2.Items.Add("");
            comboBox2.Items.Add("");
            comboBox2.Items.Add("");
            //画comboBox3,注意它要调用comboBox3_DrawItem来画
            comboBox3.Items.Add("飞机");
            comboBox3.Items.Add("大炮");
            comboBox3.Items.Add("汽车");
            comboBox3.Items.Add("坦克");

            this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
            this.comboBox2.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
            this.comboBox3.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
            this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
        }
        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {

            Graphics g = e.Graphics;
            //绘制区域
            Rectangle r = e.Bounds;
            Font fn = null;
            if (e.Index >= 0)
            {
                //设置字体、字符串格式、对齐方式
                fn = (Font)fontArray[e.Index];
                string s = (string)comboBox1.Items[e.Index];
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                //根据不同的状态用不同的颜色表示
                if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.Red), r);
                    e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r, sf);
                    e.DrawFocusRectangle();
                }
                else
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
                    e.Graphics.DrawString(s, fn, new SolidBrush(Color.Red), r, sf);
                    e.DrawFocusRectangle();
                }
            }
        }

        private void comboBox2_DrawItem(object sender, DrawItemEventArgs e)
        {
            Graphics g = e.Graphics;
            Rectangle r = e.Bounds;
            if (e.Index >= 0)
            {
                //设置字符串前矩形块rd的大小
                Rectangle rd = r;
                rd.Width = rd.Left + 20;
                Rectangle rt = r;
                r.X = rd.Right;
                //用不同的颜色画矩形块
                SolidBrush b = (SolidBrush)brushArray[e.Index];
                g.FillRectangle(b, rd);
                //设置字符串的格式
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
                {
                    //字符串背景
                    e.Graphics.FillRectangle(new SolidBrush(Color.White), r);
                    //显示字符串
                    e.Graphics.DrawString(b.Color.Name, new Font("Ariel", 8, FontStyle.Bold), new SolidBrush(Color.Black), r, sf);
                    //绘制取得焦点时的虚线框
                    e.DrawFocusRectangle();
                }
                else
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
                    e.Graphics.DrawString(b.Color.Name, new Font("Veranda", 8, FontStyle.Bold), new SolidBrush(Color.Red), r, sf);
                    e.DrawFocusRectangle();
                }
            }
        }

        private void comboBox3_DrawItem(object sender, DrawItemEventArgs e)
        {
            Rectangle r = e.Bounds;
            Size imageSize = imageList1.ImageSize;
            Font fn = null;
            if (e.Index >= 0)
            {
                fn = (Font)fontArray[0];
                string s = (string)comboBox3.Items[e.Index];
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
                {
                    //画条目背景
                    e.Graphics.FillRectangle(new SolidBrush(Color.Red), r);
                    //绘制图像
                    imageList1.Draw(e.Graphics, r.Left, r.Top, e.Index);
                    //显示字符串
                    e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r.Left + imageSize.Width, r.Top);
                    //显示取得焦点时的虚线框
                    e.DrawFocusRectangle();
                }
                else
                {
                    e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
                    imageList1.Draw(e.Graphics, r.Left, r.Top, e.Index);
                    e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r.Left + imageSize.Width, r.Top);
                    e.DrawFocusRectangle();
                }
            }

        }
    }
}

3、测试代码:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace ControlAndModule
{
    public partial class FrmTest : Form
    {
        public FrmTest()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.userComponentModel1.Value = !this.userComponentModel1.Value;
        }

        private void userComponentModel1_OnValueChange(string _value)
        {
            MessageBox.Show(_value);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.userControlDraw1.comboBox1.DroppedDown = true;
            this.userControlDraw1.comboBox2.DroppedDown = true;
            this.userControlDraw1.comboBox3.DroppedDown = true;

            this.userControlDraw1.comboBox1.SelectedIndex = 2;
            this.userControlDraw1.comboBox2.SelectedIndex = 2;
            this.userControlDraw1.comboBox3.SelectedIndex = 2;
        }

 
    }
}

 

效果如下:

 

用户组件和控件相关属性说明:

属性名 说明
BrowsableAttribute控制属性或事件是否显示在属性浏览器中。
BindableAttribute确定属性是否适合数据绑定器进行绑定。
CategoryAttribute指定属性在属性浏览器中应分组的类别(“Appearance”,
“Layout”, “Behavior”,“ Misc”等等)。
DefaultEventAttribute/ DefaultPropertyAttribute 指定对象的默认事
件或属性。
HelpAttribute 指定属性或事件的帮助文件和主题。
LicenseProviderAttribute 指向为组件提供许可证信息的 License
Provider。
MergablePropertyAttribute 在属性浏览器中当多个组件被浏览和选中时,
允许或阻止包含某属性。
PersistableAttribute 确定在 Win Forms Designer 或 Component
Designer 等可视设计器中生成代码时,属性值是否应与代码保持一致。
PersistContentsAttribute 确定代码生成是否应回归到对象的非数值类
型属性以及是否保持代码与属性值一致。ICollection 属性类型是这一应
用的典型示例。
ShowInToolboxAttribute 确定是否允许在工具框中使用这一组件。
ToolBoxItemAttriubte 指定从工具框中创建类时应使用的 ToolboxItem
类型。

posted @ 2009-12-30 18:00  星释天狼  阅读(423)  评论(0编辑  收藏  举报