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. Get rid of label margins

Get rid of label margins

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
15 Posts 5 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.
  • D Dave Kreskowiak

    Tony_P wrote:

    I can't use DrawString because I need to know the exact length of the text

    That's what MeasureString is for.

    A guide to posting questions on CodeProject[^]
    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
         2006, 2007, 2008

    T Offline
    T Offline
    Tony Pottier
    wrote on last edited by
    #6

    Alright I've just found this in the meantime. Thank you for your time!

    1 Reply Last reply
    0
    • D Dave Kreskowiak

      Tony_P wrote:

      I can't use DrawString because I need to know the exact length of the text

      That's what MeasureString is for.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      T Offline
      T Offline
      Tony Pottier
      wrote on last edited by
      #7

      this is what I came up with:

      public class EnhancedLabel : Label
      {
      protected override void OnPaint(PaintEventArgs e)
      {
      Graphics g = e.Graphics;
      Brush fore = new SolidBrush(this.ForeColor);
      Brush back = new SolidBrush(this.BackColor);

              SizeF s = g.MeasureString(this.Text, this.Font);
              this.Width = (int)s.Width;
              this.Height = (int)s.Height;
      
              g.FillRectangle(back, this.Left, this.Top, this.Width, this.Height);
              g.DrawString(this.Text, this.Font, fore, this.Location);
          }
      }
      
      T 1 Reply Last reply
      0
      • T Tony Pottier

        this is what I came up with:

        public class EnhancedLabel : Label
        {
        protected override void OnPaint(PaintEventArgs e)
        {
        Graphics g = e.Graphics;
        Brush fore = new SolidBrush(this.ForeColor);
        Brush back = new SolidBrush(this.BackColor);

                SizeF s = g.MeasureString(this.Text, this.Font);
                this.Width = (int)s.Width;
                this.Height = (int)s.Height;
        
                g.FillRectangle(back, this.Left, this.Top, this.Width, this.Height);
                g.DrawString(this.Text, this.Font, fore, this.Location);
            }
        }
        
        T Offline
        T Offline
        Tony Pottier
        wrote on last edited by
        #8

        And a version 2.0, much better ;o

        protected override void OnPaint(PaintEventArgs e)
        {
        Graphics g = e.Graphics;
        Brush fore = new SolidBrush(this.ForeColor);
        Brush back = new SolidBrush(this.BackColor);

                g.FillRectangle(back, this.Location.X, this.Location.Y, this.Width, this.Height);
                g.DrawString(this.Text, this.Font, fore, Point.Empty);
            }
        
            protected override void OnTextChanged(EventArgs e)
            {
                Graphics g = System.Drawing.Graphics.FromHwnd(this.Handle);
                SizeF s = g.MeasureString(this.Text, this.Font);
                this.Width = (int)s.Width;
                this.Height = (int)s.Height;
        
                base.OnTextChanged(e);
            }
        
        D 1 Reply Last reply
        0
        • T Tony Pottier

          And a version 2.0, much better ;o

          protected override void OnPaint(PaintEventArgs e)
          {
          Graphics g = e.Graphics;
          Brush fore = new SolidBrush(this.ForeColor);
          Brush back = new SolidBrush(this.BackColor);

                  g.FillRectangle(back, this.Location.X, this.Location.Y, this.Width, this.Height);
                  g.DrawString(this.Text, this.Font, fore, Point.Empty);
              }
          
              protected override void OnTextChanged(EventArgs e)
              {
                  Graphics g = System.Drawing.Graphics.FromHwnd(this.Handle);
                  SizeF s = g.MeasureString(this.Text, this.Font);
                  this.Width = (int)s.Width;
                  this.Height = (int)s.Height;
          
                  base.OnTextChanged(e);
              }
          
          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #9

          Just a couple of points - You should dispose of your brushes, or better still put them in using blocks.

          using (Brush back = new SolidBrush(ForeColor))
          {
          e.Graphics.FillRectangle(back, Location.X, Location.Y, Width, Height);
          }
          using (Brush fore = new SolidBrush(BackColor))
          {
          e.Graphics.DrawString(Text, Font, fore, Point.Empty);
          }

          You can use the Size struct's static Round method to convert the SizeF

          Size = Size.Round(s);

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

          T 1 Reply Last reply
          0
          • D DaveyM69

            Just a couple of points - You should dispose of your brushes, or better still put them in using blocks.

            using (Brush back = new SolidBrush(ForeColor))
            {
            e.Graphics.FillRectangle(back, Location.X, Location.Y, Width, Height);
            }
            using (Brush fore = new SolidBrush(BackColor))
            {
            e.Graphics.DrawString(Text, Font, fore, Point.Empty);
            }

            You can use the Size struct's static Round method to convert the SizeF

            Size = Size.Round(s);

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

            T Offline
            T Offline
            Tony Pottier
            wrote on last edited by
            #10

            Right, thank you for the tips. I used the brushes without even checking if it was a disposable object. Shame on me.

            L 1 Reply Last reply
            0
            • T Tony Pottier

              Right, thank you for the tips. I used the brushes without even checking if it was a disposable object. Shame on me.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #11

              Hi, the most expensive one, the one you really should dispose of, is the result of Graphics.FromHwnd() for the brushes, you might consider keeping them in a class member, rather than creating and disposing them all the time. creating a Graphics, as you do, is something I avoid even at modest cost; so I am not in favor of your 2.0 solution; instead I would do everything in OnPaint, possibly do MeasureString inside a test on text changed. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Fixturized forever. :confused:


              T realJSOPR 2 Replies Last reply
              0
              • L Luc Pattyn

                Hi, the most expensive one, the one you really should dispose of, is the result of Graphics.FromHwnd() for the brushes, you might consider keeping them in a class member, rather than creating and disposing them all the time. creating a Graphics, as you do, is something I avoid even at modest cost; so I am not in favor of your 2.0 solution; instead I would do everything in OnPaint, possibly do MeasureString inside a test on text changed. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Fixturized forever. :confused:


                T Offline
                T Offline
                Tony Pottier
                wrote on last edited by
                #12

                Well the problem is that I need the width/height of the label without painting it, so I can't do everything in the paint event. The best way is probably to store the brushes, for sure. Can I store the Graphics.FromHwnd() the same way or can the value of Graphics.FromHwnd() change?

                L 1 Reply Last reply
                0
                • T Tony Pottier

                  Well the problem is that I need the width/height of the label without painting it, so I can't do everything in the paint event. The best way is probably to store the brushes, for sure. Can I store the Graphics.FromHwnd() the same way or can the value of Graphics.FromHwnd() change?

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #13

                  Tony_P wrote:

                  I need the width/height of the label without painting it

                  Huh? if you need it inside OnPaint only, then do the MeasureString in OnPaint just before you use DrawString. if you need its result outside OnPaint, I would try and execute OnPaint once before you need the sizes. Keeping a Graphics does not feel right; for one it is an expensive object, and it contains many things that may be different each time you get a graphics; e.g. every time you move ab object over a Form, the Form may get Paint events with varying clip regions in the Graphics object. Also the user might change the resolution of the screen, and whatever more. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  Fixturized forever. :confused:


                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Hi, the most expensive one, the one you really should dispose of, is the result of Graphics.FromHwnd() for the brushes, you might consider keeping them in a class member, rather than creating and disposing them all the time. creating a Graphics, as you do, is something I avoid even at modest cost; so I am not in favor of your 2.0 solution; instead I would do everything in OnPaint, possibly do MeasureString inside a test on text changed. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    Fixturized forever. :confused:


                    realJSOPR Offline
                    realJSOPR Offline
                    realJSOP
                    wrote on last edited by
                    #14

                    Why don't you just write a function that does something like this:

                    class BlahBlah
                    {
                    SizeF m_textSize;

                    public SizeF MeasureText(string text)
                    {
                        (using Graphics graphics = Graphics.FromHwnd(this))
                        {
                            m\_textSize = graphics.MeasureString(text, someFont);
                        }
                        return m\_textSize;
                    }
                    

                    }

                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                    -----
                    "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                    L 1 Reply Last reply
                    0
                    • realJSOPR realJSOP

                      Why don't you just write a function that does something like this:

                      class BlahBlah
                      {
                      SizeF m_textSize;

                      public SizeF MeasureText(string text)
                      {
                          (using Graphics graphics = Graphics.FromHwnd(this))
                          {
                              m\_textSize = graphics.MeasureString(text, someFont);
                          }
                          return m\_textSize;
                      }
                      

                      }

                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #15

                      I will do such things only if I see no reasonable alternative; I try and avoid creating a new Graphics as much as I can. In OnPaint the Graphics is for free, so why not take advantage of that one? In the few 100K lines of C# code I have at hand, I found only two instances of Graphics.FromHwnd; I do use Graphics.FromImage of course when drawing onto a Bitmap, there simply is no alternative there. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      Fixturized forever. :confused:


                      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