难道要做100个窗体出来?

我用C#做了一个Winform的软件,这个软件是这样的:假如有两张图,每一张图上分别划分了很多不同的区域(两上图上的区域是不一样的),我点击不同的区域,就给出不同的提示信息(我用了tooltip控件实现),这些信息是通过都是通过数据库读取的。我实现的方法是:在每一张图上的不同区域放上一个Button,然后把这个button透明化,不让它遮挡住后面的图片内容,当单击的时候响应这个事件就可以了。但是,由于是两张图,那就需要有两上窗体,一个Form1和一个Form2,每个窗体上放上N多的Button,而且这个button的事件基本上一模一样,虽然现在做成功了,但是我觉得这个软件的扩展性非常的差。我现在的问题就是:如果现在需要在原基础上加两张图,难道又要重做?做出四个窗体出来?既然这些Button的事件是一样的,能不能简化一下?

怎么才能实现,即使有100张图,每张图的区域都不同,但是我只需要一个窗体就能实现?就是只在一个窗体上显示一张图,当这张图我看完了,点击“下一个”就可以自动进入下一张图呢?(由于每一张图的区域是不同的,所以不能确定每一张图上button的位置,那该如何实现呢?)


元芳怎么了
浏览 382回答 1
1回答

胡子哥哥

只需要一个窗体,如自定义一个PictureForm,每次都初始化这样一个PictureForm,包含一个Button列表,每显示一个新的图片时设置每个Button的Location:&nbsp; &nbsp;class PictureForm : Form &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp;public List<Button> Buttons { get; set; } &nbsp; &nbsp; &nbsp; &nbsp;//... &nbsp; &nbsp;} &nbsp; &nbsp;class MainForm : Form &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp;private PictureForm pictureForm; &nbsp; &nbsp; &nbsp; &nbsp;private void btnNext_Click(object sender, EventArgs e) &nbsp; &nbsp; &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (pictureForm == null) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pictureForm = new PictureForm(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Button b = new Button(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b.Location = ...; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pictureForm.Buttons.Add(b); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// add other buttons &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pictureForm.ShowDialog(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// add buttons &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pictureForm.ShowDialog(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp;}&nbsp;如果要同时显示多个图片,也很容易,只要设置多个PictureForm即可
打开App,查看更多内容
随时随地看视频慕课网APP