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. Center Text on Custom Button

Center Text on Custom Button

Scheduled Pinned Locked Moved C#
graphics
7 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.
  • P Offline
    P Offline
    PHDENG81
    wrote on last edited by
    #1

    I have a custom button that I have created which inherits from windows.forms.button. I simply override the mouseenter, mouseover, and mousedown events to paint a custom gradient over the button. In doing so, I must redraw the available image and text. I have been able to align the image with the base. However, I am unable to do the same with the text. Specifically, centering. It is close, just not perfect. SizeF wSize = graphics.MeasureString(this.Text, this.Font); float wCenterX = (Width - wSize.Width) / 2; mTextPt.X = wCenterX;//this does not work properly mTextPt.Y = Height - 19;// this works fine e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), mTextpt); For most of the buttons, the text shifts slightly to the left when I use any of the custom events. Any suggestions would be greatly appreciated. Thanks. PHD

    M I 2 Replies Last reply
    0
    • P PHDENG81

      I have a custom button that I have created which inherits from windows.forms.button. I simply override the mouseenter, mouseover, and mousedown events to paint a custom gradient over the button. In doing so, I must redraw the available image and text. I have been able to align the image with the base. However, I am unable to do the same with the text. Specifically, centering. It is close, just not perfect. SizeF wSize = graphics.MeasureString(this.Text, this.Font); float wCenterX = (Width - wSize.Width) / 2; mTextPt.X = wCenterX;//this does not work properly mTextPt.Y = Height - 19;// this works fine e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), mTextpt); For most of the buttons, the text shifts slightly to the left when I use any of the custom events. Any suggestions would be greatly appreciated. Thanks. PHD

      M Offline
      M Offline
      Manoj Kumar Rai
      wrote on last edited by
      #2

      The only issue I can see with this code is of rounding off of the wCenterX. Even you have calculated it in float while setting it to the "mTextPt.X" the decimal part will be truncated. But this will have only 1 point difference.

      Manoj Never Gives up

      P 1 Reply Last reply
      0
      • M Manoj Kumar Rai

        The only issue I can see with this code is of rounding off of the wCenterX. Even you have calculated it in float while setting it to the "mTextPt.X" the decimal part will be truncated. But this will have only 1 point difference.

        Manoj Never Gives up

        P Offline
        P Offline
        PHDENG81
        wrote on last edited by
        #3

        Thanks Manoj. I checked the precision and it seems to be fine. Any other suggestions?

        1 Reply Last reply
        0
        • P PHDENG81

          I have a custom button that I have created which inherits from windows.forms.button. I simply override the mouseenter, mouseover, and mousedown events to paint a custom gradient over the button. In doing so, I must redraw the available image and text. I have been able to align the image with the base. However, I am unable to do the same with the text. Specifically, centering. It is close, just not perfect. SizeF wSize = graphics.MeasureString(this.Text, this.Font); float wCenterX = (Width - wSize.Width) / 2; mTextPt.X = wCenterX;//this does not work properly mTextPt.Y = Height - 19;// this works fine e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), mTextpt); For most of the buttons, the text shifts slightly to the left when I use any of the custom events. Any suggestions would be greatly appreciated. Thanks. PHD

          I Offline
          I Offline
          Ian Shlasko
          wrote on last edited by
          #4

          I would suggest looking into the StringFormat parameter. Use this overload of Graphics.DrawString instead:

          public void DrawString (
          string s,
          Font font,
          Brush brush,
          RectangleF layoutRectangle,
          StringFormat format
          )

          layoutRectangle would be the entire client area of the button (0,0,Width,Height) For the last parameter, create a new StringFormat, and set the horizontal and vertical alignments to center. Basically, let the framework do the dirty work.

          P 1 Reply Last reply
          0
          • I Ian Shlasko

            I would suggest looking into the StringFormat parameter. Use this overload of Graphics.DrawString instead:

            public void DrawString (
            string s,
            Font font,
            Brush brush,
            RectangleF layoutRectangle,
            StringFormat format
            )

            layoutRectangle would be the entire client area of the button (0,0,Width,Height) For the last parameter, create a new StringFormat, and set the horizontal and vertical alignments to center. Basically, let the framework do the dirty work.

            P Offline
            P Offline
            PHDENG81
            wrote on last edited by
            #5

            Thanks for the suggestion Ian. I should have mentioned that I tried using the StringFormat parameter. There doesn't seem to be any way to "pad" the text when using the StringFormat. Therefore, when you place the text at the bottom of the control, it simply places the text about a pixel or so above the bottom. Therefore, it doesn't align with the base text. Any other suggestions? Thanks.

            I 1 Reply Last reply
            0
            • P PHDENG81

              Thanks for the suggestion Ian. I should have mentioned that I tried using the StringFormat parameter. There doesn't seem to be any way to "pad" the text when using the StringFormat. Therefore, when you place the text at the bottom of the control, it simply places the text about a pixel or so above the bottom. Therefore, it doesn't align with the base text. Any other suggestions? Thanks.

              I Offline
              I Offline
              Ian Shlasko
              wrote on last edited by
              #6

              Sorry for the delayed response, but in case you haven't found an answer yet... If you're centering text, it doesn't really make a difference, but if you're aligning to a side, you might want to shrink the bounding box. Instead of (0,0,Width,Height), use, say, (2,2,Width-4,Height-4). That'll add a 2-pixel margin on all sides. You could try just tweaking this until it matches up.

              P 1 Reply Last reply
              0
              • I Ian Shlasko

                Sorry for the delayed response, but in case you haven't found an answer yet... If you're centering text, it doesn't really make a difference, but if you're aligning to a side, you might want to shrink the bounding box. Instead of (0,0,Width,Height), use, say, (2,2,Width-4,Height-4). That'll add a 2-pixel margin on all sides. You could try just tweaking this until it matches up.

                P Offline
                P Offline
                PHDENG81
                wrote on last edited by
                #7

                Hey Ian, Once again, thanks for the response. I don't seem to have an issue aligning to the sides. I just can't calculate the proper "X" value while centering the desired text. This is completely beyond me. I really appreciate your help. If you have any other suggestions, I would like to hear them. Thanks again PHD

                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