CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Text Alignment

  1. #1
    Join Date
    Sep 2008
    Posts
    63

    Text Alignment

    Hi,

    How to align string to Left, Right, Center and Middle? I have done other alignment like middle center,middle left,middle right, top left etc using StringAligment.

    Help me to alignment string in Left, Right, Center and Middle.

    Code:
    StringFormat objStringFormat = new StringFormat();
    objStringFormat.Alignment = StringAlignment.Center;
    objStringFormat.LineAlignment = StringAlignment.Center;

    Thank you
    Saravana

  2. #2
    Join Date
    Nov 2009
    Location
    .net 3.5 csharp 2008 developer
    Posts
    36

    Re: Text Alignment

    Where are you trying to align your text? I mean, is it a UserControl you built yourself and want to render the text aligned somewhere on your control? Or is it a RichTextBox ?

    StringFormat is used to give DrawString (of the Graphics class) formatting flags.

    This will draw a string in the middle of a bitmap:
    Code:
    Graphics g = Graphics.FromImage(myBmp);
    StringFormat strFmt = new StringFormat();
    strFmt.Alignment = StringAlignment.Center;
    strFmt.LineAlignment = StringAlignment.Center;
    
    Rectangle rect = new Rectangle(0,0,myBmp.Width,myBmp.height);
    
    g.DrawString("asdf",SystemBrushes.WindowText,someFont,rect,strFmt);
    I might have the DrawString parameters wrong, haven't fired up Visual Studio yet

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured