7

I can't find any function that can help me with this and I don't want to write crazy function that will HitTest every pixel of ListView area, to find out coords of needed Column (if it ever possible to get Column from HitTest).

enter image description here

Thanks to Yair Nevet comment, I wrote next function to determine Left position of needed Column:

private int GetLeftOfColumn(ColumnHeader column, ListView lv)
{
    if (!lv.Columns.Contains(column))
        return -1;

    int calculated_left = 0;

    for (int i = 0; i < lv.Columns.Count; i++)
        if (lv.Columns[i] == column)
            return calculated_left;
        else
            calculated_left += lv.Columns[i].Width + 1;

    return calculated_left;
}
4

2 に答える 2

2

PointToScreenおよびPointToClientクラスを使用してそれを実現できます。以下をご覧ください。

Point locationOnForm = listView1.FindForm()
              .PointToClient(listView1.Parent.PointToScreen(listView1.Location));

次に、受け取った Point の X 座標と Y 座標、および列の幅と高さを使用して、フォーム上の位置を計算します。

private int GetLeftOfColumn(ColumnHeader column, ListView lv)
{
    if (!lv.Columns.Contains(column))
        return -1;

    int calculated_left = 0;

    for (int i = 0; i < lv.Columns.Count; i++)
        if (lv.Columns[i] == column)
            return calculated_left;
        else
            calculated_left += lv.Columns[i].Width + 1;

    return calculated_left;
}
于 2013-07-14T18:28:37.570 に答える