110,536
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Demo_1
{
public partial class Form1 : Form
{
public class PanelEx : Panel
{
public PanelEx()
{
SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor |
ControlStyles.UserPaint,
true
);
UpdateStyles();
}
}
public abstract class DraggableObject
{
public abstract Rectangle Region { get; set; }
public abstract Bitmap Setimage { get; set; }
public abstract void OnPaint(PaintEventArgs e);
}
public class Draggable : DraggableObject
{
private Rectangle m_Region;
private Bitmap m_SetImage;
public Draggable(string id, Rectangle regin, Bitmap image)
{
m_Region = regin;
m_SetImage = image;
}
public override Rectangle Region { get => m_Region; set => m_Region = value; }
public override Bitmap Setimage { get => m_SetImage; set => m_SetImage = value; }
public override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(m_SetImage, m_Region);
}
}
public List<DraggableObject> DesignObjects = new List<DraggableObject>();
public PanelEx panelContent;
public Form1()
{
InitializeComponent();
panelContent = new PanelEx()
{
Dock = DockStyle.Fill,
BackgroundImageLayout = ImageLayout.Stretch,
BackgroundImage = Image.FromFile("back.jpg")
};
panelContent.Paint += PanelContent_Paint;
this.Controls.Add(panelContent);
string[] files = new string[] { "01.png", "02.png", "03.png" };
for (int i = 0; i < 3; i++)
{
Bitmap bmp = Image.FromFile(files[i]) as Bitmap;
Draggable draggableBlock = new Draggable(DateTime.Now.ToString(), new Rectangle(0, 0, bmp.Width, bmp.Height), bmp);
DesignObjects.Add(draggableBlock);
}
Task.Run(async () =>
{
int set_x = 0;
while (true)
{
DesignObjects[0].Region = new Rectangle(set_x, 100, DesignObjects[0].Region.Width, DesignObjects[0].Region.Height);
panelContent.Invalidate();
await Task.Delay(5);
if (set_x < this.Width) set_x += 1;
else set_x = 0;
}
});
Task.Run(async () =>
{
int set_y = 0;
while (true)
{
DesignObjects[1].Region = new Rectangle(100, set_y, DesignObjects[1].Region.Width, DesignObjects[1].Region.Height);
panelContent.Invalidate();
await Task.Delay(5);
if (set_y < this.Height) set_y += 1;
else set_y = 0;
}
});
Task.Run(async () =>
{
int set_x = 0, set_y = 0;
while (true)
{
DesignObjects[2].Region = new Rectangle(set_x, set_y, DesignObjects[2].Region.Width, DesignObjects[2].Region.Height);
panelContent.Invalidate();
await Task.Delay(5);
if ((set_y + 1 + DesignObjects[2].Region.Height) < this.Height)
{
set_x += 1;
set_y += 1;
}
else
{
set_x = 0;
set_y = 0;
}
}
});
}
private void PanelContent_Paint(object sender, PaintEventArgs e)
{
foreach (DraggableObject item in DesignObjects)
{
item.OnPaint(e);
}
}
}
}