0

我是应用程序开发的新手。我创建了一个应用程序,该应用程序预计将在 RichEditControl2 中以表格格式显示以下数据,但我面临字符间距问题。

日期 MilsStone Sub 2012-03-12 需求/票证分析 需求理解 2.0 2012-03-14 设计开发/文档设计 3.0 2012-03-15 设计设计审查 3.0 2012-03-15 编码和单元测试开发 4.0

在这种情况下,我无法设置宽度(使用 AddDataToDisplayBox 中的 Format())。请帮忙。

void Csdlc_verifierDlg::AddDataToDisplayBox(int index,COLORREF color, bool bold, bool italic, bool underline)


{
    CString strTemp;
    char buf[255]={0};
    record_data record = mRecData.GetAt(index); 
    strTemp.Format("%-15s%-50s%-50s%-5s%-15s",record.date,record.milestone,record.tasktype,record.effort,record.name);  
    AddLine(strTemp,NEWLINE,color,bold,italic,underline);
}

int Csdlc_verifierDlg::AddLine(CString str, int seperator, COLORREF color, bool bold, bool italic, bool underline)
{           
    int txtLen = mRichEditCtrl.GetTextLength();

    // Save number of lines before insertion of new text
    int nOldLines = mRichEditCtrl.GetLineCount();

    // Initialize character format structure
    CHARFORMAT cf = {0};
    cf.cbSize = sizeof(CHARFORMAT);
    cf.dwMask = CFM_COLOR|CFM_BOLD|CFM_ITALIC|CFM_UNDERLINE|CFM_CHARSET|CFM_SPACING; //Mask validates the active field in this case.
    cf.dwEffects = (bold ? CFE_BOLD : 0) | (italic ? CFE_ITALIC : 0) | (underline ? CFE_UNDERLINE : 0);
    cf.crTextColor = color;

    //Add newline character, if required.
    switch(seperator)
    {
    case NEWLINE:
        str.AppendChar('\n');
        break;

    case SPACE:
        str.AppendChar(' ');
        break;
    }
    //Insert data at the end.
    mRichEditCtrl.SetSel(txtLen, -1); // Set the cursor to the end of the text area and deselect everything.
    mRichEditCtrl.ReplaceSel(str); // Inserts when nothing is selected.

    // Apply formating to the just inserted text.
    mRichEditCtrl.SetSel(txtLen-(nOldLines-1), mRichEditCtrl.GetTextLength());
    mRichEditCtrl.SetSelectionCharFormat(cf);

    // Scroll by the number of lines just inserted
    mRichEditCtrl.LineScroll(mRichEditCtrl.GetLineCount() - nOldLines); 
    return 0;
}
4

2 回答 2

0

要在 Rich Edit 控件中创建对齐的列,您需要创建一个固定间距字体,例如“Courier”,并使用 mRichEditCtrl.SetFont(...); 为控件设置该字体;

可以通过在列之间使用制表符'\t'来创建一些具有比例空间字体的列对齐效果,但这仅在每行文本具有大致相同的列文本宽度时才有效。如果一行使用完整的 50 个字符,而另一行只有几个字符,则单个 '\t' 将不足以对齐。对于这些情况,您需要进行额外的处理,根据列中的字符数计算要插入的制表符数。

于 2013-02-16T18:42:59.387 回答
0

如果您来自同一所学校,或者只是在不同的论坛上发布了相同的问题,请在此处查看答案

于 2013-02-18T01:00:20.010 回答