DrawText不会将省略号添加到MFC中文本的末尾

时间:2015-11-10 18:12:48

标签: c++ mfc

我有一个简单的MFC应用程序,其中我试图将省略号添加到字符串的末尾。

这是我的代码:

void CMFCApplication1Dlg::OnPaint()
{
     if (IsIconic())
     {
         CPaintDC dc(this); // device context for painting

         SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

         // Center icon in client rectangle
         int cxIcon = GetSystemMetrics(SM_CXICON);
         int cyIcon = GetSystemMetrics(SM_CYICON);
         CRect rect;
         GetClientRect(&rect);
         int x = (rect.Width() - cxIcon + 1) / 2;
         int y = (rect.Height() - cyIcon + 1) / 2;

         // Draw the icon
         dc.DrawIcon(x, y, m_hIcon);
     }
     else
     {
         CPaintDC dc(this); // device context for painting

         CRect rect;
         rect.top = 0;
         rect.bottom = 100;
         rect.left = 0;
         rect.right = 100;
         dc.DrawText(_T("This is a very very long text that should span accross multiple lines and have elipsis at the end"), -1, &rect, DT_WORDBREAK | DT_MODIFYSTRING | DT_END_ELLIPSIS);

         CDialogEx::OnPaint();
     }
 }

这就是我得到的:

output

我期待将省略号添加到字符串的末尾。我做错了什么?

1 个答案:

答案 0 :(得分:4)

如@JonnyMoop DT_MODIFYSTRING所述,DT_END_ELLIPSIS不能与常量字符串一起使用。在这种情况下,您只需删除DT_MODIFYSTRING

DT_WORDBREAKDT_END_ELLIPSIS无法很好地协同工作。尝试添加DT_EDITCONTROL

此外,CDialogEx::OnPaint();是对CPaintDC dc(this);的调用,因此请勿同时使用它们。

void CMFCApplication1Dlg::OnPaint()
{
    CPaintDC dc(this);

    CRect rect;
    rect.top = 0;
    rect.bottom = 100;
    rect.left = 0;
    rect.right = 100;
    CString s = L"This is a very very long text that should span accross multiple lines and have elipsis at the end";
    dc.DrawText(s, &rect, DT_EDITCONTROL | DT_WORDBREAK | DT_END_ELLIPSIS);
}