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

con*_*ist 1 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();
     }
 }
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:

输出

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

Bar*_*ani 5

正如 @JonnyMoop 所指出的,DT_MODIFYSTRING不能与DT_END_ELLIPSIS常量字符串一起使用。在这种情况下你可以直接删除DT_MODIFYSTRING

DT_WORDBREAK并且DT_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);
}
Run Code Online (Sandbox Code Playgroud)