如何在对话框中获取列表控件的多行工具提示?

时间:2015-04-06 09:20:05

标签: c++ mfc tooltip listcontrol

由于我的列表框上的文字非常庞大,我试图在列表控件上获取多行工具提示。

BOOL CTestDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    mylist.EnableToolTips(TRUE);
    mylist.SetExtendedStyle(LVS_EX_INFOTIP | mylist.GetExtendedStyle());

    mylist.InsertColumn(0, L"suri", LVCFMT_LEFT, 10000);
    CString str1 = L"nonNegativeInteger GetVehicleOwnerHolderByRegNumAndDateResponse.GetVehicleOwnerHolderByRegNumAndDateResult[optional].GetVehicleOwnerHolderByRegNumAndDateResultType.VHOwnerHolderResponse.VHOwnerHolderResponseType.Body.VehicleCountries.VehicleCountriesType.VehicleCountry[1, unbound].VehicleCountryType.VehCountryReplies.VehCountryRepliesType.VehCountryReply[1, unbound].Messages[optional].Message[1, unbound].MessageType.MessageCode"; 
    for (int i = 0; i < 20 ; i++) {
        CString str2;
        str2.Format(L"%d",i);
        str2 = str2 + str1;
        mylist.InsertItem(LVIF_TEXT | LVIF_PARAM, i, str2, 0, 0, 0, NULL);
    }

    return TRUE;  // return TRUE  unless you set the focus to a control
}

我得到以下输出,这是截断的文本,即缺少完整的文本。   enter image description here

如何在工具提示多行上获取文字?

编辑:我也使用了以下。 还是一样的结果。

CToolTipCtrl* pToolTip = AfxGetModuleThreadState()->m_pToolTip;
   if (pToolTip)
      pToolTip->SetMaxTipWidth(SHRT_MAX);

2 个答案:

答案 0 :(得分:2)

您可以使用newlines-charecters获取多行工具提示,其中SetMaxTipWidth()设置为较大的值。如果使用较小的值调用SetMaxTipWidth(),则在遇到空格字符时会自动分成多行。

您需要继承工具提示/信息提示的子类才能使用它:

BEGIN_MESSAGE_MAP(CListCtrl_InfoTip, CListCtrl)
    ON_NOTIFY_REFLECT_EX(LVN_GETINFOTIP, OnGetInfoTip)
    ON_NOTIFY_EX(TTN_NEEDTEXTA, 0, OnToolNeedText)
    ON_NOTIFY_EX(TTN_NEEDTEXTW, 0, OnToolNeedText)
END_MESSAGE_MAP()

void CListCtrl_InfoTip::PreSubclassWindow()
{
    CListCtrl::PreSubclassWindow();
    SetExtendedStyle(LVS_EX_INFOTIP | GetExtendedStyle());
}

BOOL CListCtrl_InfoTip::OnGetInfoTip(NMHDR* pNMHDR, LRESULT* pResult)
{
    // Will only request tooltip for the label-column
    NMLVGETINFOTIP* pInfoTip = (NMLVGETINFOTIP*)pNMHDR;
    CString tooltip = GetToolTipText(pInfoTip->iItem, pInfoTip->iSubItem);
    if (!tooltip.IsEmpty())
    {
        _tcsncpy(pInfoTip->pszText, static_cast<LPCTSTR>(tooltip), pInfoTip->cchTextMax);
    }
    return FALSE;    // Let parent-dialog get chance
}

BOOL CListCtrl_InfoTip::OnToolNeedText(UINT id, NMHDR* pNMHDR, LRESULT* pResult)
{
...
   // Break tooltip into multiple lines if it contains newlines (\r\n)
   CToolTipCtrl* pToolTip = AfxGetModuleThreadState()->m_pToolTip;
   if (pToolTip)
      pToolTip->SetMaxTipWidth(SHRT_MAX);
...
}

答案 1 :(得分:0)

要考虑两个方面:

1。窗口大小

激活多行模式。这条指令就足够了:

 pToolTip->SetMaxTipWidth(SHRT_MAX);

2。要显示的字符数

对于第二点,有必要提防,因为 pszText 字段的大小限制为 80个字符

typedef struct tagNMTTDISPINFOA {
NMHDR hdr;
LPSTR lpszText;
char szText[80];
...
}

因此,即使您更改 SetMaxTipWidth ,您也不会看到任何区别。 建议您使用无限制的 lpszText 字段。下面是您感兴趣的代码片段:

pTTTW->lpszText = T2W (strTipText.GetBuffer (strTipText.GetLength ()));

strTipText 是包含要显示在弹出窗口中的消息的CString

相关问题