新建一个usercontral
添加图片前要先stop
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace usercontral1
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public UserControl1(Image[] img)
{
InitializeComponent();
Add(img);
}
bool state = false;
int pic_weith = 100;
int pic_heith = 100;
int time_time = 10;
int jiange_heith = 2;
int jiange_weith = 2;
public bool State
{
get { return state; }
set { state = value ;}
}
public int Pic_weith
{
get { return pic_weith; }
set { pic_weith = value ;}
}
public int Pic_heith
{
get { return pic_heith; }
set { pic_heith = value ;}
}
public int Timer_time
{
get { return time_time ; }
set { time_time = value; }
}
public int Jiange_weith
{
get { return jiange_weith ; }
set { jiange_weith = value; }
}
public int Jiange_heith
{
get { return jiange_heith; }
set { jiange_heith = value; }
}
List<Image> img = new List<Image>();
PictureBoxSizeMode mode = PictureBoxSizeMode.Zoom ;
int heicount = 1, weicount=1, all=1,nowNo=0,nowheith=0;
PictureBox[] pic;
public int Add(Image[] img)
{
for (int i = 0; i < img.Length; i++)
{
try
{
this.img.Add(img[i]);
}
catch
{
return 0;
}
}
return 1;
}
public int Clear()
{
img.Clear();
if (img.Count != 0)
{
return -1;
}
else
{
return 1;
}
}
public int Start()
{
if (State == true)
{
return -1;
}
else
{
State = true ;
imagego(); //轮播代码
return 1;
}
}
public int Stop()
{
if (State == false )
{
return -1;
}
else
{
imagestop();
State = false ;
return 1;
}
}
public int SetSize(int weith,int heith ,PictureBoxSizeMode mode)
{
Pic_weith = weith;
Pic_heith = heith;
return 1;
}
private void imagego()
{
timer1.Interval = Timer_time;
//确定一共需要初始化多少个pictureBox
Size s = this.Size;
heicount = s.Height / (Pic_heith + Jiange_heith );
weicount = s.Width / (Pic_weith+Jiange_weith );
all = heicount * weicount + 1;
pic = new PictureBox[all];
for (int i = 0; i < pic.Length; i++)
{
nowNo = i;
pic[i] = new PictureBox();
pic[i].Name ="pic_"+i.ToString();
pic[i].SizeMode = mode ;
pic[i].Size = new Size(Pic_weith, Pic_heith);
pic[i].Image = img[i];
pic[i].Location = new Point ( 0 - (i + 1) *(Pic_weith+Jiange_weith ),0);
this.Controls.Add(pic[i]);
}
timer1.Start();
}
private void imagestop()
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
for (int i = 0; i < pic.Length; i++)
{
pic[i].Location = new Point(pic[i].Location.X + 1, pic[i].Location.Y);
if (pic[i].Right > this.Right)
{
if (pic[i].Top + Pic_heith + Jiange_heith < this.Top + this.Height)
{
pic[i].Location = new Point(0-Pic_weith ,pic[i].Top + pic[i].Height + Jiange_heith );
nowheith=0;
}
else
{
pic[i].Location = new Point(0-Pic_weith ,0);
if (nowNo >= img.Count)
{
nowNo = 0;
}
pic[i].Image = img[nowNo];
nowheith++;
nowNo++;
}
}
}
}
private void UserControl1_Load(object sender, EventArgs e)
{
FileInfo[] files = new DirectoryInfo(@"C:\Users\Administrator\Pictures\明星库\debug\2NE1").GetFiles("*.jpg");
for (int i = 0; i < files.Count <FileInfo >(); i++)
{
img.Add(new Bitmap(files[i].FullName));
}
this.Start();
}
}
}