C# 如何使用pictureBox1创建用户控件,以便以后在任何项目中使用它?

C# 如何使用pictureBox1创建用户控件,以便以后在任何项目中使用它?,c#,winforms,C#,Winforms,首先,我添加了许多属性,但这不是传递变量的正确方法。我该怎么处理 其次,此控件将像form1designer(或任何其他表单设计器)中的pictureBox1一样使用。在表格1中,我使用了一些pictureBox1的事件。现在我需要在控件中使用picturebox1的事件,那么我需要创建并使用控件代码中的所有事件而不是form1吗 这是用户控制代码: namespace Find_Distance { public partial class pictureBox1Control : U

首先,我添加了许多属性,但这不是传递变量的正确方法。我该怎么处理

其次,此控件将像
form1d
esigner(或任何其他表单设计器)中的
pictureBox1
一样使用。在表格1中,我使用了一些pictureBox1的事件。现在我需要在控件中使用
picturebox1
的事件,那么我需要创建并使用控件代码中的所有事件而不是form1吗

这是用户控制代码:

namespace Find_Distance
{
    public partial class pictureBox1Control : UserControl
    {

        public static PictureBox pb1;
        // blinking colors: yellow, red, yellow, transparent, repeat...
        public static Brush[] cloudColors = new[] { Brushes.Yellow, Brushes.Transparent };
        // current color index
        public static int cloudColorIndex = 0;

        public pictureBox1Control()
        {
            InitializeComponent();

            pb1 = new PictureBox();
            this.pictureBox1 = pb1;
            SetStyle(
            ControlStyles.AllPaintingInWmPaint |

            ControlStyles.OptimizedDoubleBuffer |
            ControlStyles.UserPaint |
            ControlStyles.ResizeRedraw, true);
        }

        private void pictureBox1Control_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;

            e.Graphics.Clear(Color.White);
            e.Graphics.DrawImage(pictureBox1Control.pb1.Image, movingPoint);
            CloudEnteringAlert.Paint(e.Graphics, currentfactor, _distance);

            float distance = _kilometers / (float)1.09;//289617486; // One pixel distance is 1.09 kilometer.
            Pen p;
            p = new Pen(Brushes.Green);
            if (_points == null)
            {
                return;
            }
            foreach (Point pt in _pointsint)
            {
                e.Graphics.FillEllipse(Brushes.Red, pt.X * (float)currentfactor, pt.Y * (float)currentfactor, 2f, 2f);
            }
            foreach (PointF pt in _movingpoints)
            {
                if (_Cloud == null)
                {
                    e.Graphics.FillEllipse(Brushes.Red, (pt.X - distance) * (float)currentfactor, pt.Y * (float)currentfactor, 2f, 2f);
                }
            }
            foreach (PointF pt in _pointtocolor)
            {
                e.Graphics.FillEllipse(Brushes.Yellow, pt.X * (float)currentfactor, pt.Y * (float)currentfactor, 2f, 2f);
            }
            if (_Cloud != null)
            {
                foreach (PointF pt in _Cloud)
                {
                    e.Graphics.FillEllipse(cloudColors[cloudColorIndex], pt.X * (float)currentfactor, pt.Y * (float)currentfactor, 2f, 2f);
                }
            }
            else
            {
                return;
            }

            foreach (var cloud in _clouds)
            {
                e.Graphics.FillEllipse(
                   cloud.Brush, cloud.Center.X, cloud.Center.Y,
                   cloud.Diameter, cloud.Diameter);
            }
        }

        public static List<Ellipse> _clouds = new List<Ellipse>();
        public List<Ellipse> Clouds
        {
            get { return _clouds; }
        }

        public static List<PointF> _points = new List<PointF>();
        public List<PointF> Points
        {
            get { return _points; }
        }

        public static List<Point> _pointsint = new List<Point>();
        public List<Point> PointsInt
        {
            get { return _pointsint; }
        }

        public static List<PointF> _movingpoints = new List<PointF>();
        public List<PointF> MovingPoints
        {
            get { return _movingpoints; }
        }

        public static List<PointF> _pointtocolor = new List<PointF>();
        public List<PointF> PointtoColor
        {
            get { return _pointtocolor; }
        }

        public static List<PointF> _Cloud = new List<PointF>();
        public List<PointF> Cloud
        {
            get { return _Cloud; }
        }

        public static float _kilometers;
        public float Kilometers
        {
            get { return _kilometers; }
        }

        public static float _distance;
        public float Distance
        {
            get { return _distance; }
        }

        public static Point movingPoint;
        public static double currentfactor;
        public static float distance;
    }
}
但它不会在控件的pictureBox1上显示图像。

而不是使用:

pictureBox1Control.pb1.Image = test.jpg;
试用

pictureBox1Control.pb1.Load(@"c:\test.jpg");
当然,请确保该文件存在。此外,还应删除此行中的“static”:

 public static PictureBox pb1;
如果打开了10个窗口,则可能不希望显示所有相同的图像。我看到您的PictureBox对象没有添加到控件中。在第一种方法中,创建PictureBox后,使用

this.Controls.Add(pb1)
this.Controls.Add(pb1)