<PRE lang=cs id=pre2 style="MARGIN-TOP: 0px">private bool HitTest(Point hitPoint, out int row, out int column)
{
const int LVM_GETSUBITEMRECT = 0x1038; //Is LVM_FIRST (0x1000) + 56
const int LVM_COLUMNORDERARRAY = 0x103B; //Is LVM_FIRST (0x1000) + 59
const int LVIR_BOUNDS = 0;
bool retval = false;
RECT subItemRect;
row = column = -1;
ListViewItem item = m_lvListView.GetItemAt(hitPoint.X, hitPoint.Y);
if(item != null && m_lvListView.Columns.Count > 1)
{
if(m_lvListView.AllowColumnReorder)
{
int[] columnOrder = new int[m_lvListView.Columns.Count];
// Get the order of columns in case
// they've changed from the user.
if(SendMessage(m_lvListView.Handle,
LVM_COLUMNORDERARRAY, m_lvListView.Columns.Count,
columnOrder) != 0)
{
int i;
// Get the subitem rectangles (except column 0),
// but get them in the proper order.
RECT[] subItemRects = new RECT[m_lvListView.Columns.Count];
for(i = 1; i < m_lvListView.Columns.Count; i++)
{
subItemRects[columnOrder[i]].top = i;
subItemRects[columnOrder[i]].left = LVIR_BOUNDS;
SendMessage(m_lvListView.Handle,
LVM_GETSUBITEMRECT, item.Index,
ref subItemRects[columnOrder[i]]);
}
// Find where column 0 is.
for(i = 0; i < columnOrder.Length; i++)
if(columnOrder[i] == 0)
break;
// Fix column 0 since we can't get
// the rectangle bounds of it using above.
if(i > 0)
{
// If column 0 not at index 0, set using the previous.
subItemRects[i].left = subItemRects[i-1].right;
subItemRects[i].right = subItemRects[i].left
+ m_lvListView.Columns[0].Width;
}
else
{
// Else, column 0 is at index 0, so use the next.
subItemRects[0].left = subItemRects[1].left -
m_lvListView.Columns[0].Width;
subItemRects[0].right = subItemRects[1].left;
}
// Go through the subitem rectangle bounds and
// see where our point is.
for(int index = 0; index < subItemRects.Length; index++)
{
if(hitPoint.X >= subItemRects[index].left &
hitPoint.X <= subItemRects[index].right)
{
row = item.Index;
column = columnOrder[index];
retval = true;
break;
}
}
}
}
// No column reordering...much simpler.
else
{
for(int index = 1; index <= m_lvListView.Columns.Count-1;
index++)
{
subItemRect = new RECT();
subItemRect.top = index;
subItemRect.left = LVIR_BOUNDS;
if(SendMessage(m_lvListView.Handle,
LVM_GETSUBITEMRECT, item.Index, ref subItemRect) != 0)
{
if(hitPoint.X < subItemRect.left)
{
row = item.Index;
column = 0;
retval = true;
break;
}
if(hitPoint.X >= subItemRect.left & hitPoint.X <=
subItemRect.right)
{
row = item.Index;
column = index;
retval = true;
break;
}
}
}
}
}
return retval;
}</PRE>