日期:2014-05-18 浏览次数:20972 次
//自定义绘图类,里面包含以下代码
    public interface IMyDraw   //绘图接口
    {
        void Draw(System.Drawing.Graphics g);
    }
    public class MyDraw_Circle : IMyDraw  //绘制圆形类
    {
        public void Draw(System.Drawing.Graphics g)
        {
            g.DrawEllipse(System.Drawing.Pens.Red, new System.Drawing.Rectangle(0, 0, 100, 100));
        }
    }
    public class MyDraw_Rect : IMyDraw  //绘制矩形类
    {
        public void Draw(System.Drawing.Graphics g)
        {
            g.DrawRectangle(System.Drawing.Pens.Red, new System.Drawing.Rectangle(0, 0, 100, 100));
        }
    }
//自定义组件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class UserControl1 : UserControl
    {
        IMyDraw _mydraw;
        public UserControl1()
        {
            InitializeComponent();
        }
        public UserControl1(IMyDraw mydraw)  //自定义构造,传入绘图接口
        {
            _mydraw = mydraw;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            _mydraw.Draw(e.Graphics); //调用接口的Draw
            base.OnPaint(e);
        }
    }
}
调用
            UserControl1 u = new UserControl1(new MyDraw_Circle());//这个绘制圆形
            u.Parent = this;
            u.Left = 0;
            u.Top = 300;
            u = new UserControl1(new MyDraw_Rect());//这个绘制矩形
            u.Parent = this;
            u.Left = 200;
            u.Top = 300;
------解决方案--------------------
很难想象是啥错,单从毛病上起码可以想到5,6种常见的错误
1.是lz自己提到的静态
2.直接引用了对象,而不是new滴
3.公用了数据源
4.dc获取错了??
5 或者使用了非线程安全的东西,两线程互相串改数据??