Search code examples
c#winformsgraphicsgdi+gdi

Is it possible to draw the charcter partially in a cell rectangle?


I'm working in WinForms application. In my scenario, i need to hide the text at beginning insteaf of last character when resize the cell rectangle.I can resolved this by removing the characters from first and draw it in rectangle.

Now i need to draw the character partially while resizing the cell rectangle. Please refer to the attached image. For example, the cell value is "20.229.88", after resizing the cell value should be "0.229.88", but "0" should be shown as partially.

enter image description here

Please anyone help me if it is possible.

Thanks in advance.


Solution

  • You can pass a StringFormat object to the DrawString method, which sets the text to be right aligned within the rectangle, so then the left side gets cut off.

    var sf = new StringFormat(StringFormatFlags.NoWrap);
    sf.Alignment = StringAlignment.Far;
    sf.Trimming = StringTrimming.None;
    
    e.Graphics.DrawString("my text", this.Font, Brushes.Blue, new RectangleF(10,0,120,20), sf);