다음을 통해 공유


CHeaderCtrl::HitTest

Determines which header item, if any, is located at a specified point.

int HitTest(
        LPHDHITTESTINFO* phdhti
);

Parameters

Parameter

Description

[in, out] phdhti

Pointer to a HDHITTESTINFO structure that specifies the point to test and receives the results of the test.

Return Value

The zero-based index of the header item, if any, at the specified position; otherwise, –1.

Remarks

This method sends the HDM_HITTEST message, which is described in the Windows SDK.

Requirements

Header: afxcmn.h

This method is supported in Windows NT 3.51 and later.

Example

The following code example defines the variable, m_headerCtrl, that is used to access the current header control. This variable is used in the next example.

   CHeaderCtrl m_headerCtrl;
    CSplitButton m_splitButton;

The following code example demonstrates the HitTest method. In an earlier section of this code example, we created a header control with five columns. However, you can drag a column separator so that the column is not visible. This example reports the index of the column if it is visible and -1 if the column is not visible.

void CNVC_MFC_CHeaderCtrl_s4Dlg::OnXHittest()
{
    if (controlCreated == FALSE) {
        MessageBox(_T("Header control has not been created yet."));
        return;
    }
    // Initialize HDHITTESTINFO structure.
    HDHITTESTINFO hdHitIfo;
    memset(&hdHitIfo, 0, sizeof(HDHITTESTINFO));

    CString str;
    CRect rect;
    int iRetVal = -1;
    for(int i = 0; i < m_headerCtrl.GetItemCount(); i++)
    {
        m_headerCtrl.GetItemRect(i, &rect);
        hdHitIfo.pt = rect.CenterPoint();
        // The hit test depends on whether the header item is visible.
        iRetVal = m_headerCtrl.HitTest(&hdHitIfo);
        str.AppendFormat(_T("Item = %d, Hit item = %d\n"), i, iRetVal);
    }
    MessageBox(str, _T("Hit test results"));
}

See Also

Concepts

CHeaderCtrl Class

CHeaderCtrl Members

Hierarchy Chart

HDM_HITTEST

HDHITTESTINFO