C#分屏控件用法实例
程序员文章站
2023-12-21 14:23:40
本文实例中的自定义类picturebox继承于usercontrol,最终实现简单的分屏功能。分享给大家供大家参考之用。具体实现代码如下:
public part...
本文实例中的自定义类picturebox继承于usercontrol,最终实现简单的分屏功能。分享给大家供大家参考之用。具体实现代码如下:
public partial class picturecontrol : usercontrol { public event mouseeventhandler piccontrol_doubleclick; private int picnum; /// <summary> /// 屏幕个数 /// </summary> public int picnum { get { return picnum; } set { if (value == 4 || value == 6 || value == 9 || value == 12 || value == 16 || value == 20 || value == 25)//只能 是4、6、9、12、16、20、25 { picnum = value; this.setpicturebox(this.picnum); } else { this.picnum = 12; this.setpicturebox(this.picnum); } } } public picturecontrol() { this.picnum = 4; initializecomponent(); this.setpicturebox(this.picnum); } /// <summary> /// 根据个数布局picturebox /// </summary> /// <param name="num"></param> private void setpicturebox(int num) { this.controls.clear(); size size = this.size; switch (num) { case 4: this.setpicturebox(2, 2, size); break; case 6: this.setpicturebox(2, 3, size); break; case 9: this.setpicturebox(3, 3, size); break; case 12: this.setpicturebox(3, 4, size); break; case 16: this.setpicturebox(4, 4, size); break; case 20: this.setpicturebox(4, 5, size); break; case 25: this.setpicturebox(5, 5, size); break; } } /// <summary> /// 布局picturebox /// </summary> /// <param name="x">几行</param> /// <param name="y">几列</param> /// <param name="size">当前控件的大小</param> private void setpicturebox(int x, int y,size size) { int num = 0; for (int i = 0; i < x; i++) { for (int j = 0; j < y; j++) { picturebox pic = new picturebox(); pic.sizemode = pictureboxsizemode.zoom; //设置自动缩放 pic.backcolor = color.white; //设置背景颜色 pic.location = new point((size.width / y) * j, (size.height / x) * i); //设置location pic.borderstyle = borderstyle.fixedsingle; //设置边框 pic.mousedoubleclick += new mouseeventhandler(pic_mousedoubleclick); //订阅控件双击事件 pic.size = new size(size.width / y, size.height / x); //设置控件大小 pic.tag = num; //设定控件编号即屏幕序号 this.controls.add(pic); //添加 num++; } } } void pic_mousedoubleclick(object sender, mouseeventargs e) { if (this.piccontrol_doubleclick != null) { this.piccontrol_doubleclick(sender, e);//将动态添加的控件的双击事件 传向控件体外。 } } private void picturecontrol_sizechanged(object sender, eventargs e) { this.setpicturebox(this.picnum); } private picturebox getpicbyindex(int index) { foreach (control c in this.controls) { if (convert.toint32(c.tag) == index) { return (picturebox)c; } } picturebox p = new picturebox(); p.tag = -1; return p; } /// <summary> /// 根据屏幕序号设置图像 /// </summary> /// <param name="index">屏幕号</param> /// <param name="img">图像</param> public void setimagebyindex(int index, image img) { getpicbyindex(index).image = img; } }
希望本文所述实例对大家c#程序设计有所帮助。