• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1498
  • Last Modified:

problem with display of large text in a TStringGrid in Borland C++ Builder 5.0

hi,
I am using Borland C++ builder 5.0 and am using a 2 column TStringGrid to display some text .It works fine when there is not much text in each cell of the TStringGrid. I have a problem when text is more than 500 or 1000 characters (multiple lines of text) and it has to be displayed in one cell. The full text is not shown. I tried setting DefaultDrawing to false and writing my own DrawCell callback function. I set the DT_WORDBREAK formatting option to the Windows API DrawText(...) in the DrawCell() event handler function. But even this is not showing the full text but only the first few lines of text.How do I solve the problem?

Thanks and Warm Regards
Raghu
0
rraghu08
Asked:
rraghu08
  • 3
  • 2
1 Solution
 
itsmeandnobodyelseCommented:
>>>> more than 500 or 1000 characters (multiple lines of text) and it has to be displayed in one cell.
Does the text fit physically into the cell? Or would you need scrolling?

If the first I would assume the problem already is when you insert the text to the cell. Can you post the code sequence where this was done? And what were the buffers and string variables where the text comes from and was set to the grid cell?

When you were writing your own drawing callback, how did you get the text to draw? Is it already shortened there or only when displaying on the screen? If the latter, you might check the drawing rectangle? If it for example could only take 4 lines of text, it necessarily would cut the rest. If the text already was spoiled when the callback was called you might think about providing the full text at drawing time from own resources rather than storing it in the grid. Most grids have the option to only store some kind of handle (pointer) to a cell and only when the cell is going to be visualized the text was requested by means of a callback. Or if owner-drawn you anyhow can get the text to be drawn from other sources.
0
 
rraghu08Author Commented:
It has scrolling enabled. The test is fully there in the DrawCell  event handler function . It gets the right rectangle required for the text . I am using DrawText() Windows API to diplay the test with the given rectangle. Everyhtin is right but it still fails to display the full text.I have attached the code from the DrawCell() callback function.  

In the function where I am assigning the text to a particular cell in the TStringGrid, I am modifying the cell height as shown in the below code before I assign text to that cell.

   TRect Rect;
    Rect.Left = 636;
    Rect.Top  = 416;
    Rect.Right = Rect.Left + 235;

    DrawText(FORM->resCmntStringGrid->Canvas->Handle,
            strtmp, strlen(strtmp),
            &Rect, DT_WORDBREAK |  DT_CALCRECT|  DT_NOCLIP | DT_LEFT);
    FORM->resCmntStringGrid->RowHeights[Num] = Rect.Height();
    FORM->resCmntStringGrid->Cells[1][Num] = cmntStr;

Thanks and Warm Regards
Raghu

void __fastcall TscanDataForm::resCmntStringGridDrawCell(TObject *Sender,
      int ACol, int ARow, TRect &Rect, TGridDrawState State)
{
    if (ARow == 0)
         FORM->resCmntStringGrid->Canvas->Brush->Color = CYAN_BG;
    else
        FORM->resCmntStringGrid->Canvas->Brush->Color = 
       resCmntStringGrid->Color;
 
    FORM->resCmntStringGrid->Canvas->FillRect(Rect);
 
    RECT R = RECT(Rect);
    UINT format ;
 
    AnsiString text = resCmntStringGrid->Cells[ACol][ARow];
 
    format =      DT_WORDBREAK | DT_LEFT | DT_CALCRECT |  DT_NOCLIP;
 
    char* txtbuf = new char[ text.Length() + 1 ];
    sprintf(txtbuf, "%s", text.c_str());
    int len = strlen(txtbuf);
    DrawText(resCmntStringGrid->Canvas->Handle,
             txtbuf, len,
             &Rect, format);
    format =      DT_WORDBREAK | DT_LEFT |  DT_NOCLIP;
 
    DrawText(resCmntStringGrid->Canvas->Handle,
             txtbuf, len,
             &Rect, format);
 
 }

Open in new window

0
 
itsmeandnobodyelseCommented:
First, instead of copying the text to a buffer which never was freed after that, thus producing memory leaks, you should do

   AnsiString& text = resCmntStringGrid->Cells[ACol][ARow];  // note the &
   DrawText(resCmntStringGrid->Canvas->Handle, text.c_str(), text.Length(),
             &Rect, format);

Second, if drawing the text yourself, you also must handle the scrolling case. That means you need to find out which lines of the text should be outputted and parse the text for "\r\n" CRLF line wraps. You can forget the automatic line and word wrap if owner drawing. Instead, you need to make the wrap on your own, e. g. by putting all text to a hidden multi-line edit control of same size as the (visual) cell. Then you can retrieve line by line from that edit control. Unfortunatly I have no idea  how to get the scroll information from the cell. You would need to check the structure passed as argument to the drawing callback for any information regarding the scroll  status of a multi-line text cell.

Regards, Alex
0
 
rraghu08Author Commented:
hi
I replaced the idea of directly displaying text in each cell of the TStringGrid  by using a dynamically created TMemo for each cell which would display the full text when the user clicks on the cell.

also are yousing DT_WORDBREAK doesn't work when used as formatting argument in DrawText() win API.  Is it not supposed to work since its a direct Windows API and you are directly calling it.

thanks and regards
Raghu

0
 
rraghu08Author Commented:
hi ,
I am not sure if  DT_WORDBREAK doesn't work when used as formatting argument in DrawText() Win32 API.  Is it not supposed to work since its a direct Windows API and you are directly calling it. Do let me know if you can confirm it either way.  thanks and regards Raghu
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now