在窗体上的PictureBox的感测 [英] Sensing of PictureBoxes on a Form

查看:69
本文介绍了在窗体上的PictureBox的感测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当时正在考虑在我的应用程序中包含一项功能,其中当从菜单中选择一种模式时,我们必须在一定时间后转到窗体上的每个PictureBox.例如:有5个图片框(由于图片框是在运行时创建的,因此数量不固定).当用户选择模式时,鼠标会定期移至每个PictureBox的中心.如屏幕截图所示,具有焦点的图片框应具有暗红色的边框( http: //img508.imageshack.us/img508/1279/focus.png [ ^ ]).其余功能与应用程序相同.

I was thinking to include a feature in my application in which when Selects a mode from a menu, then we have to go each PictureBox on the form after a certain time. For eg: There are 5 pictureboxes (number not fixed since pictureboxes are created at runtime). When the user selects the mode, the Mouse goes to the center of each pictureBox periodically. The picture box that has focus should have a frame around it with a dark red color as shown in the screenshot clearly ( http://img508.imageshack.us/img508/1279/focus.png[^] ). The rest of the functioning is the same as for the application.

推荐答案

您需要做一些事情.

首先,使用Form.Controls属性为您提供表单上的控件列表.然后,可以在控件上使用GetType方法来确定控件是否为图片框.

如果是,则获取位置和大小,然后找到中心,并可能将其放入包含Point对象集合的列表中.

然后,您可以获得鼠标要周期性移动的位置的列表(可以通过计时器来实现).有了它之后,当鼠标移至索引1时,将图片框的边框设置为红色正方形.

您完成了:)

我将针对主要概念编写一些快速代码,以帮助您入门:


There are a few things you need to do.

First, use the Form.Controls property gives you the list of controls on the form. You can then use the GetType method on the control to determine if the control is a picture box.

If it is, you get the location and the size and find the centre and possibly put it into a list holding a collection of Point objects.

You then have a list of locations for your mouse to go to periondically (which can be implemented by a timer). Once you have this, when your mouse goes to index 1, you set the border of the picture box to a red square.

Your done :)

I will write some quick code on the main concepts to get you started:


// list that holds the centers of all picture boxes on the form
            List<Point> picture_centers = new List<Point>();

            foreach(Control c in this.Controls)
            {
              // is this control a picture box?
              if(c.GetType() == typeof(PictureBox))
              {// yes it is!
                  // work out centre here and assign it
                  Point center = new Point();

                  // add the point to the list of pic centers
                  picture_centers.Add(center);
              }
            }

            // now you simply make a timer and in the event handler you
            // go through the list created and say...
            foreach(Point p in picture_centers)
            {
                // set cursor to location of cetner of pic
                Cursor.Position = p;

                // get the control under the cursor (it should be a picture box by design)
                // so you can put an assert here
                Control c = this.GetChildAtPoint(p);
                PictureBox pic = (PictureBox) c;
                pic.BorderStyle = ;
                // etc for more formatting options and maybe some sleep time
            }


这篇关于在窗体上的PictureBox的感测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆