0

我在 Windows 窗体面板中有一个标签数组(最多 50 个标签)。当垂直滚动数组的“可见”索引时 - 更改。如何仅获取当前屏幕标签的索引?

4

1 回答 1

0

这是一种将 Panel 的 ClientRectangle 和每个 Label 转换为屏幕坐标然后检查交叉点的方法:

滚动面板中的可见标签

public partial class Form1 : Form
{

    private Label[] Labels;

    public Form1()
    {
        InitializeComponent();
        Labels = new Label[] { label1, label2, label3, label4, label5 };
    }

    private void button1_Click(object sender, EventArgs e)
    {
        List<string> names = new List<string>(); // for demonstration purposes

        // determine which Labels are currently visible in the scrolled panel:
        Rectangle rectPanel = panel1.RectangleToScreen(panel1.ClientRectangle);
        for(int i = 0; i < Labels.Length; i++)
        {
            Rectangle rectLabel = Labels[i].RectangleToScreen(Labels[i].ClientRectangle);
            if (rectLabel.IntersectsWith(rectPanel))
            {
                // ... do something with "i" ...
                names.Add(Labels[i].Name); // for demonstration purposes
            }
        }

        listBox1.DataSource = null; // for demonstration purposes
        listBox1.DataSource = names; // for demonstration purposes
    }

}
于 2013-06-10T15:38:20.067 回答