c# 照片轮播控件

新建一个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();
        }
    }
}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值