Word wrapping on DrawString method

Discussion in 'Microsoft Dot NET Compact Framework' started by Marcos Cruz Arjona, May 22, 2004.

  1. Hi,

    In order to draw a formatted text inside a rectangle on a custom
    control, I use Graphics.DrawString(string, Font, Brush, RectangleF) method.
    However, when the text is too large for the specified rectangle, the method
    word wraps the text, instead of just clipping it on a single line.
    Therefore, do I have to use DrawText unmanaged function or is there a
    workaround?


    Best regards,

    M a r c o s
     
    Marcos Cruz Arjona, May 22, 2004
    #1
    1. Advertisements

  2. Try setting the RectangleF parameter to be the height of the single line of
    text. To do this, use MeasureString with a string short enough not to wrap

    --
    Alex Feinman
    ---
    Visit http://www.opennetcf.org
    "Marcos Cruz Arjona" <> wrote in message
    news:e%...
    > Hi,
    >
    > In order to draw a formatted text inside a rectangle on a custom
    > control, I use Graphics.DrawString(string, Font, Brush, RectangleF)

    method.
    > However, when the text is too large for the specified rectangle, the

    method
    > word wraps the text, instead of just clipping it on a single line.
    > Therefore, do I have to use DrawText unmanaged function or is there a
    > workaround?
    >
    >
    > Best regards,
    >
    > M a r c o s
    >
    >
     
    Alex Feinman [MVP], May 22, 2004
    #2
    1. Advertisements

  3. Alex,

    I had already tried this and it doesn't works. DrawString always cuts
    words (separated by spaces) that doesn't fit in the specified rectangle
    width, in spite of rectangle height.

    Thanks,

    M a r c o s

    "Alex Feinman [MVP]" <> escribió en el mensaje
    news:%...
    > Try setting the RectangleF parameter to be the height of the single line

    of
    > text. To do this, use MeasureString with a string short enough not to wrap
    >
    > --
    > Alex Feinman
    > ---
    > Visit http://www.opennetcf.org
    > "Marcos Cruz Arjona" <> wrote in message
    > news:e%...
    > > Hi,
    > >
    > > In order to draw a formatted text inside a rectangle on a custom
    > > control, I use Graphics.DrawString(string, Font, Brush, RectangleF)

    > method.
    > > However, when the text is too large for the specified rectangle, the

    > method
    > > word wraps the text, instead of just clipping it on a single line.
    > > Therefore, do I have to use DrawText unmanaged function or is there a
    > > workaround?
    > >
    > >
    > > Best regards,
    > >
    > > M a r c o s
    > >
    > >

    >
    >
     
    Marcos Cruz Arjona, May 22, 2004
    #3
  4. Marcos Cruz Arjona

    Tim Wilson Guest

    One way to do this is to define the clipping region for the Graphics object
    and then use the DrawString overload that takes X and Y coordinates [public
    void DrawString(string, Font, Brush, float, float)].

    <Basic Idea>
    e.Graphics.Clip = new Region(new Rectangle(0, 0, 50, 20));
    e.Graphics.DrawString("This is a pretty long string of text!", this.Font,
    new SolidBrush(Color.Black), 0, 0);
    e.Graphics.ResetClip();
    </Basic Idea>

    --
    Tim Wilson
    ..Net Compact Framework MVP
    {cf147fdf-893d-4a88-b258-22f68a3dbc6a}
    "Marcos Cruz Arjona" <> wrote in message
    news:%...
    > Alex,
    >
    > I had already tried this and it doesn't works. DrawString always cuts
    > words (separated by spaces) that doesn't fit in the specified rectangle
    > width, in spite of rectangle height.
    >
    > Thanks,
    >
    > M a r c o s
    >
    > "Alex Feinman [MVP]" <> escribió en el mensaje
    > news:%...
    > > Try setting the RectangleF parameter to be the height of the single line

    > of
    > > text. To do this, use MeasureString with a string short enough not to

    wrap
    > >
    > > --
    > > Alex Feinman
    > > ---
    > > Visit http://www.opennetcf.org
    > > "Marcos Cruz Arjona" <> wrote in message
    > > news:e%...
    > > > Hi,
    > > >
    > > > In order to draw a formatted text inside a rectangle on a custom
    > > > control, I use Graphics.DrawString(string, Font, Brush, RectangleF)

    > > method.
    > > > However, when the text is too large for the specified rectangle, the

    > > method
    > > > word wraps the text, instead of just clipping it on a single line.
    > > > Therefore, do I have to use DrawText unmanaged function or is there a
    > > > workaround?
    > > >
    > > >
    > > > Best regards,
    > > >
    > > > M a r c o s
    > > >
    > > >

    > >
    > >

    >
    >
     
    Tim Wilson, May 22, 2004
    #4
  5. Tim,

    I'll try this!

    Thanks,

    M a r c o s

    "Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> escribió en el
    mensaje news:...
    > One way to do this is to define the clipping region for the Graphics

    object
    > and then use the DrawString overload that takes X and Y coordinates

    [public
    > void DrawString(string, Font, Brush, float, float)].
    >
    > <Basic Idea>
    > e.Graphics.Clip = new Region(new Rectangle(0, 0, 50, 20));
    > e.Graphics.DrawString("This is a pretty long string of text!", this.Font,
    > new SolidBrush(Color.Black), 0, 0);
    > e.Graphics.ResetClip();
    > </Basic Idea>
    >
    > --
    > Tim Wilson
    > .Net Compact Framework MVP
    > {cf147fdf-893d-4a88-b258-22f68a3dbc6a}
    > "Marcos Cruz Arjona" <> wrote in message
    > news:%...
    > > Alex,
    > >
    > > I had already tried this and it doesn't works. DrawString always cuts
    > > words (separated by spaces) that doesn't fit in the specified rectangle
    > > width, in spite of rectangle height.
    > >
    > > Thanks,
    > >
    > > M a r c o s
    > >
    > > "Alex Feinman [MVP]" <> escribió en el

    mensaje
    > > news:%...
    > > > Try setting the RectangleF parameter to be the height of the single

    line
    > > of
    > > > text. To do this, use MeasureString with a string short enough not to

    > wrap
    > > >
    > > > --
    > > > Alex Feinman
    > > > ---
    > > > Visit http://www.opennetcf.org
    > > > "Marcos Cruz Arjona" <> wrote in message
    > > > news:e%...
    > > > > Hi,
    > > > >
    > > > > In order to draw a formatted text inside a rectangle on a custom
    > > > > control, I use Graphics.DrawString(string, Font, Brush, RectangleF)
    > > > method.
    > > > > However, when the text is too large for the specified rectangle, the
    > > > method
    > > > > word wraps the text, instead of just clipping it on a single line.
    > > > > Therefore, do I have to use DrawText unmanaged function or is there

    a
    > > > > workaround?
    > > > >
    > > > >
    > > > > Best regards,
    > > > >
    > > > > M a r c o s
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Marcos Cruz Arjona, May 22, 2004
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul G. Tobey [eMVP]

    Re: Graphics.DrawString

    Paul G. Tobey [eMVP], Jul 31, 2003, in forum: Microsoft Dot NET Compact Framework
    Replies:
    2
    Views:
    690
    Alex Feinman [MVP]
    Aug 1, 2003
  2. Butt Chin Chuan

    Drawline and DrawString Error?

    Butt Chin Chuan, Aug 1, 2003, in forum: Microsoft Dot NET Compact Framework
    Replies:
    2
    Views:
    2,933
    Butt Chin Chuan
    Aug 1, 2003
  3. Armin Sadeghi [MSFT]

    RE: DrawString on off-screen

    Armin Sadeghi [MSFT], Aug 2, 2003, in forum: Microsoft Dot NET Compact Framework
    Replies:
    1
    Views:
    251
    msnews.microsoft.com
    Aug 6, 2003
  4. Mark Reddick

    Re: Drawstring in picturbox not working (font?)

    Mark Reddick, Aug 6, 2003, in forum: Microsoft Dot NET Compact Framework
    Replies:
    0
    Views:
    252
    Mark Reddick
    Aug 6, 2003
  5. DrawString word wrap fails on Compact Framework

    , Oct 26, 2005, in forum: Microsoft Dot NET Compact Framework
    Replies:
    8
    Views:
    4,145
    cmohanc
    Jun 16, 2011
Loading...

Share This Page