這就是我在用戶控制代碼中所做的: 我在我的項目中添加了一個用戶控件。我如何創建一個pictureBox用戶控件?
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)
{
}
}
}
但是,當IM在Form1使用它例如:
pictureBox1Control.Image
Image屬性是不存在的。 我需要使用此控件作爲常規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);
}
爲什麼事件永遠不會火?
您的班級沒有圖片屬性。所以*當然*你無法在屬性窗口或IntelliSense中找到它。添加圖片屬性。 –