如何创建pictureBox用户控件?

时间:2014-01-11 14:00:34

标签: c# winforms

这就是我在用户控制代码中所做的: 我在我的项目中添加了一个用户控件。

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

namespace Find_Distance
{
    public partial class pictureBox1Control : UserControl
    {
        public pictureBox1Control()
        {
            InitializeComponent();

            SetStyle(
            ControlStyles.AllPaintingInWmPaint |
            ControlStyles.OptimizedDoubleBuffer |
            ControlStyles.UserPaint |
            ControlStyles.ResizeRedraw, true);

        }

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

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;

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

            base.OnPaint(e);
        }


        private void pictureBox1Control_Load(object sender, EventArgs e)
        {

        }
    }
}

但是当我在form1中使用它时,例如:

pictureBox1Control.Image

Image属性不存在。 我需要将此控件用作常规pictureBox1以及其他内容。

编辑**

在pictureBox中添加了paint事件:

pictureBox1 = new pictureBox1Control();
pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);

但它永远不会进入油漆事件:

 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(pictureBox1.Image, movingPoint);
     CloudEnteringAlert.Paint(e.Graphics, currentfactor, distance);   
 }

为什么活动永远不会开火?

2 个答案:

答案 0 :(得分:1)

然后你应该继承PictureBox类。按如下方式声明您的课程:

public partial class MyPictureBox : PictureBox

然后,您可以创建此类的实例并使用Image属性(或使用设计在表单上添加该图片框):

MyPictureBox pictureBox1Control = new MyPictureBox();
pictureBox1Control.Image...

答案 1 :(得分:0)

您继承的UserControl没有Image属性。要解决此问题,您可以继承PictureBox或自己动手。