Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何创建pictureBox用户控件?_C#_Winforms - Fatal编程技术网

C# 如何创建pictureBox用户控件?

C# 如何创建pictureBox用户控件?,c#,winforms,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; usin

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

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)
        {

        }
    }
}
图像属性不存在。 我需要将此控件用作常规pictureBox1,也用于其他用途

编辑**

将绘制事件添加到pictureBox:

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);   
 }

为什么事件从未触发?

那么您应该从PictureBox类继承。按如下方式申报您的班级:

public partial class MyPictureBox : PictureBox
然后,您可以创建此类的实例并使用Image属性(或使用设计用于在表单上添加该picturebox的):


您继承的是
UserControl
,它没有
Image
属性。要解决此问题,您可以从
PictureBox继承,也可以自己滚动。

您的类没有图像属性。因此,您当然无法在属性窗口或IntelliSense中找到它。添加一个Image属性。Cosmin在哪里声明MyPictureBox公共分部类?我在pictureBox1Control代码中声明它?在您编写的代码中,您需要更改以下行:公共部分类pictureBox1Control:UserControl,而不是UserControl,您应该让PictureBoxMin查看我的编辑现在请查看我在用户控件设计器代码中的两个错误。确定它现在工作没有错误。但我在控制面板上没有看到任何东西。没有什么是吸引。我是否需要添加到pictureBox事件中,如paint?我是否还需要添加到用户控件设计器pictureBox控件?pictureBox也没有AutoScaleMode和AutoScaleDimensions属性。这通常设置在控件的容器上,而不是控件本身上。
public partial class MyPictureBox : PictureBox
MyPictureBox pictureBox1Control = new MyPictureBox();
pictureBox1Control.Image...