how to print data in tabular format in rich edit c

2019-09-09 13:33发布

I am a novice in application development. I have created a app which is expected to display following data in RichEditControl2 in tabular format, but i am facing issue with character spacing.

Date MilsStone Sub 2012-03-12 Requirement/Ticket-Analysis Requirements understanding 2.0 2012-03-14 Design Develop/ Document Design 3.0 2012-03-15 Design Design Review 3.0 2012-03-15 Coding&Unit Testing Develop 4.0

I am unable to set width in this case (using Format() in AddDataToDisplayBox). Please help.

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;
}

2条回答
甜甜的少女心
2楼-- · 2019-09-09 13:58

To created aligned columns in a Rich Edit control, you will need to create a fixed-space font such as "Courier" and set that font for the control using mRichEditCtrl.SetFont(...);

It is possible to create some column alignment effects with a proportional space font by using tabs '\t' between the columns, but that only works if each row of text has approximately the same width of text for the column. If one row uses the full 50 characters, and another row has only a few characters, a single '\t' won't be enough for alignment. For those cases, you need to additional processing that calculates the number of tabs to insert based on the number of characters in the column.

查看更多
3楼-- · 2019-09-09 14:12

if you are from the same school, or just posted the same question on different forums, see the answer here

查看更多
登录 后发表回答