Centering Text on Custom Button
-
I am reposting to add more detail. 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 finee.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. I have thoroughly tried using the StringFormatter to align the text accordingly. However, I have come up empty handed. When you set the LineAlignment to place the text at the bottom of the control, there doesn't seem to be the ability to "pad" the text from the bottom. Therefore, the text simply "sits" on the base or bottom of the control and fails to align with the base text. Any help would be greatly appreciated. Thanks. PHD
-
I am reposting to add more detail. 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 finee.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. I have thoroughly tried using the StringFormatter to align the text accordingly. However, I have come up empty handed. When you set the LineAlignment to place the text at the bottom of the control, there doesn't seem to be the ability to "pad" the text from the bottom. Therefore, the text simply "sits" on the base or bottom of the control and fails to align with the base text. Any help would be greatly appreciated. Thanks. PHD
I'd do your calculation like this:
float wCenterX = (float)(Width - wSizeWidth) / 2f;
This might be the solution. Also, I usually use the
System.Windows.Forms.TextRenderer.MeasureText
method for measuring text.Standards are great! Everybody should have one!
-
I am reposting to add more detail. 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 finee.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. I have thoroughly tried using the StringFormatter to align the text accordingly. However, I have come up empty handed. When you set the LineAlignment to place the text at the bottom of the control, there doesn't seem to be the ability to "pad" the text from the bottom. Therefore, the text simply "sits" on the base or bottom of the control and fails to align with the base text. Any help would be greatly appreciated. Thanks. PHD
Your code does not make much sense.
PHDENG81 wrote:
SizeF wSize = graphics.MeasureString(this.Text, this.Font);float wCenterX = (Width - wSize.Width) / 2;
you measure and calculate the centre of the string, but how is this related to the area that should contain the string. remember DrawString needs the upper left corner of where the string should be drawn. Hope this helps
-
I'd do your calculation like this:
float wCenterX = (float)(Width - wSizeWidth) / 2f;
This might be the solution. Also, I usually use the
System.Windows.Forms.TextRenderer.MeasureText
method for measuring text.Standards are great! Everybody should have one!
Hey Bekjong. Thanks a lot for your help. However, I tried both of your suggestions and it continues to react in the same way. Any other ideas? It appears as if it doesn't always grab the proper size of the bounding area for the text. Regardless of which method I use. If I tweak it a little with a few values, some of the text looks perfect. Thanks again. PHD
-
Your code does not make much sense.
PHDENG81 wrote:
SizeF wSize = graphics.MeasureString(this.Text, this.Font);float wCenterX = (Width - wSize.Width) / 2;
you measure and calculate the centre of the string, but how is this related to the area that should contain the string. remember DrawString needs the upper left corner of where the string should be drawn. Hope this helps
Andre, Thanks for your help. With all do respect, just because it doesn't make sense to you, doesn't mean it "doesn't make sense." It is simple math. If written down on paper, the equations seem to be correct. However, it would appear as if I am not receiving the proper values or something else may be skewed. Nevertheless, you have two boxes. One being the button and the other being the bounding box around the text. You are correct in saying that DrawString needs the upper left X and Y coordinates. Therefore, we merely need to divide both boxes in half and subtract the larger half from the smaller. This will give us the X and Y coordinates we desire. Or in this case, the X coordinate. The Y coordinate is working pefectly.
PointF mTextpt = new Point(0);//floating points used for X and Y coordinates
wSize = graphics.MeasureString(this.Text, this.Font);this.Width//gives us the width of the client rectangle. Our "bigger" box
this.Height//gives us the height of the client rectangle.
wSize.Width//this SHOULD be the width of the bounding box encompassing the text
wSize.Height//gives us the height of the bounding box encompassing the textfloat wCenterX = (float)((this.Width - wSize.Width) /2f);
mTextpt.X = wCenterX;
mTextpt.Y = this.Height - 19;e.graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), mTextpt);
I don't claim to have all of the answers. Clearly if that was the case, this simple problem of mine wouldn't be an issue. I must be missing something or screwing something up. I greatly appreciate your help. PHD
-
Andre, Thanks for your help. With all do respect, just because it doesn't make sense to you, doesn't mean it "doesn't make sense." It is simple math. If written down on paper, the equations seem to be correct. However, it would appear as if I am not receiving the proper values or something else may be skewed. Nevertheless, you have two boxes. One being the button and the other being the bounding box around the text. You are correct in saying that DrawString needs the upper left X and Y coordinates. Therefore, we merely need to divide both boxes in half and subtract the larger half from the smaller. This will give us the X and Y coordinates we desire. Or in this case, the X coordinate. The Y coordinate is working pefectly.
PointF mTextpt = new Point(0);//floating points used for X and Y coordinates
wSize = graphics.MeasureString(this.Text, this.Font);this.Width//gives us the width of the client rectangle. Our "bigger" box
this.Height//gives us the height of the client rectangle.
wSize.Width//this SHOULD be the width of the bounding box encompassing the text
wSize.Height//gives us the height of the bounding box encompassing the textfloat wCenterX = (float)((this.Width - wSize.Width) /2f);
mTextpt.X = wCenterX;
mTextpt.Y = this.Height - 19;e.graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), mTextpt);
I don't claim to have all of the answers. Clearly if that was the case, this simple problem of mine wouldn't be an issue. I must be missing something or screwing something up. I greatly appreciate your help. PHD
To find the X you want to do the following: Find the center of the button. Take half of the width of the text and subtract it from the center of the button. That should do it.
The only way to speed up a Macintosh computer is at 9.8 m/sec/sec.
-
To find the X you want to do the following: Find the center of the button. Take half of the width of the text and subtract it from the center of the button. That should do it.
The only way to speed up a Macintosh computer is at 9.8 m/sec/sec.
Thanks for the response ExpertComing. The posts prior to yours describes my methods and they do precisely what you illustrate. However, they still don't seem to align with the windows.forms.button text when it is centered. Any other suggestions? Thanks again. PHD