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