Reputation: 6613
I am trying to draw two pieces of text one by one in MFC as they would be part of the same text. Right now I am drawing them as they are just one string:
CString text1 = "A text";
CString text2 = "A second text";
CString textToDraw = text1 + text2;
CDC* dc = GetDC(); //assume that this is initialized elsewhere
dc->TextOut(0, 0, textToDraw);
It is simple to draw the both texts as one because I only need to find the position where they should be started to be draw. The problem I am facing is how to compute the new X coordinate at which the second text should be draw (considering that the texts can be chosen at run-time so they do not have a known length):
dc->TextOut(0, 0, text1);
int X;
//how should I compute X...?
dc->TextOut(X, 0, text2);
I appreciate any help received!
Upvotes: 1
Views: 190
Reputation: 309
You can use DrawText() with DT_CALCRECT flag to calculate the width and height the text would occupy without actually drawing the text. The following may be the answer to your question.
dc->TextOut(100, 100, text1);
RECT rect = { 0, 0, 0, 0 };
dc->DrawText(text1, &rect, DT_CALCRECT);
dc->TextOut(100 + rect.right, 100, text2);
Upvotes: 4