use GDI+ to change text color [modified]
-
Hi All, I use this code to change the color of some words in the text, using GDI+ (it based on the example from MSDN). I want to change the color of words in range specified by the charRanges. But the result is kind of weird, the words are not displayed as specified by the range, and the position is moving to right-bottom a little bit. --edit-- I think I know the problem. The text displayed in the new region is trimmed and positioned based on the region's size. So, I wonder if there any other way to do this? Below is some of the code... If you have any experience with GDI+ pls help me... Thank you!
... // Three ranges of character positions within the string int p[3] = {4,17,39}; int q[3] = {5,3,13}; CharacterRange charRanges[3] = { CharacterRange(4, 5), CharacterRange(17, 3), CharacterRange(39, 13), }; // Font and string format used to apply to string when drawing Font myFont(L"Times New Roman", 12); StringFormat strFormat; StringFormat stringFormat; // Other variables Region* pCharRangeRegions; // pointer to CharacterRange regions short i; // loop counter int count; // number of character ranges set CString strMsg = "The quick, brown fox easily jumps over the lazy dog."; LPWSTR string; string = CStringToWchar(strMsg); // Set three ranges of character positions. strFormat.SetMeasurableCharacterRanges(3, charRanges); // Get the number of ranges that have been set, and allocate memory to // store the regions that correspond to the ranges. count = strFormat.GetMeasurableCharacterRangeCount(); pCharRangeRegions = new Region[count]; // Get the regions that correspond to the ranges within the string. // Then draw the string and show the regions. graphics.MeasureCharacterRanges(string, -1, &myFont, layoutRect, &strFormat, count, pCharRangeRegions); RectF *enclosingRect; enclosingRect = new RectF[count]; graphics.DrawString(string, -1,&myFont, layoutRect, &strFormat, &blueBrush); for ( i = 0; i < count; i++) { string = CStringToWchar(strMsg.Mid(p[i],q[i])); graphics.SetClip(&pCharRangeRegions[i]); graphics.GetClipBounds(&enclosingRect[i]); graphics.DrawString(string, -1, &myFont, enclosingRect[i], &stringFormat, &redBrush); }
-- modified at 4:28 Thursday 26th July, 2007-Houari