Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. My EllipseLabel - wrapping text question

My EllipseLabel - wrapping text question

Scheduled Pinned Locked Moved C#
graphicsquestiontutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    I wrote my first own control, it derives from Windows.Forms.Control, and it's a simple ellipse label. The code for drawing: protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics; Brush foreBrush = new SolidBrush(ForeColor); Brush backBrush = new SolidBrush(BackColor); g.FillEllipse(backBrush, ClientRectangle); StringFormat fmt = new StringFormat(); fmt.Alignment = StringAlignment.Center; fmt.LineAlignment = StringAlignment.Center; g.DrawString(Text, Font, foreBrush, ClientRectangle, fmt); base.OnPaint(pe); } } The question - how to implement so that the label wraps text if it is to long to fit in a single line?

    K 1 Reply Last reply
    0
    • A Anonymous

      I wrote my first own control, it derives from Windows.Forms.Control, and it's a simple ellipse label. The code for drawing: protected override void OnPaint(PaintEventArgs pe) { Graphics g = pe.Graphics; Brush foreBrush = new SolidBrush(ForeColor); Brush backBrush = new SolidBrush(BackColor); g.FillEllipse(backBrush, ClientRectangle); StringFormat fmt = new StringFormat(); fmt.Alignment = StringAlignment.Center; fmt.LineAlignment = StringAlignment.Center; g.DrawString(Text, Font, foreBrush, ClientRectangle, fmt); base.OnPaint(pe); } } The question - how to implement so that the label wraps text if it is to long to fit in a single line?

      K Offline
      K Offline
      keith maddox
      wrote on last edited by
      #2

      Hi, you can use MeasureString to give you the length of the string in drawing units (pixels) and compare the width of your control - border (or just client width if there is not an NC portion). Then of course, just extend the height of your control by the height of the current font, split, and wrap the line. You can get the font height with the Font property and call its Height property but make sure you use your graphics object's MeasureString method to get the length of the string in question. hope that is enough detail for you, if not let me know and i'll try to help further.

      I 1 Reply Last reply
      0
      • K keith maddox

        Hi, you can use MeasureString to give you the length of the string in drawing units (pixels) and compare the width of your control - border (or just client width if there is not an NC portion). Then of course, just extend the height of your control by the height of the current font, split, and wrap the line. You can get the font height with the Font property and call its Height property but make sure you use your graphics object's MeasureString method to get the length of the string in question. hope that is enough detail for you, if not let me know and i'll try to help further.

        I Offline
        I Offline
        ICustomTypeDescriptor
        wrote on last edited by
        #3

        That'll do, thanks a lot man!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups