Wednesday, March 30, 2011

Drawing text in Right to Left direction


Hi friends,
Recently I came across a situation where I had to display string in right to left flowdirection.
And at this point I realize that it was coming as mirror image Smile
Here is the method to solve this problem:

private void TransFormAndDrawText(FormattedText fTxt ,double dStartPoint, double dEndPoint, DrawingContext g)
{
    MatrixTransform antiMirror = null;
    if (FlowDirection == FlowDirection.RightToLeft)
    {
        double offsetX = fTxt.Width;
        double offsetY = 0;
        antiMirror = new MatrixTransform(-1, 0, 0, 1, offsetX, offsetY);
        dStartPoint *= -1;
    }
    if (antiMirror != null)
    {
        g.PushTransform(antiMirror);
    }

    g.DrawText(fTxt, new Point(dStartPoint, dEndPoint));
    if (antiMirror != null)
    {
        g.Pop();
    }
}

1 comment:

Anonymous said...

Very useful info.
Thanks