//例如:你此次股票买卖盈利10次。

那么CStringW str[3] = {L"你此次股票买卖盈利",:L"10",L"次"};

Color clr[3] = {黑色,绿色,黑色};


DrawColorText(g,str,2,L"微软雅黑",FontStyleBold,18,rtF,StringAlignmentCenter,StringAlignmentCenter,clr);

/*

wStr :需要被输出的数组

 nSize:数组大小

wfontName:字体Name

style:字体风格

rtf:输出的位置

lAlign:水平对齐方式

vlAlign :竖直对齐方式

clrText:字体颜色Color数组,维数大于等于nSize

*/

void DrawColorText(Graphics &g,CStringW* wStr,int nSize,WCHAR *wfontName,FontStyle style,int nFontsize,RectF &rtf,StringAlignment lAlign,StringAlignment vlAlign,Color *clrText)
{
Gdiplus::Font capft(wfontName,nFontsize,style,UnitPixel);
StringFormat strformat;
strformat.SetAlignment(StringAlignmentNear);
strformat.SetLineAlignment(vlAlign);


CStringW WstrCap;;
for (int i = 0;i < nSize;++i)
{
WstrCap += wStr[i];
}
//计算起始点
PointF ptFStart(rtf.GetLeft(),rtf.GetTop());
RectF rtBondBox(0,0,0,0);
if(lAlign == StringAlignment::StringAlignmentCenter)
{
g.MeasureString(WstrCap,WstrCap.GetLength(),&capft,rtf,&rtBondBox);
if(rtBondBox.Width < rtf.Width)
{
ptFStart.X += (rtf.Width - rtBondBox.Width)/2 - 1;
}
}
//计算每个字符串的宽度
int *pWidth = new int[nSize];
for(int i = 0;i < nSize;++i)
{
g.MeasureString(wStr[i],wStr[i].GetLength(),&capft,rtf,&rtBondBox);
pWidth[i] = rtBondBox.Width;
}


RectF rtItem(ptFStart.X,rtf.GetTop(),rtf.GetRight(),rtf.GetBottom());
for(int i = 0;i < nSize;++i)
{
g.DrawString(wStr[i],wStr[i].GetLength(),&capft,rtItem,&strformat,&SolidBrush(clrText[i]));
rtItem.X += pWidth[i];
}
delete pWidth;
}