CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2000
    Location
    England
    Posts
    574

    Outputting CStrings to a screen ?

    Hi,
    I have a CString that i am outputing to a screen but the problem,
    i have is that i have to create the starting position of the text
    then i get some other Text that needs to go onto the same line but abit further along.

    I can find the length() of the first part of the string
    but given that i have the X and Y position for the first part of the string and the lengtn of that first part
    can i simply just add the length to the X starting position to move it along ?

    if not do i need to do some converting ?

    Thank again

    P

  2. #2
    Join Date
    Sep 2002
    Posts
    924
    That would only work if you setup the logical coordinates so that the size of X/Y is the size of the widest character of whatever font you are using, and the height of 1 line, etc, so X/Y coordinates are directly related to the number of characters/number of lines, etc

  3. #3
    Join Date
    May 2000
    Location
    England
    Posts
    574
    and here was i hoping that there would be a easy way ...

    Thanks russ

  4. #4
    Join Date
    Sep 2002
    Posts
    924
    Depending on what you are doing, one solution might be to only output the string when you have a full line. Set the string to the first part of the line, and when you know what the rest of the line needs to be, append that to the string and then output the whole line at once, etc.

  5. #5
    Join Date
    May 2000
    Location
    England
    Posts
    574
    Thats what i was doing bt unfortunately
    some parts of the string need to be different colours ???
    so if i create the string as one long string i cannot change certain parts of its colour ?

  6. #6
    Join Date
    Sep 2002
    Posts
    924
    I thought that was probably the case.
    Another possible solution might be to use GetTextExtent() to get the length in pixels of the string, and use that info to determine the X coordinate of the end of the string. I am not sure on the details as it has been a long time since I tried anything similair.
    You might also try searching the forum for GetTextExtent, etc to see if a similar question has allready been asked and answered.

  7. #7
    Join Date
    May 2000
    Location
    England
    Posts
    574
    Thanks Russ

  8. #8
    Join Date
    Sep 2002
    Posts
    924
    Your welcome.

    Here is a quick test I tried in OnPaint of a dialog using GetTextExtent() that seemed to work as you want.
    Code:
    CPaintDC dc(this);
    COLORREF clrNewColor = RGB(192,0,0);
    COLORREF clrOldColor;
    CSize size;
    CString strText1 = "Hello ";
    CString strText2 = "World!";
    int nOldBackMode = dc.GetBkMode();
    dc.SetBkMode(TRANSPARENT);
    dc.TextOut(0, 0, strText1);
    clrOldColor = dc.SetTextColor(clrNewColor);
    size = dc.GetTextExtent(strText1);
    dc.TextOut(size.cx, 0, strText2);
    dc.SetTextColor(clrOldColor);
    dc.SetBkMode(nOldBackMode);
    CDialog::OnPaint();
    Last edited by RussG1; March 17th, 2004 at 12:46 PM.

  9. #9
    Join Date
    May 2000
    Location
    England
    Posts
    574
    Thanks Russ
    i implemented this, this morning and it works fine ..

    Have a good one

    P

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured