May 18th, 2019, 01:22 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Form Titlebar Text Centering
When I have Form1.Caption = "Some Text" it always shows it on the left. How can I center the text
May 18th, 2019, 01:38 PM
#2
Re: Form Titlebar Text Centering
Originally Posted by
Code Dummy
When I have Form1.Caption = "Some Text" it always shows it on the left.
No it doesn't. At least not under Windows 8.1. It's centered here.
Originally Posted by
Code Dummy
How can I center the text
I'd leave that to the System's discretion. Probably except if you're targeting at one specific OS version.
Wolfgang
May 18th, 2019, 02:16 PM
#3
Re: Form Titlebar Text Centering
Here...this works (at least) on Windows 10
Attachment 168519
May 18th, 2019, 02:45 PM
#4
Re: Form Titlebar Text Centering
I also would advise letting the system decide where it should go, better to have it consistent with other programs. If you code it in such a way as to pad it to the center on one system it may get pushed off to the right on another.
May 18th, 2019, 02:56 PM
#5
Thread Starter
Fanatic Member
Re: Form Titlebar Text Centering
I don't see what difference it makes about the OS. If you know the width of your Form isn't that the same on all OS's even if the width differs in size from one OS to another the value you get from Form1.Width would be correct. So, knowing the width how do I center text
May 18th, 2019, 03:11 PM
#6
Re: Form Titlebar Text Centering
Originally Posted by
SamOscarBrown
This code is not taking into account the size of the font, hence here where I have set a bigger font it does not center the text properly.
To fix that, I addded this line after the line formName.Font.Name = strPuffer :
Code:
formName.Font.Size = -ncm.lfCaptionFont.lfHeight / (1440 / Screen.TwipsPerPixelY) * 72
Still, it is not taking into account if the font is bold and italic, but those are less likely to be changed.
May 18th, 2019, 04:35 PM
#7
Re: Form Titlebar Text Centering
Correct Ed....but I would have hope my example would suffice in 95% of cases. :-)
May 19th, 2019, 12:11 PM
#8
Thread Starter
Fanatic Member
Re: Form Titlebar Text Centering
May 19th, 2019, 01:54 PM
#9
Re: Form Titlebar Text Centering
The WM_GETTITLEBARINFOEX message is available from Vista and later.
Attached is a modified version that works also on XP.
Note: it assumes that there are the three buttons at the title bar. If it is only the close button it will require a change (to delete '* 3.05').
Attached Files
May 19th, 2019, 02:40 PM
#10
Thread Starter
Fanatic Member
Re: Form Titlebar Text Centering
Originally Posted by
Eduardo-
The WM_GETTITLEBARINFOEX message is available from Vista and later.
Attached is a modified version that works also on XP.
Note: it assumes that there are the three buttons at the title bar. If it is only the close button it will require a change (to delete '* 3.05').
So does that mean to delete this line
Code:
theSpaces = (formName.ScaleWidth - GetSystemMetrics(SM_CXSIZE) * 3.05 * 15 - lngTitle) / 2
May 19th, 2019, 02:47 PM
#11
Re: Form Titlebar Text Centering
Originally Posted by
Code Dummy
So does that mean to delete this line
Code:
theSpaces = (formName.ScaleWidth - GetSystemMetrics(SM_CXSIZE) * 3.05 * 15 - lngTitle) / 2
I said to delete '* 3.05', not the whole line.
If you delete the whole line it won't work.
But only there is the need to delete '* 3.05' from the line in the case that the form has just the close button and not the minimize and maximize buttons.
Or even better, do it automatically. Replace the line with this one:
Code:
theSpaces = (formName.ScaleWidth - GetSystemMetrics(SM_CXSIZE) * If(formName.MinButton, 3.05, 1) * 15 - lngTitle) / 2
Attached Files
Last edited by Eduardo-; May 19th, 2019 at 02:57 PM .
May 19th, 2019, 05:18 PM
#12
Thread Starter
Fanatic Member
Re: Form Titlebar Text Centering
May 20th, 2019, 09:45 AM
#13
Re: Form Titlebar Text Centering
Originally Posted by
Code Dummy
It doesn't work on XP
What are you still doing on XP?
May 20th, 2019, 12:17 PM
#14
Thread Starter
Fanatic Member
Re: Form Titlebar Text Centering
Why do some people drive 25+ year old cars
May 20th, 2019, 12:20 PM
#15
Re: Form Titlebar Text Centering
May 20th, 2019, 12:21 PM
#16
Re: Form Titlebar Text Centering
But I have an 85 year old car ('34 Plymouth Coupe) that I drive.
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width